1

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

bitbar
  • 121
  • 1
  • 10

2 Answers2

2

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.

GioIacca9
  • 406
  • 4
  • 8
0

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'])
武状元 Woa
  • 690
  • 3
  • 10
  • 24