bot.send_media_group 失败(python-telegram-bot)

发布于 2025-01-20 05:59:54 字数 1288 浏览 0 评论 0原文

我需要一个用于机器人的处理程序,该处理程序将拍摄一组照片和视频,并将它们与文本一起发送给该组。 下面的代码仅发送分开的消息:

def photo(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id
    context.bot.send_media_group(chat_id=fwd_chat_id,
                                 media=[InputMediaPhoto(media=update.message.photo[0].file_id)])

photo_handler = MessageHandler(Filters.photo | Filters.video, photo)
dispatcher.add_handler(photo_handler)

但是,如果我尝试先收集所有照片,然后将它们发送给所有我收到的“ telegram.error.badrequest:消息文本为空”

def photo(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id, media_group
    if media_group == []:
        media_group.append(InputMediaPhoto(media=update.message.photo[0].file_id, caption='1'))
    else:
        media_group.append(InputMediaPhoto(media=update.message.photo[0].file_id))

photo_handler = MessageHandler(Filters.photo | Filters.video, photo)
dispatcher.add_handler(photo_handler)


def finish_photo_loading(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id, media_group
    context.bot.send_media_group(chat_id=fwd_chat_id,
                                 media=media_group)

finish_handler = CommandHandler('finish', finish_photo_loading)
dispatcher.add_handler(finish_handler)

I need a handler for bot that will take a group of photos and videos, and will send them in one message along with the text to the group.
The code below sends only separated messages:

def photo(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id
    context.bot.send_media_group(chat_id=fwd_chat_id,
                                 media=[InputMediaPhoto(media=update.message.photo[0].file_id)])

photo_handler = MessageHandler(Filters.photo | Filters.video, photo)
dispatcher.add_handler(photo_handler)

But if I try to collect all photos first and then send them all I get "telegram.error.BadRequest: Message text is empty"

def photo(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id, media_group
    if media_group == []:
        media_group.append(InputMediaPhoto(media=update.message.photo[0].file_id, caption='1'))
    else:
        media_group.append(InputMediaPhoto(media=update.message.photo[0].file_id))

photo_handler = MessageHandler(Filters.photo | Filters.video, photo)
dispatcher.add_handler(photo_handler)


def finish_photo_loading(update: Update, context: CallbackContext) -> None:
    global fwd_chat_id, media_group
    context.bot.send_media_group(chat_id=fwd_chat_id,
                                 media=media_group)

finish_handler = CommandHandler('finish', finish_photo_loading)
dispatcher.add_handler(finish_handler)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-01-27 05:59:54

因此,我的解决方案是与以下方式收集所有打磨照片:

context.bot_data[update.message.from_user.username + '_media']. \
append(InputMediaPhoto(media=update.message.photo[-1].file_id))

然后使用send_media_group:

context.bot.send_media_group(chat_id=update.effective_chat.id,
                         media=context.bot_data[update.message.from_user.username+'_media'])

获得命令/finish 之后

So my solution is to collect all sanded photos with:

context.bot_data[update.message.from_user.username + '_media']. \
append(InputMediaPhoto(media=update.message.photo[-1].file_id))

And then use send_media_group:

context.bot.send_media_group(chat_id=update.effective_chat.id,
                         media=context.bot_data[update.message.from_user.username+'_media'])

after getting command /finish

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文