def show_tasks(message, db_collection): tasks = user_get_tasks(message, db_collection) tables = create_tables(tasks, ui.table_asset, ui.pic_task_status, ui.menu_list_items['important'], ui.menu_list_items['ordinary']) bot.send_message(message.from_user.id, tables[0], parse_mode='HTML') bot.send_message(message.from_user.id, tables[1], parse_mode='HTML')
def callback_inline_keyboard(call): if call.message: if call.data == list(ui.button_blanks.keys())[0]: # Show expanded help text = ui.help_todo['full_info'] bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=text, parse_mode='HTML', reply_markup=None) if call.data == list(ui.button_blanks.keys())[1]: # Delete task del_status = del_task(call, COLLECTION, call.message.text) if del_status: text = ui.dialogue['delete_task_success'].format( ui.different_signs['warning']) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=text) else: text = ui.dialogue['delete_failed'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=text) if call.data == list(ui.button_blanks.keys())[2]: # Edit text task edit_text(call, COLLECTION, call.message.text) if call.data == list(ui.button_blanks.keys())[3]: # Mark the task as completed switch_task_status(call, COLLECTION, call.message.text) if call.data == list(ui.button_blanks.keys())[4]: # Mark task as not completed switch_task_status(call, COLLECTION, call.message.text)
def add_new_text(msg, need_list, need_index): new_text = msg.text user_task = user_get_tasks(msg, db_collection)[need_list][need_index] user_task['task'] = new_text update_status = user_update_task(msg, COLLECTION, need_list, need_index, user_task) if update_status: notify_text = ui.dialogue['text_changed'].format( ui.different_signs['warning']) bot.send_message(chat_id=call.message.chat.id, text=notify_text, reply_markup=MENU_BACK) new_screen_info = edited_task_card(user_task) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=new_screen_info[0], reply_markup=new_screen_info[1], parse_mode='HTML') else: notify_text = ui.dialogue['text_not_changed'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=notify_text, reply_markup=MENU_BACK)
def todo_start(message): user_registered_status = utils.db.add_user(message, COLLECTION) user_first_name = message.from_user.first_name if not user_registered_status: sticker = open(ui.sticker_path['returned'], 'rb') bot.send_sticker(message.from_user.id, sticker) quantity = utils.todo.quantity_tasks(utils.db.user_get_tasks(message, COLLECTION)) text = ui.dialogue['user_returned'] \ .format(user_first_name, ui.menu_list_items['important'].replace('й', 'х'), quantity['important'], ui.menu_list_items['ordinary'].replace('й', 'x'), quantity['ordinary']) bot.send_message(message.from_user.id, text, reply_markup=MENU_MAIN, parse_mode='HTML') else: bot_name = ui.bot_name sticker = open(ui.sticker_path['hello'], 'rb') bot.send_sticker(message.from_user.id, sticker) text = ui.dialogue['welcome_text'].format(user_first_name, bot_name) bot.send_message(message.from_user.id, text, reply_markup=MENU_MAIN) notifi_data = utils.db.user_get_notifications(message, COLLECTION) if notifi_data['notifications']: schedule.clear(message.from_user.id) job = utils.todo.run_notifi(message, notifi_data['notification_interval']) else: schedule.clear(message.from_user.id)
def interval_validity(message): try: answer = int(message.text) if MIN_VALUE <= answer <= MAX_VALUE: return answer else: raise ValueError except ValueError: text = ui.dialogue['interval_error'].format( ui.different_signs['exclamatory'], message.from_user.first_name) bot.send_message(message.from_user.id, text, reply_markup=MENU_NOTIFI)
def notifi_change_status(message, db_collection, status: bool): success_change = user_update_notification(message, db_collection, 'notifications', status) if success_change: if not status: text = ui.dialogue['notifications_off'].format( ui.different_signs['bell_off']) bot.send_message(message.from_user.id, text) if status: text = ui.dialogue['notifications_on'].format( ui.different_signs['bell']) bot.send_message(message.from_user.id, text)
def show_card(cards_tasks): show_card_text = cards_tasks['head'] bot.send_message(message.from_user.id, show_card_text, parse_mode='HTML') if cards_tasks['cards_full_info'] is not None: for index, current_dict in enumerate( cards_tasks['cards_full_info']): if current_dict.get('task_status'): show_card_text = cards_tasks['screen_info'][index] bot.send_message(message.from_user.id, show_card_text, reply_markup=BTN_CNL_BAR_NOT_READY, parse_mode='HTML') else: show_card_text = cards_tasks['screen_info'][index] bot.send_message(message.from_user.id, show_card_text, reply_markup=BTN_CNL_BAR_READY, parse_mode='HTML') else: show_card_text = cards_tasks['screen_info'] bot.send_message(message.from_user.id, show_card_text, parse_mode='HTML')
def edit_text(call, db_collection, text): target_id = extract_id(text) target_list = find_list(call, db_collection, target_id) task_index = find_index_task(call, db_collection, target_id, target_list) if target_list and task_index is not False: bot.send_message(chat_id=call.message.chat.id, text=ui.dialogue['new_text'].format( ui.different_signs['warning']), reply_markup=telebot.types.ReplyKeyboardRemove()) def add_new_text(msg, need_list, need_index): new_text = msg.text user_task = user_get_tasks(msg, db_collection)[need_list][need_index] user_task['task'] = new_text update_status = user_update_task(msg, COLLECTION, need_list, need_index, user_task) if update_status: notify_text = ui.dialogue['text_changed'].format( ui.different_signs['warning']) bot.send_message(chat_id=call.message.chat.id, text=notify_text, reply_markup=MENU_BACK) new_screen_info = edited_task_card(user_task) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=new_screen_info[0], reply_markup=new_screen_info[1], parse_mode='HTML') else: notify_text = ui.dialogue['text_not_changed'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=notify_text, reply_markup=MENU_BACK) bot.register_next_step_handler(call.message, add_new_text, target_list, task_index) else: text = ui.dialogue['wrong_edit_task'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=text)
def task_notification(message): def get_not_ready_tasks(): result = [] for index, current_task in enumerate(tasks_important): if not current_task.get('task_status'): result.append(tasks_important[index]) else: continue return result tasks_important = user_get_tasks(message, COLLECTION)['important'] if tasks_important is not None: task = get_not_ready_tasks() if task: random_task = choice(task) text_task = random_task.get('task') text = ui.dialogue['task_notification'].format( ui.different_signs['warning'], message.from_user.first_name, text_task) bot.send_message(message.from_user.id, text)
def interval_change(message): new_interval = interval_validity(message) if new_interval: notifi_status = user_get_notifications(message, COLLECTION)['notifications'] if not notifi_status: notifi_change_status(message, COLLECTION, True) success_change = user_update_notification(message, COLLECTION, 'notification_interval', new_interval) if success_change: text = ui.dialogue['new_interval'].format( ui.different_signs['warning']) bot.send_message(message.from_user.id, text) todo_start(message) else: text = ui.dialogue['interval_fail'].format( ui.different_signs['exclamatory']) bot.send_message(message.from_user.id, text, reply_markup=MENU_NOTIFI)
def switch_task_status(call, db_collection, text): target_id = extract_id(text) target_list = find_list(call, db_collection, target_id) task_index = find_index_task(call, db_collection, target_id, target_list) if target_list and task_index is not False: user_task = user_get_tasks(call, db_collection)[target_list][task_index] if user_task.get('task_status'): user_task['task_status'] = False else: user_task['task_status'] = True update_status = user_update_task(call, COLLECTION, target_list, task_index, user_task) if update_status: new_screen_info = edited_task_card(user_task) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=new_screen_info[0], reply_markup=new_screen_info[1], parse_mode='HTML') text = ui.dialogue['task_status_changed'].format( ui.different_signs['warning']) bot.send_message(chat_id=call.message.chat.id, text=text) else: text = ui.dialogue['task_status_not_changed'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=text) else: text = ui.dialogue['wrong_task_status'].format( ui.different_signs['exclamatory']) bot.send_message(chat_id=call.message.chat.id, text=text)
def add_task(message, list_name, db_collection): rec_status = None if list_name == 'important': task = create_task(message.text) rec_status = user_set_task(message, db_collection, list_name, task) if list_name == 'ordinary': task = create_task(message.text) rec_status = user_set_task(message, db_collection, list_name, task) if not rec_status: text = ui.dialogue['not_success'] bot.send_message(message.from_user.id, text) else: text = ui.dialogue['add_success'] bot.send_message(message.from_user.id, text) text = ui.dialogue['change_list'] bot.send_message(message.from_user.id, text, reply_markup=MENU_CASE)
def todo_main(message): if message.chat.type == 'private': if message.text == ui.menu_main_items['help']: bot.send_message(message.from_user.id, ui.help_todo['short_info'], parse_mode='HTML') text = ui.dialogue['more_info'].format( message.from_user.first_name) bot.send_message(message.from_user.id, text, reply_markup=BTN_MORE_HELP) if message.text == ui.menu_main_items['add']: text = ui.dialogue['change_list'] bot.send_message(message.from_user.id, text, reply_markup=MENU_CASE) if message.text == ui.menu_list_items['important']: list_name = list(ui.menu_list_items.keys())[0] text = ui.dialogue['task_desc'] notification_before_task(message, text) bot.register_next_step_handler(message, add_task, list_name, COLLECTION) if message.text == ui.menu_list_items['ordinary']: list_name = list(ui.menu_list_items.keys())[1] text = ui.dialogue['task_desc'] notification_before_task(message, text) bot.register_next_step_handler(message, add_task, list_name, COLLECTION) if message.text == ui.menu_list_items['back']: sticker = open(ui.sticker_path['main'], 'rb') bot.send_sticker(message.from_user.id, sticker) text = ui.dialogue['main_menu'] bot.send_message(message.from_user.id, text, reply_markup=MENU_MAIN) if message.text == ui.menu_main_items['view']: show_tasks(message, COLLECTION) if message.text == ui.menu_main_items['edit']: edit_tasks(message, COLLECTION) if message.text == ui.menu_main_items['notifi']: text = ui.dialogue['notification_settings'].format( ui.different_signs['gear']) bot.send_message(message.from_user.id, text, reply_markup=MENU_NOTIFI) if message.text == ui.menu_settings_notification['notification_off']: notifi_change_status(message, COLLECTION, False) todo_start(message) if message.text == ui.menu_settings_notification[ 'notification_interval']: text = ui.interval_warning['desc_warning'].format(min=MIN_VALUE, max=MAX_VALUE) bot.send_message(message.from_user.id, text) pre_interval_notifications(message) bot.register_next_step_handler(message, interval_change)
def edit_tasks(message, db_collection): tasks = user_get_tasks(message, db_collection) if tasks['important'] is None and tasks['ordinary'] is None: text = ui.dialogue['lists_empty'].format(ui.different_signs['warning']) bot.send_message(message.from_user.id, text, reply_markup=MENU_BACK) else: text = ui.dialogue['lists_ready_change'].format( ui.different_signs['warning']) bot.send_message(message.from_user.id, text, reply_markup=MENU_BACK) cards = [] def create_task_cards(asset, pic_status, txt_status, tasks_dict, list_name, item_list): blank = { 'head': asset['card_head'].format(list_item=item_list, separator=asset['separator']), 'cards_full_info': tasks_dict[list_name], 'screen_info': [] } if blank['cards_full_info'] is not None: for value in blank['cards_full_info']: if value.get('task_status'): txt, pic = txt_status['ready'], pic_status['ready'] else: txt, pic = txt_status['not_ready'], pic_status['not_ready'] blank['screen_info'].append(asset['card_desc'].format( task=value.get('task'), status=txt, pic_status=pic, task_id=value.get('task_id'))) cards.append(blank) else: blank['screen_info'].append(asset['card_empty']) cards.append(blank) def show_card(cards_tasks): show_card_text = cards_tasks['head'] bot.send_message(message.from_user.id, show_card_text, parse_mode='HTML') if cards_tasks['cards_full_info'] is not None: for index, current_dict in enumerate( cards_tasks['cards_full_info']): if current_dict.get('task_status'): show_card_text = cards_tasks['screen_info'][index] bot.send_message(message.from_user.id, show_card_text, reply_markup=BTN_CNL_BAR_NOT_READY, parse_mode='HTML') else: show_card_text = cards_tasks['screen_info'][index] bot.send_message(message.from_user.id, show_card_text, reply_markup=BTN_CNL_BAR_READY, parse_mode='HTML') else: show_card_text = cards_tasks['screen_info'] bot.send_message(message.from_user.id, show_card_text, parse_mode='HTML') create_task_cards( ui.card_asset, ui.pic_task_status, ui.txt_task_status, tasks, 'important', ui.menu_list_items['important'], ) create_task_cards( ui.card_asset, ui.pic_task_status, ui.txt_task_status, tasks, 'ordinary', ui.menu_list_items['ordinary'], ) show_card(cards[0]) show_card(cards[1])
def notification_before_task(message, text): bot.send_message(message.from_user.id, text, reply_markup=telebot.types.ReplyKeyboardRemove())
def pre_interval_notifications(message): text = ui.dialogue['input_interval'] bot.send_message(message.from_user.id, text, reply_markup=telebot.types.ReplyKeyboardRemove())