def import_yes_replace_callback_query_handler(call): tasks = get_tasks(call.from_user.id) import_file_name = call.data.split(':')[1] with open(import_file_name, 'rb') as f: imported_tasks = pickle.load(f) if call.data.startswith('import_yes'): tasks += imported_tasks status_message = bot.send_message(call.from_user.id, 'Добавение...') response = 'да' else: tasks = imported_tasks status_message = bot.send_message(call.from_user.id, 'Замена...') response = 'заменить' save_tasks(tasks, call.from_user.id) os.remove(import_file_name) chat_id, message_id = status_message.chat.id, status_message.message_id bot.edit_message_text(status_message.text + ' Готово!', chat_id, message_id) bot.edit_message_text(call.message.text + ' (ваш ответ: ' + response + ')', call.message.chat.id, call.message.message_id) bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=types.InlineKeyboardMarkup())
def new_item_handler(message): try: task_name = message.text.replace('/new_item ', '') except: task_name = None if task_name == '/new_item': bot.send_message(message.chat.id, 'Как называется новая задача?') change_user_state(message.from_user.id, 'new_item') return tasks = get_tasks(message.from_user.id) summary = task_name if summary == None: summary = message.caption.replace('/new_item ', '') media = None ct = message.content_type if ct != 'text': media = (ct, eval('message.' + ct)[0].file_id) # summary: str, isDone: bool, media: tuple, creation_date: str, completion_date: str, message_id: str tasks.append([ summary, False, media, str(datetime.datetime.now())[:-7], None, message.message_id ]) n = len(tasks) save_tasks(tasks, message.from_user.id) bot.send_message(message.chat.id, 'Задача номер ' + str(n - 1) + ' успешно добавлена!', parse_mode='html')
def delete_handler(message): '''Удалить задачу по номеру''' task_number = message.text.replace('/delete ', '') if task_number == '/delete': bot.send_message(message.chat.id, 'Какой номер у задачи, которую нужно удалить?') change_user_state(message.from_user.id, 'delete') return task_number = int(task_number) tasks = get_tasks(message.from_user.id) try: deleted = tasks.pop(task_number) except: bot.send_message( message.chat.id, 'Задачи с таким номером нет! /all для просмотра всех задач.') return save_tasks(tasks, message.from_user.id) bot.send_message(message.chat.id, 'Задача ``` ' + deleted[0] + '``` успешно удалена!', parse_mode='markdown')
def show_all_handler(message): '''Показать все задачи''' tasks = get_tasks(message.from_user.id) bot.send_message(message.chat.id, task_list_to_str(tasks), parse_mode='html')
def done_callback_query_handler(call): task_number, message_id, chat_id, user_id = [ int(x) for x in call.data.split(':')[1:] ] message = bot.edit_message_reply_markup(chat_id, message_id) tasks = get_tasks(user_id) tasks[task_number][1] = True tasks[task_number][4] = str(datetime.datetime.now())[:-7] save_tasks(tasks, user_id) show_task_handler(message, True, task_number, user_id)
def undone_handler(message): '''Вернуть (к несделанным) задачу по номеру''' task_number = message.text.replace('/undone ', '') if task_number == '/undone': bot.send_message(message.chat.id, 'Какой номер у задачи, которую нужно вернуть?') change_user_state(message.from_user.id, 'undone') return task_number = int(task_number) tasks = get_tasks(message.from_user.id) tasks[task_number][1] = False tasks[task_number][4] = None save_tasks(tasks, message.from_user.id) bot.send_message(message.chat.id, 'Задача ``` ' + tasks[task_number][0] + '``` успешно возвращена!', parse_mode='markdown')
def done_handler(message): '''Зачеркнуть задачу по номеру''' task_number = message.text.replace('/done ', '') if task_number == '/done': bot.send_message(message.chat.id, 'Какой номер у задачи, которую нужно зачеркнуть?') change_user_state(message.from_user.id, 'done') return task_number = int(task_number) tasks = get_tasks(message.from_user.id) tasks[task_number][1] = True tasks[task_number][4] = str(datetime.datetime.now())[:-7] save_tasks(tasks, message.from_user.id) bot.send_message(message.chat.id, 'Задача ``` ' + tasks[task_number][0] + '``` успешно зачёркнута!', parse_mode='markdown')
def show_task_handler(message, replace=False, task_number=None, user_id=None): '''Показывает сообщение с картинкой (даже если картинка не была прикреплена); эта функция вызывается также с помощью кнопки, поэтому ей нужно знать task_number и user_id''' if type(task_number) != int: task_number = message.text.replace('/show_task ', '') if task_number == '/show_task': bot.send_message(message.chat.id, 'Какой номер у задачи, которую мне нужно показать?') change_user_state(message.from_user.id, 'show_task') return task_number = int(task_number) if user_id == None: user_id = message.from_user.id tasks = get_tasks(user_id) task = tasks[task_number] task_text = str( task_number) + ') ' + task[0] + '\n\n🕰 Добавлена: ' + task[3] if task[1]: task_text += '\n✅ Выполнена: ' + task[4] if task[2]: content_type, file_id = task[2] if replace: showed_task = eval( f'bot.edit_message_media(types.InputMedia{content_type.capitalize()}(file_id, caption=task_text), message.chat.id, message.message_id)' ) else: showed_task = eval( f'bot.send_{content_type}(message.chat.id, {content_type}={repr(file_id)}, caption={repr(task_text)})' ) else: if replace: try: showed_task = bot.edit_message_media( types.InputMediaPhoto(emptyPhoto, caption=task_text), message.chat.id, message.message_id) except: showed_task = bot.edit_message_caption(task_text + '`!`', message.chat.id, message.message_id, parse_mode='markdown') else: showed_task = bot.send_photo(message.chat.id, emptyPhoto, task_text) message_id = ':' + str(showed_task.message_id) chat_id = ':' + str(showed_task.chat.id) user_id = ':' + str(user_id) next_number = (task_number + 1) % len(tasks) prev_number = (task_number - 1) % len(tasks) keyboard = types.InlineKeyboardMarkup() callback_button_next = types.InlineKeyboardButton( text='>>', callback_data='show:' + str(next_number) + message_id + chat_id + user_id) callback_button_previous = types.InlineKeyboardButton( text='<<', callback_data='show:' + str(prev_number) + message_id + chat_id + user_id) callback_button_done = types.InlineKeyboardButton( text='✅', callback_data='done:' + str(task_number) + message_id + chat_id + user_id) callback_button_link = types.InlineKeyboardButton( text='Показать сообщение', callback_data='reply:' + str(task[5]) + chat_id + ':.') keyboard.row(callback_button_previous, callback_button_done, callback_button_next) keyboard.add(callback_button_link) bot.edit_message_reply_markup(showed_task.chat.id, showed_task.message_id, reply_markup=keyboard)