Search code examples
pythontelebot

Telebot Python - how to send photos group


I'm trying to send photos from disk with media group and pyTelegramBotAPI (latest version 4.10.0)

import telebot
from telegram import InputMediaPhoto
from telegram.constants import ParseMode
bot = telebot.TeleBot(API_KEY)
all_photos = []
IMAGE_FILE_PATH = "images/image.jpg"
current_image_file = open(IMAGE_FILE_PATH, 'rb')
all_photos.append(InputMediaPhoto(
           media = current_image_file,
           parse_mode = ParseMode.HTML,
           caption = "Sample <b>Text</b>"
))
all_photos.append(InputMediaPhoto(
           media = current_image_file,
           parse_mode = ParseMode.HTML,
           caption = "Sample <b>Text</b>"
))
bot.send_media_group(CHAT_ID, all_photos)

That code return error: *** telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: there are no messages to send

How it can be fixed?


Solution

  • from telegram import InputMediaPhoto
    

    InputMediaPhoto needs to be imported from telebot.types


    Here an example based on your code:

    import telebot
    from telebot.types import InputMediaPhoto
    
    CHAT_ID=123456
    API_KEY='85916......'
    
    bot = telebot.TeleBot(API_KEY)
    
    all_photos = []
    
    for pic in [ "./photo1.png", "./photo2.png" ]:
    
        current_image_file = open(pic, 'rb')
        mediaPhoto = InputMediaPhoto(current_image_file, "Sample <b>Text</b>")
    
        all_photos.append(mediaPhoto)
    
    bot.send_media_group(CHAT_ID, all_photos)
    

    Which results in this message send by the Bot: enter image description here