skip to Main Content

I’m using Postman app to interact with a Telegram bot api. I’ve sent photos using the sendPhoto() method, like this:

https://api.telegram.org/botToken/sendPhoto?chat_id=00000000&photo=AgAC***rgehrehrhrn

But I don’t understand the sendMediaGroup() method. Can someone post an example how to compose the https string to send two photos?
Thanks

2

Answers


  1. You need to send a POST request at the url https://api.telegram.org/botToken/sendPhoto with a JSON body. You are using the url to specify all the parameters of the request but urls are only 2000 characters long. The body of a POST request, instead, has no limits in terms of size. The JSON body should look something like this:

    {
      "chat_id": 777000,
      "media": [
        {
          "type": "photo",
          "media": "https://example.com/first_photo_url.png",
          "caption": "an optional description of the first photo",
          "parse_mode": "optional (you can delete this parameter) the parse mode of the caption"
        },
        {
          "type": "photo",
          "media": "https://example.com/fsecond_photo_url.png",
          "caption": "an optional description of the second photo",
          "parse_mode": "optional (you can delete this parameter) the parse mode of the caption"
        }
      ],
    }
    

    For more info see:

    how to send JSON (raw) data with Postman

    and

    sendMediaGroup Telegram API’s method.

    Login or Signup to reply.
  2. You must send JSON as a string, or serialized JSON, to Telegram API. The format is as same as @GioIacca9’s answer.

    Note: only the caption in the first image will be showen.

    Have a try this Python code.

    def send_photos(api_key, chat_id, photo_paths):
      params = {
        'chat_id': chat_id,
        'media': [],
      }
      for path in photo_paths:
          params['media'].append({'type': 'photo', 'media': path})
      params['media'] = json.dumps(params['media'])
      url = f'https://api.telegram.org/bot{api_key}/sendMediaGroup' 
      return requests.post(url, data=params)
    
    
    if __name__ == '__main__':
      send_photos('your_key', '@yourchannel', ['http://your.image.one', 'http://your.image.two'])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search