From 353b83d415061bff2881cbe324273409740be64c Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Fri, 24 Feb 2023 21:08:59 -0800 Subject: bot: implement delayed checkin, (kind of), some TODO left --- bot.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'bot.py') diff --git a/bot.py b/bot.py index 364a85f..fe45970 100644 --- a/bot.py +++ b/bot.py @@ -11,6 +11,7 @@ from starlette.requests import Request from starlette.responses import PlainTextResponse, Response, JSONResponse from starlette.routing import Route from telegram import Update +from telegram.error import BadRequest from telegram.ext import ( Application, CallbackContext, @@ -33,7 +34,8 @@ from callback import ( callback_skip_location_keyword_search, callback_add_comment, callback_skip_comment, - callback_add_media + callback_add_media, + callback_delayed_checkin ) from command import ( start_command, @@ -44,7 +46,8 @@ from command import ( toggle_visibility_command, callback_toggle_visibility, logout_command, - list_command + list_command, + delayed_checkin_command, ) from config import ( FEDI_LOGIN, @@ -57,7 +60,8 @@ from config import ( BOT_TOKEN, BOT_SCOPE, MAIN_MENU, - WAIT_VISIBILITY + WAIT_VISIBILITY, + DELAYED_CHECKIN, ) from prompt.string import PROMPT_CHOOSE_ACTION @@ -114,10 +118,14 @@ async def process_oauth_login_callback(update: FediLoginCallbackUpdate, context: user.access_key = encrypt(access_token, ENCRYPT_KEY) user.save() - text = "You have successfully logged in to your Mastodon account!" + text = "You have successfully logged in to your Mastodon account!" + try: await context.bot.delete_message(chat_id=user.telegram_user_id, message_id=context.user_data[PROMPT_FEDI_LOGIN]) await context.bot.send_message(chat_id=user.telegram_user_id, text=text) await context.bot.send_message(chat_id=user.telegram_user_id, text=PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU) + except BadRequest as e: + if "not found" in str(e.message): + pass async def main() -> None: @@ -176,11 +184,26 @@ async def main() -> None: allow_reentry=True, ) + delayed_checkin_handler = ConversationHandler( + entry_points=[ + CommandHandler("delay", delayed_checkin_command), + ], + states={ + DELAYED_CHECKIN: [ + MessageHandler(filters.TEXT & ~filters.COMMAND, callback_delayed_checkin), + ], + }, + fallbacks=[CommandHandler("cancel", cancel_command)], + per_message=False, + allow_reentry=True, + ) + application.add_handler(CommandHandler("logout", logout_command)) application.add_handler(CommandHandler("list", list_command)) application.add_handler(CommandHandler("Help", help_command)) application.add_handler(TypeHandler(type=FediLoginCallbackUpdate, callback=process_oauth_login_callback)) + application.add_handler(delayed_checkin_handler, 3) application.add_handler(visibility_conversation_handler, 2) application.add_handler(checkin_handler, 1) -- cgit v1.2.3