def start(bot, update): global chat_id chat_id = update.message.chat_id db_worker = SQL_worker(config.database_name) cur_user = (chat_id, ) print(cur_user) exist = db_worker.searh_user() print(exist) if cur_user in exist: print("С возвращением!") bot.send_message(chat_id=update.message.chat_id, text=locale.old_user) else: db_worker.new_user(update.message.chat_id) dp.remove_handler(not_started) markup = telegram.ReplyKeyboardMarkup(locale.start_keyboard) bot.send_message(chat_id=update.message.chat_id, text=locale.welcome_message, reply_markup=markup) dp.add_handler(RegexHandler("^(Settings)$", settings_handler)) dp.add_handler(RegexHandler("^(Send Feedback)$", feedback_handler)) dp.add_handler(RegexHandler("^(Add new Task)$", new_task)) dp.add_handler(RegexHandler("^(Read my Tasks)$", read_task)) dp.add_handler(RegexHandler("^(Read my Tasks)$", read_task)) dp.add_handler( RegexHandler("^(Start daemon)$", start_daemon, pass_job_queue=True, pass_chat_data=True)) dp.add_handler(RegexHandler("^(My Stats)$", read_stats))
def start_daemon(bot, update, job_queue, chat_data): markup = telegram.ReplyKeyboardMarkup(locale.main_keyboard) bot.send_message(chat_id=update.message.chat_id, text="A new day was started.", reply_markup=markup) sql = SQL_worker(database=config.database_name) morning_time = str(sql.select_morning_time( user_id=update.message.chat_id)).replace("('", '').replace("',)", '').split(":") future = (datetime.datetime.now() + datetime.timedelta(days=0)).replace( hour=int(morning_time[0]), minute=int(morning_time[1])) delta = future - datetime.datetime.now() job = job_queue.run_once(new_day, delta, context=update.message.chat_id) chat_data['job'] = job evening_time = str(sql.select_evening_time( user_id=update.message.chat_id)).replace("('", '').replace("',)", '').split(":") future = (datetime.datetime.now() + datetime.timedelta(days=0)).replace( hour=int(evening_time[0]), minute=int(evening_time[1])) delta = future - datetime.datetime.now() job = job_queue.run_once(day_end, delta, context=update.message.chat_id) chat_data['job'] = job
def read_stats(bot, update): sql = SQL_worker(database=config.database_name) stats = (str(sql.select_stats(chat_id)).replace("('", '').replace("',)", '')).split('/') bot.send_message(chat_id=update.message.chat_id, text="You done " + stats[0] + " tasks of " + stats[1] + "\nPercentage of completed tasks: " + str(int(stats[0]) / int(stats[1])) + "%.")
def new_day(bot, job): sql = SQL_worker(database=config.database_name) current_tasks = (str(sql.select_task(chat_id)).replace("('", '').replace( "',)", '')).split(' ^$^ ') temp = "My tasks for today: \n" for i in current_tasks: print(i) temp += '- ' + i + '\n' print("It's a new day!\n" + temp) bot.send_message(job.context, text="It's a new day!\n" + temp)
def read_task(bot, update): sql = SQL_worker(database=config.database_name) current_tasks = (str(sql.select_task(update.message.chat_id)).replace( "('", '').replace("',)", '')).split(' ^$^ ') temp = "My tasks for today: \n" for i in current_tasks: print(i) temp += '- ' + i + '\n' print(temp) bot.send_message(chat_id=update.message.chat_id, text=temp)
def write_stats(bot, update): dp.remove_handler(text_handler) dp.remove_handler(none_work_handler) sql = SQL_worker(database=config.database_name) current_tasks = len( (str(sql.select_task(chat_id)).replace("('", '').replace("',)", '')).split(' ^$^ ')) stats = (str(sql.select_stats(chat_id)).replace("('", '').replace("',)", '')).split('/') try: temp = int(update.message.text) if current_tasks < temp: bot.send_message(chat_id=update.message.chat_id, text="Wrong value") stats[0] = str(int(stats[0]) + temp) stats[1] = str(int(stats[1]) + current_tasks) stats = "/".join(stats) sql.write_new_stats(stats=stats, user_id=update.message.chat_id) sql.write_new_task(task=locale.no_tasks, user_id=update.message.chat_id) bot.send_message(chat_id=update.message.chat_id, text=locale.done) except Exception: bot.send_message(chat_id=update.message.chat_id, text="Something went wrong. I don't understand you.")
def day_end(bot, job): """Send the alarm message.""" global text_handler, none_work_handler none_work_handler = RegexHandler("^(None)$", cancel_button) sql = SQL_worker(database=config.database_name) current_tasks = (str(sql.select_task(chat_id)).replace("('", '').replace( "',)", '')).split(' ^$^ ') none = telegram.ReplyKeyboardMarkup([["None"]]) bot.send_message(job.context, text="You had " + str(len(current_tasks)) + " tasks today\nHow many of them have you done?", reply_markup=none) text_handler = MessageHandler(Filters.text, write_stats) dp.add_handler(text_handler)
def set_morning_write(bot, update): sql = SQL_worker(database=config.database_name) temp = str(update.message.text).split(":") try: int(temp[0]) int(temp[1]) time = ":".join([temp[0], temp[1]]) print(time) if int(temp[0]) <= 23 or int(temp[1]) <= 59: sql.write_morning(user_id=update.message.chat_id, time=time) dp.remove_handler(text_handler) bot.send_message(chat_id=update.message.chat_id, text=locale.done, reply_markup=telegram.ReplyKeyboardMarkup( locale.start_keyboard)) else: bot.send_message(chat_id=update.message.chat_id, text="Wrong value") except ValueError: bot.send_message(chat_id=update.message.chat_id, text="Wrong value")
def task_writer(bot, update): sql = SQL_worker(database=config.database_name) current_tasks = (str(sql.select_task(update.message.chat_id)).replace( "('", '').replace("',)", '')).split(' ^$^ ') temp = str(update.message.text) if current_tasks[0] == "No actual tasks" or current_tasks[ 0] == "You haven't any tasks yet.": current_tasks = temp else: current_tasks = current_tasks + [temp] current_tasks = " ^$^ ".join(current_tasks) sql.write_new_task(current_tasks, update.message.chat_id) sql.close() bot.send_message(chat_id=update.message.chat_id, text=locale.done, reply_markup=telegram.ReplyKeyboardMarkup( locale.start_keyboard)) dp.remove_handler(cancel_work_handler) dp.remove_handler(new_task_handler)