diff options
author | clarkzjw <[email protected]> | 2023-02-22 11:08:15 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-22 11:08:15 -0800 |
commit | eab21831cf13c9afdafc59adde32c85b9716e76b (patch) | |
tree | faaf96f2ccab196a4eb307a40886ed1ad32b750c | |
parent | 0590292da6eec52e6f569749037c8f8f7bdd948e (diff) | |
download | swarm2fediverse-eab21831cf13c9afdafc59adde32c85b9716e76b.tar.gz |
fix /cancel flow
-rw-r--r-- | bot.py | 6 | ||||
-rw-r--r-- | callback.py | 22 | ||||
-rw-r--r-- | command.py | 13 |
3 files changed, 25 insertions, 16 deletions
@@ -55,15 +55,15 @@ def main() -> None: | |||
55 | MessageHandler(filters.LOCATION, callback_location_sharing), | 55 | MessageHandler(filters.LOCATION, callback_location_sharing), |
56 | ], | 56 | ], |
57 | LOCATION_SEARCH_KEYWORD: [ | 57 | LOCATION_SEARCH_KEYWORD: [ |
58 | MessageHandler(filters.TEXT, callback_location_keyword_search), | 58 | MessageHandler(filters.TEXT & ~filters.COMMAND, callback_location_keyword_search), |
59 | CallbackQueryHandler(callback_skip_location_keyword), | 59 | CallbackQueryHandler(callback_skip_location_keyword), |
60 | ], | 60 | ], |
61 | LOCATION_CONFIRMATION: [ | 61 | LOCATION_CONFIRMATION: [ |
62 | CallbackQueryHandler(callback_location_confirmation), | 62 | CallbackQueryHandler(callback_location_confirmation), |
63 | MessageHandler(filters.TEXT, callback_manual_location) | 63 | MessageHandler(filters.TEXT & ~filters.COMMAND, callback_manual_location) |
64 | ], | 64 | ], |
65 | ADD_COMMENT: [ | 65 | ADD_COMMENT: [ |
66 | MessageHandler(filters.TEXT, callback_add_comment), | 66 | MessageHandler(filters.TEXT & ~filters.COMMAND, callback_add_comment), |
67 | CallbackQueryHandler(callback_skip_comment), | 67 | CallbackQueryHandler(callback_skip_comment), |
68 | ], | 68 | ], |
69 | ADD_MEDIA: [MessageHandler(filters.PHOTO, callback_add_media), | 69 | 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 | |||
43 | context.user_data["status_id"] = status["id"] | 43 | context.user_data["status_id"] = status["id"] |
44 | context.user_data["status_content"] = content | 44 | context.user_data["status_content"] = content |
45 | 45 | ||
46 | print("status_id", context.user_data["status_id"]) | ||
47 | |||
48 | await update.message.reply_text( | 46 | await update.message.reply_text( |
49 | text=f"Selected place: {poi['name']}, \nPosted to Mastodon: {status['url']}", | 47 | text=f"Selected place: {poi['name']}, \nPosted to Mastodon: {status['url']}", |
50 | parse_mode=ParseMode.MARKDOWN, | 48 | parse_mode=ParseMode.MARKDOWN, |
@@ -78,8 +76,6 @@ async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT | |||
78 | context.user_data["status_id"] = status["id"] | 76 | context.user_data["status_id"] = status["id"] |
79 | context.user_data["status_content"] = content | 77 | context.user_data["status_content"] = content |
80 | 78 | ||
81 | print("status_id", context.user_data["status_id"]) | ||
82 | |||
83 | await update.message.reply_text( | 79 | await update.message.reply_text( |
84 | text=f"Manually selected place: {loc}, \nPosted to Mastodon: {status['url']}", | 80 | text=f"Manually selected place: {loc}, \nPosted to Mastodon: {status['url']}", |
85 | parse_mode=ParseMode.MARKDOWN, | 81 | parse_mode=ParseMode.MARKDOWN, |
@@ -109,8 +105,6 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D | |||
109 | context.user_data["status_id"] = status["id"] | 105 | context.user_data["status_id"] = status["id"] |
110 | context.user_data["status_content"] = content | 106 | context.user_data["status_content"] = content |
111 | 107 | ||
112 | print("status_id", context.user_data["status_id"]) | ||
113 | |||
114 | await query.message.reply_text( | 108 | await query.message.reply_text( |
115 | text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", | 109 | text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", |
116 | parse_mode=ParseMode.MARKDOWN, | 110 | parse_mode=ParseMode.MARKDOWN, |
@@ -125,12 +119,12 @@ async def callback_location_confirmation(update: Update, context: ContextTypes.D | |||
125 | async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 119 | async def callback_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
126 | await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_LOCATION_KEYWORD]) | 120 | await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_LOCATION_KEYWORD]) |
127 | 121 | ||
128 | location_search = update.effective_message.text | 122 | location_search_keyword = update.effective_message.text |
129 | latitude = context.user_data["latitude"] | 123 | latitude = context.user_data["latitude"] |
130 | longitude = context.user_data["longitude"] | 124 | longitude = context.user_data["longitude"] |
131 | 125 | ||
132 | keyboard = [] | 126 | keyboard = [] |
133 | poi_result = query_poi(location_search, latitude, longitude) | 127 | poi_result = query_poi(location_search_keyword, latitude, longitude) |
134 | if len(poi_result) == 0: | 128 | if len(poi_result) == 0: |
135 | poi_result = query_poi("", latitude, longitude) | 129 | poi_result = query_poi("", latitude, longitude) |
136 | 130 | ||
@@ -140,13 +134,13 @@ async def callback_location_keyword_search(update: Update, context: ContextTypes | |||
140 | ]) | 134 | ]) |
141 | 135 | ||
142 | if len(keyboard) == 0: | 136 | if len(keyboard) == 0: |
143 | await update.message.reply_text(PROMPT_NO_NEARBY_POI) | 137 | msg = await update.message.reply_text(PROMPT_NO_NEARBY_POI) |
144 | return LOCATION_CONFIRMATION | ||
145 | else: | 138 | else: |
146 | reply_markup = InlineKeyboardMarkup(keyboard) | 139 | reply_markup = InlineKeyboardMarkup(keyboard) |
147 | context.user_data["location_search"] = location_search | 140 | context.user_data["location_search"] = location_search_keyword |
148 | await update.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup) | 141 | msg = await update.message.reply_text(PROMPT_CHOOSE_POI_FROM_LIST, reply_markup=reply_markup) |
149 | 142 | ||
143 | context.user_data[PROMPT_CHOOSE_POI_FROM_LIST] = msg.message_id | ||
150 | return LOCATION_CONFIRMATION | 144 | return LOCATION_CONFIRMATION |
151 | 145 | ||
152 | 146 | ||
@@ -188,7 +182,9 @@ async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYP | |||
188 | 182 | ||
189 | async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 183 | async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
190 | await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_ADD_COMMENT]) | 184 | await context.bot.delete_message(update.effective_chat.id, context.user_data[PROMPT_ADD_COMMENT]) |
191 | prompt_attach_photo_msg = await update.message.reply_text(PROMPT_ADD_MEDIA, reply_markup=INLINE_SKIP_MENU) | 185 | prompt_attach_photo_msg = await context.bot.send_message(chat_id=update.effective_chat.id, |
186 | text=PROMPT_ADD_MEDIA, | ||
187 | reply_markup=INLINE_SKIP_MENU) | ||
192 | context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id | 188 | context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id |
193 | return ADD_MEDIA | 189 | return ADD_MEDIA |
194 | 190 | ||
@@ -1,5 +1,6 @@ | |||
1 | from telegram import Update | 1 | from telegram import Update |
2 | from telegram.constants import ParseMode | 2 | from telegram.constants import ParseMode |
3 | from telegram.error import BadRequest | ||
3 | from telegram.ext import ContextTypes, ConversationHandler | 4 | from telegram.ext import ContextTypes, ConversationHandler |
4 | 5 | ||
5 | from config import * | 6 | from config import * |
@@ -24,5 +25,17 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No | |||
24 | 25 | ||
25 | 26 | ||
26 | async def cancel_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: | 27 | async def cancel_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
28 | for prompt in [PROMPT_LOCATION_KEYWORD, PROMPT_CHOOSE_POI_FROM_LIST, PROMPT_ADD_COMMENT, PROMPT_ADD_MEDIA]: | ||
29 | try: | ||
30 | if context.user_data.get(prompt): | ||
31 | await context.bot.delete_message(chat_id=update.message.chat_id, | ||
32 | message_id=context.user_data[prompt]) | ||
33 | except BadRequest as e: | ||
34 | if "not found" in str(e.message): | ||
35 | pass | ||
36 | except Exception as e: | ||
37 | print(e) | ||
38 | |||
27 | await update.message.reply_text(text=PROMPT_CANCELED, reply_markup=MAIN_MENU) | 39 | await update.message.reply_text(text=PROMPT_CANCELED, reply_markup=MAIN_MENU) |
40 | context.user_data.clear() | ||
28 | return ConversationHandler.END | 41 | return ConversationHandler.END |