From aa474abeaa9ee40aa11d830578b2bbffe681e971 Mon Sep 17 00:00:00 2001 From: faridik Date: Tue, 31 Aug 2021 18:43:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BA=D0=B0=D0=BC=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D0=B2=D0=B8=D0=B4=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TelegramBot/bot.py | 59 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/TelegramBot/bot.py b/TelegramBot/bot.py index b3c7d6e..1cfe400 100644 --- a/TelegramBot/bot.py +++ b/TelegramBot/bot.py @@ -7,6 +7,7 @@ import telegram from telegram import ( ReplyKeyboardRemove, Update, + ParseMode, ) from telegram.ext import ( CallbackContext, @@ -26,7 +27,7 @@ TOKEN = Path(".secrets/bot_token.txt").read_text() MESSAGES = Messages() HOST = "http://carolyn-spreadsheets:5000" BROADCAST_MESSAGE, BROADCAST_PUBLISH_DONE = range(2) - +GRADES_CALLBACK = 1 logging.setLoggerClass(TgLogger) logging.basicConfig( @@ -103,11 +104,15 @@ def grades(update: Update, context: CallbackContext): update.message.reply_text( text=MESSAGES.Assignments.SELECT_ASSNT, reply_markup=reply_markup ) + return_value = GRADES_CALLBACK + except: LOG.exception("Failed to get grades.") update.message.reply_sticker(MESSAGES.Stickers.DEAD) + return_value = ConversationHandler.END diff = time.monotonic() - start - look_msg.edit_text(MESSAGES.Score.timeit(diff)) + look_msg.edit_text(MESSAGES.Assignments.timeit(diff)) + return return_value def callback(update: Update, context: CallbackContext): @@ -123,7 +128,7 @@ def callback(update: Update, context: CallbackContext): update.callback_query.message.edit_text( text=MESSAGES.Assignments.SELECT_COURSE, reply_markup=reply_markup ) - return + return GRADES_CALLBACK assignments = context.user_data["assignments"] name, subject = call.split("#")[1:3] @@ -135,7 +140,11 @@ def callback(update: Update, context: CallbackContext): f = lambda ass: ass["name"] == name and ass["subject"] == subject assignment = next(filter(f, assignments)) update.callback_query.message.edit_text( - text=assignment["name"], reply_markup=reply_markup + text=MESSAGES.Assignments.get(assignment['name'], + assignment['points'], + assignment['how_to_display']), + parse_mode=ParseMode.HTML, + reply_markup=reply_markup ) # Нажатие кнопок при выборе дисциплины @@ -153,6 +162,29 @@ def callback(update: Update, context: CallbackContext): MESSAGES.Assignments.SELECT_ASSNT, reply_markup=reply_markup ) + if call.startswith("cancel#"): + update.callback_query.message.edit_text( + MESSAGES.Assignments.END, + ) + return ConversationHandler.END + + return GRADES_CALLBACK + +def timeout(update: Update, context: CallbackContext): + update.callback_query.message.edit_text( + MESSAGES.Assignments.TIMEOUT, + ) + return ConversationHandler.END + +def grades_end(update: Update, context: CallbackContext): + update.message.edit_text( + MESSAGES.Assignments.TIMEOUT, + ) + update.message.reply_text( + MESSAGES.Assignments.END + ) + return ConversationHandler.END + def broadcast(update: Update, context: CallbackContext): """Команда: разослать студентам.""" @@ -231,9 +263,20 @@ def main() -> None: start_handler = CommandHandler("start", start) - grades_handler = CommandHandler("grades", grades) - - callback_handler = CallbackQueryHandler(callback) + grades_handler = ConversationHandler( + entry_points=[CommandHandler("grades", grades)], + states={ + GRADES_CALLBACK: [CallbackQueryHandler(callback)], + ConversationHandler.TIMEOUT: [ + MessageHandler(Filters.text | Filters.command, timeout) + ], + # ConversationHandler.END: [ + # MessageHandler(Filters.text | Filters.command, grades_end) + # ] + }, + fallbacks=[CommandHandler("cancel", cancel)], + conversation_timeout=10, + ) broadcast_handler = ConversationHandler( entry_points=[CommandHandler("broadcast", broadcast)], @@ -247,7 +290,7 @@ def main() -> None: dispatcher.add_handler(start_handler) dispatcher.add_handler(grades_handler) dispatcher.add_handler(broadcast_handler) - dispatcher.add_handler(callback_handler) + #dispatcher.add_handler(callback_handler) # Начало работы бота updater.start_polling()