0

I'm trying to upload the outcomes of my code (they are images) in a group with a Telegram bot.

I've tried:

import requests
import numpy as np

paths=np.array([])
for z in range(8):
    paths=np.append(paths,str(f"path\\{z}.jpg"))

url="https://api.telegram.org/bot<<my-bot-http-token>>/senddocument?chat_id=<<chat-id>>"
for path in paths:
    files={'document':open(path,'rb')}
    resp=requests.post(url,files=files)

It works but it sends the files separately and I want to group them then send.

0stone0
  • 34,288
  • 4
  • 39
  • 64

1 Answers1

1

You're using the sendDocument method, however, for grouped files/image, you'll need sendMediaGroup with an array of media


Here a minimum reproducable example to send a group of local photo's:

import json
import requests

chat_id = 1234567
TOKEN = '85916...the-rest-of-your-token'

data = {
    "chat_id": chat_id,
    "media": json.dumps([
        {"type": "photo", "media": "attach://photo1.png"},
        {"type": "photo", "media": "attach://photo2.png"}
    ])
}

files = {
    "photo1.png" : open("./photo1.png", 'rb'),
    "photo2.png" : open("./photo2.png", 'rb')
}

temp = requests.post("https://api.telegram.org/bot" + TOKEN + "/sendMediaGroup", data=data, files=files)

print(temp.json())

Result screenshot:

enter image description here

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Thanks, but can I send them as file group not media group? Because people who I work for, need those images in files not media. –  Mar 15 '23 at 17:26
  • 1
    Yes, use a [InputMediaDocument](https://core.telegram.org/bots/api#inputmediadocument) in stead of the [InputMediaPhoto](https://core.telegram.org/bots/api#inputmediaphoto). Please see the Docs for more info about the different types you can send. – 0stone0 Mar 15 '23 at 17:29
  • can you take a look at this? https://stackoverflow.com/questions/75785179/how-to-send-an-image-group-with-telethon-in-telegram-in-python –  Mar 19 '23 at 22:09