From eab21831cf13c9afdafc59adde32c85b9716e76b Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 22 Feb 2023 11:08:15 -0800 Subject: fix /cancel flow --- bot.py | 6 +++--- callback.py | 22 +++++++++------------- command.py | 13 +++++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/bot.py b/bot.py index cd0d4ea..02182cd 100644 --- a/bot.py +++ b/bot.py @@ -55,15 +55,15 @@ def main() -> None: MessageHandler(filters.LOCATION, callback_location_sharing), ], LOCATION_SEARCH_KEYWORD: [ - MessageHandler(filters.TEXT, callback_location_keyword_search), + MessageHandler(filters.TEXT & ~filters.COMMAND, callback_location_keyword_search), CallbackQueryHandler(callback_skip_location_keyword), ], LOCATION_CONFIRMATION: [ CallbackQueryHandler(callback_location_confirmation), - MessageHandler(filters.TEXT, callback_manual_location) + MessageHandler(filters.TEXT & ~filters.COMMAND, callback_manual_location) ], ADD_COMMENT: [ - MessageHandler(filters.TEXT, callback_add_comment), + MessageHandler(filters.TEXT & ~filters.COMMAND, callback_add_comment), CallbackQueryHandler(callback_skip_comment), ], ADD_MEDIA: [MessageHandler(filters.PHOTO, callback_add_media), diff --git a/callback.py b/callback.py index a7f7181..6a09421 100644 --- a/callback.py +++ b/callback.py @@ -43,8 +43,6 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL context.user_data["status_id"] = status["id"] context.user_data["status_content"] = content - print("status_id", context.user_data["status_id"]) - await update.message.reply_text( text=f"Selected place: {poi['name']}, \nPosted to Mastodon: {status['url']}", parse_mode=ParseMode.MARKDOWN, @@ -78,8 +76,6 @@ async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT context.user_data["status_id"] = status["id"] context.user_data["status_content"] = content - print("status_id", context.user_data["status_id"]) - await update.message.reply_text( text=f"Manually selected place: {loc}, \nPosted to Mastodon: {status['url']}", parse_mode=ParseMode.MARKDOWN, @@ -109,8 +105,6 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D context.user_data["status_id"] = status["id"] context.user_data["status_content"] = content - print("status_id", context.user_data["status_id"]) - await query.message.reply_text( text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", parse_mode=ParseMode.MARKDOWN, @@ -125,12 +119,12 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_LOCATION_KEYWORD]) - location_search = update.effective_message.text + location_search_keyword = update.effective_message.text latitude = context.user_data["latitude"] longitude = context.user_data["longitude"] keyboard = [] - poi_result = query_poi(location_search, latitude, longitude) + poi_result = query_poi(location_search_keyword, latitude, longitude) if len(poi_result) == 0: poi_result = query_poi("", latitude, longitude) @@ -140,13 +134,13 @@ async def callback_location_keyword_search(update: Update, context: ContextTypes ]) if len(keyboard) == 0: - await update.message.reply_text(PROMPT_NO_NEARBY_POI) - return LOCATION_CONFIRMATION + msg = await update.message.reply_text(PROMPT_NO_NEARBY_POI) else: reply_markup = InlineKeyboardMarkup(keyboard) - context.user_data["location_search"] = location_search - await update.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup) + context.user_data["location_search"] = location_search_keyword + msg = await update.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup) + context.user_data[PROMPT_CHOOSE_POI_FROM_LIST] = msg.message_id return LOCATION_CONFIRMATION @@ -188,7 +182,9 @@ async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYP async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_ADD_COMMENT]) - prompt_attach_photo_msg = await update.message.reply_text(PROMPT_ADD_MEDIA, reply_markup=INLINE_SKIP_MENU) + prompt_attach_photo_msg = await context.bot.send_message(chat_id=update.effective_chat.id, + text=PROMPT_ADD_MEDIA, + reply_markup=INLINE_SKIP_MENU) context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id return ADD_MEDIA diff --git a/command.py b/command.py index 09340fc..579fa48 100644 --- a/command.py +++ b/command.py @@ -1,5 +1,6 @@ from telegram import Update from telegram.constants import ParseMode +from telegram.error import BadRequest from telegram.ext import ContextTypes, ConversationHandler from config import * @@ -24,5 +25,17 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No async def cancel_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + for prompt in [PROMPT_LOCATION_KEYWORD, PROMPT_CHOOSE_POI_FROM_LIST, PROMPT_ADD_COMMENT, PROMPT_ADD_MEDIA]: + try: + if context.user_data.get(prompt): + await context.bot.delete_message(chat_id=update.message.chat_id, + message_id=context.user_data[prompt]) + except BadRequest as e: + if "not found" in str(e.message): + pass + except Exception as e: + print(e) + await update.message.reply_text(text=PROMPT_CANCELED, reply_markup=MAIN_MENU) + context.user_data.clear() return ConversationHandler.END -- cgit v1.2.3