'bot.send_media_group fails (python-telegram-bot)
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)
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Salavat Khalikov |
