From be5368af0ddc75ad04cfadcb235c6139232c6f76 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Wed, 22 Feb 2023 12:41:35 -0800 Subject: support image caption --- callback.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/callback.py b/callback.py index 38b0456..2c69fbd 100644 --- a/callback.py +++ b/callback.py @@ -40,20 +40,19 @@ async def process_media_group(context: CallbackContext): media_id = [] chat_id = context.job.data[0].get("chat_id") - for msg_dict in context.job.data: + for media_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")) + file = await context.bot.get_file(media_dict.get("media_id")) img = await get_img_file_bytes(file) - media = mastodon_client.media_post(img, mime_type="image/jpeg") + media = mastodon_client.media_post(img, description=media_dict.get("caption"), 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) + mastodon_client.status_update(id=media_dict.get("status_id"), + status=media_dict.get("content"), + media_ids=media_id) await context.bot.send_message(chat_id=chat_id, text=PROMPT_DONE, reply_markup=MAIN_MENU) @@ -222,7 +221,7 @@ async def callback_add_media(update: Update, context: CallbackContext): context.user_data["media"] = [] if message.media_group_id: media_id = message.photo[-1].file_id if message.photo else message.effective_attachment.file_id - msg_dict = { + group_media = { "media_id": media_id, "caption": message.caption_html, "status_id": status_id, @@ -231,14 +230,18 @@ async def callback_add_media(update: Update, context: CallbackContext): } jobs = context.job_queue.get_jobs_by_name(str(message.media_group_id)) if jobs: - jobs[0].data.append(msg_dict) + jobs[0].data.append(group_media) else: - context.job_queue.run_once(callback=process_media_group, when=MEDIA_GROUP_TIMEOUT, - data=[msg_dict], name=str(message.media_group_id)) + context.job_queue.run_once(callback=process_media_group, + when=MEDIA_GROUP_TIMEOUT, + data=[group_media], + name=str(message.media_group_id)) else: file = await update.message.effective_attachment[-1].get_file() img = await get_img_file_bytes(file) - media = mastodon_client.media_post(img, mime_type="image/jpeg") + media = mastodon_client.media_post(img, + description=message.caption_html, + 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