From 1046a4dd835468ee4855d70ca0a5e6dc6f79cb32 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Tue, 21 Feb 2023 01:43:24 -0800 Subject: add allow_reentry=True to fix job queue exec without return to conversation handler callbacks --- bot.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'bot.py') diff --git a/bot.py b/bot.py index 4402d08..a0a00b3 100644 --- a/bot.py +++ b/bot.py @@ -179,6 +179,9 @@ async def media_group_sender(context: CallbackContext): media_id = [] chat_id = context.job.data[0].get("chat_id") for msg_dict in context.job.data: + if len(media_id) >= 4: + print("Cannot attach more than 4 photos") + break file = await context.bot.get_file(msg_dict.get("media_id")) img = io.BytesIO() await file.download_to_memory(img) @@ -218,8 +221,6 @@ async def photo(update: Update, context: CallbackContext): "content": status_content, "chat_id": message.chat_id, } - global jobname - jobname = str(message.media_group_id) jobs = context.job_queue.get_jobs_by_name(str(message.media_group_id)) if jobs: jobs[0].data.append(msg_dict) @@ -234,16 +235,25 @@ async def photo(update: Update, context: CallbackContext): img = io.BytesIO() await file.download_to_memory(img) img.seek(0) - context.user_data["photo"].append(img.read()) - await process_location(update, context) + media = mastodon_client.media_post(img.read(), mime_type="image/jpeg") + mastodon_client.status_update( + status=status_content, + id=status_id, + media_ids=media["id"]) + + await update.message.reply_text(text="Done", + reply_markup=telegram.ReplyKeyboardMarkup([ + [telegram.KeyboardButton(text="Check in", request_location=True)], + [telegram.KeyboardButton(text="Setting")]])) async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE): print(context.user_data) - reply_markup = telegram.ReplyKeyboardRemove() await update.message.reply_text( - text="Done.", reply_markup=reply_markup + text="Done.", reply_markup=telegram.ReplyKeyboardMarkup([ + [telegram.KeyboardButton(text="Check in", request_location=True)], + [telegram.KeyboardButton(text="Setting")]]) ) return ConversationHandler.END @@ -254,6 +264,7 @@ def main() -> None: conv_handler = ConversationHandler( entry_points=[ CommandHandler("start", start), + MessageHandler(filters.LOCATION, checkin), ], states={ LOCATION: [ @@ -265,6 +276,7 @@ def main() -> None: }, fallbacks=[CommandHandler("cancel", cancel)], per_message=False, + allow_reentry=True, ) application.add_handler(conv_handler) -- cgit v1.2.3