From be2bd723e314cf090890961756f019ade7e86b3a Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 22 Feb 2023 12:22:51 -0800 Subject: remove some redundant code blocks --- bot.py | 4 +- callback.py | 177 ++++++++++++++++++++++++++++++------------------------------ 2 files changed, 91 insertions(+), 90 deletions(-) diff --git a/bot.py b/bot.py index 02182cd..e73fe1b 100644 --- a/bot.py +++ b/bot.py @@ -17,7 +17,7 @@ from callback import ( callback_manual_location, callback_location_confirmation, callback_location_keyword_search, - callback_skip_location_keyword, + callback_skip_location_keyword_search, callback_add_comment, callback_skip_comment, callback_add_media @@ -56,7 +56,7 @@ def main() -> None: ], LOCATION_SEARCH_KEYWORD: [ MessageHandler(filters.TEXT & ~filters.COMMAND, callback_location_keyword_search), - CallbackQueryHandler(callback_skip_location_keyword), + CallbackQueryHandler(callback_skip_location_keyword_search), ], LOCATION_CONFIRMATION: [ CallbackQueryHandler(callback_location_confirmation), diff --git a/callback.py b/callback.py index 9b82d78..38b0456 100644 --- a/callback.py +++ b/callback.py @@ -27,6 +27,37 @@ def generate_toot_text(poi_name, poi_locality, poi_region, poi_lat, poi_lon): return f"I'm at {poi_name}, {osm_url}" +async def get_img_file_bytes(telegram_media_file): + img = io.BytesIO() + await telegram_media_file.download_to_memory(img) + img.seek(0) + + return img.read() + + +async def process_media_group(context: CallbackContext): + context.job.data = cast(List[MsgDict], context.job.data) + + media_id = [] + chat_id = context.job.data[0].get("chat_id") + for msg_dict in context.job.data: + if len(media_id) >= 4: + await context.bot.send_message(chat_id=chat_id, text=PROMPT_MAX_PHOTO_REACHED, reply_markup=MAIN_MENU) + return + + file = await context.bot.get_file(msg_dict.get("media_id")) + img = await get_img_file_bytes(file) + media = mastodon_client.media_post(img, mime_type="image/jpeg") + media_id.append(media["id"]) + + mastodon_client.status_update( + status=msg_dict.get("content"), + id=msg_dict.get("status_id"), + media_ids=media_id) + + await context.bot.send_message(chat_id=chat_id, text=PROMPT_DONE, reply_markup=MAIN_MENU) + + async def callback_location_sharing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: if update.message.venue is not None: context.user_data["fsq_id"] = update.message.venue.foursquare_id @@ -59,27 +90,32 @@ async def callback_location_sharing(update: Update, context: ContextTypes.DEFAUL return LOCATION_SEARCH_KEYWORD -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.get(PROMPT_LOCATION_KEYWORD)) +async def _process_location_search(keyword, lat, lon) -> list: + keyboard = [] + poi_result = [] - location_search_keyword = update.effective_message.text - latitude = context.user_data.get("latitude") - longitude = context.user_data.get("longitude") + if keyword: + poi_result = query_poi(keyword, lat, lon) - keyboard = [] - poi_result = query_poi(location_search_keyword, latitude, longitude) if len(poi_result) == 0: - poi_result = query_poi("", latitude, longitude) + poi_result = query_poi("", lat, lon) for poi in poi_result: keyboard.append([ InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]), ]) + return keyboard + + +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.get(PROMPT_LOCATION_KEYWORD)) + key = update.effective_message.text + + keyboard = await _process_location_search(key, context.user_data.get("latitude"), context.user_data.get("longitude")) if len(keyboard) == 0: msg = await update.message.reply_text(PROMPT_WAIT_LOCATION_CONFIRMATION_NO_NEARBY_POI) else: - context.user_data["location_search"] = location_search_keyword msg = await update.message.reply_text(PROMPT_WAIT_LOCATION_CONFIRMATION, reply_markup=InlineKeyboardMarkup(keyboard)) @@ -87,121 +123,89 @@ async def callback_location_keyword_search(update: Update, context: ContextTypes return LOCATION_CONFIRMATION -async def callback_skip_location_keyword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: +async def callback_skip_location_keyword_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: query = update.callback_query await query.answer() - await query.message.delete() - latitude = context.user_data.get("latitude") - longitude = context.user_data.get("longitude") - - keyboard = [] - - for poi in query_poi("", latitude, longitude): - keyboard.append([ - InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]), - ]) + keyboard = await _process_location_search("", context.user_data.get("latitude"), context.user_data.get("longitude")) await query.message.reply_text(PROMPT_WAIT_LOCATION_CONFIRMATION, reply_markup=InlineKeyboardMarkup(keyboard)) return LOCATION_CONFIRMATION -async def callback_location_confirmation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: - query = update.callback_query - await query.answer() - - context.user_data["fsq_id"] = query.data - await query.delete_message() +async def _process_location_selection(context: ContextTypes.DEFAULT_TYPE) -> int: + poi_name = context.user_data.get("poi_name") + if context.user_data.get("fsq_id") is not None: + poi = get_loc(context.user_data.get("fsq_id")) + content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) + else: + content = generate_toot_text(poi_name, "", "", context.user_data.get("latitude"), context.user_data.get("longitude")) - poi = get_loc(context.user_data.get("fsq_id")) - content = generate_toot_text(poi["name"], poi["locality"], poi["region"], poi["latitude"], poi["longitude"]) status = mastodon_client.status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) context.user_data[KEY_TOOT_STATUS_ID] = status["id"] context.user_data[KEY_TOOT_STATUS_CONTENT] = content - await query.message.reply_text( - text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", - parse_mode=ParseMode.MARKDOWN, - ) + await context.bot.send_message(chat_id=context.user_data.get("chat_id"), + text=f"Selected place: {poi_name}, \nPosted to Mastodon: {status['url']}", + parse_mode=ParseMode.MARKDOWN) + + msg = await context.bot.send_message(chat_id=context.user_data.get("chat_id"), + text=PROMPT_ADD_COMMENT, + reply_markup=INLINE_SKIP_MENU) - msg = await query.message.reply_text(PROMPT_ADD_COMMENT, reply_markup=INLINE_SKIP_MENU) context.user_data[PROMPT_ADD_COMMENT] = msg.message_id return ADD_COMMENT -async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: - manual_poi_name = update.effective_message.text - content = generate_toot_text(manual_poi_name, "", "", context.user_data.get("latitude"), - context.user_data.get("longitude")) - status = mastodon_client.status_post(content, visibility=DEFAULT_TOOT_VISIBILITY, media_ids=[]) +async def callback_location_confirmation(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + query = update.callback_query + await query.answer() + context.user_data["fsq_id"] = query.data + await query.delete_message() - context.user_data[KEY_TOOT_STATUS_ID] = status["id"] - context.user_data[KEY_TOOT_STATUS_CONTENT] = content + context.user_data["chat_id"] = update.effective_chat.id - await update.message.reply_text( - text=f"Manually selected place: {manual_poi_name}, \nPosted to Mastodon: {status['url']}", - parse_mode=ParseMode.MARKDOWN, - ) + return await _process_location_selection(context) - msg = await update.message.reply_text(PROMPT_ADD_COMMENT, reply_markup=INLINE_SKIP_MENU) - context.user_data[PROMPT_ADD_COMMENT] = msg.message_id - return ADD_COMMENT +async def callback_manual_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + context.user_data["poi_name"] = update.effective_message.text + context.user_data["chat_id"] = update.effective_chat.id + + return await _process_location_selection(context) + + +async def _process_comment(context: ContextTypes.DEFAULT_TYPE) -> int: + msg = await context.bot.send_message(chat_id=context.user_data.get("chat_id"), + text=PROMPT_ADD_MEDIA, + reply_markup=INLINE_SKIP_MENU) + context.user_data[PROMPT_ADD_MEDIA] = msg.message_id + return ADD_MEDIA async def callback_add_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + context.user_data["chat_id"] = update.effective_chat.id await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) - comment = update.effective_message.text + comment = update.effective_message.text mastodon_client.status_update(id=context.user_data.get(KEY_TOOT_STATUS_ID), status=f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT)) - context.user_data[KEY_TOOT_STATUS_CONTENT] = f"{comment} " + context.user_data.get(KEY_TOOT_STATUS_CONTENT) - prompt_attach_photo_msg = await update.message.reply_text(PROMPT_ADD_MEDIA, reply_markup=INLINE_SKIP_MENU) - context.user_data[PROMPT_ADD_MEDIA] = prompt_attach_photo_msg.message_id - return ADD_MEDIA + return await _process_comment(context) async def callback_skip_comment(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + context.user_data["chat_id"] = update.effective_chat.id await context.bot.delete_message(update.effective_chat.id, context.user_data.get(PROMPT_ADD_COMMENT)) - 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 - -async def callback_add_media(update: Update, context: CallbackContext): - async def process_media_group(context: CallbackContext): - context.job.data = cast(List[MsgDict], context.job.data) - - _media_id = [] - chat_id = context.job.data[0].get("chat_id") - for _msg_dict in context.job.data: - if len(_media_id) >= 4: - await context.bot.send_message(chat_id=chat_id, text=PROMPT_MAX_PHOTO_REACHED, reply_markup=MAIN_MENU) - return - - _file = await context.bot.get_file(_msg_dict.get("media_id")) - _img = io.BytesIO() - await _file.download_to_memory(_img) - - _img.seek(0) + return await _process_comment(context) - _media = mastodon_client.media_post(_img.read(), mime_type="image/jpeg") - _media_id.append(_media["id"]) - - mastodon_client.status_update( - status=_msg_dict.get("content"), - id=_msg_dict.get("status_id"), - media_ids=_media_id) - - await context.bot.send_message(chat_id=chat_id, text=PROMPT_DONE, reply_markup=MAIN_MENU) +async def callback_add_media(update: Update, context: CallbackContext): await update.message.reply_chat_action(ChatAction.TYPING) try: @@ -233,11 +237,8 @@ async def callback_add_media(update: Update, context: CallbackContext): data=[msg_dict], name=str(message.media_group_id)) else: file = await update.message.effective_attachment[-1].get_file() - img = io.BytesIO() - await file.download_to_memory(img) - img.seek(0) - - media = mastodon_client.media_post(img.read(), mime_type="image/jpeg") + img = await get_img_file_bytes(file) + media = mastodon_client.media_post(img, mime_type="image/jpeg") mastodon_client.status_update(status=status_content, id=status_id, media_ids=media["id"]) await update.message.reply_text(text=PROMPT_DONE, reply_markup=MAIN_MENU) -- cgit v1.2.3