send multiple files to a telegram channel in a single message using bot

3.4k Views Asked by At

I'm trying to send 3 files to a chat, bt I'm not even able to send a single file. I tried this code

import requests

url = "https://api.telegram.org/botxxxxxxxxxxxxxxx/sendDocument"

payload = {
    "chat_id": "xxxxx",
    "document": "/students10.txt",
    "caption": "Total students in 10",
}
headers = {
    "accept": "application/json",
    "User-Agent": "Telegram Bot SDK - (https://github.com/irazasyed/telegram-bot-sdk)",
    "content-type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.text)

but it gives response as

{"ok":false,"error_code":400,"description":"Bad Request: wrong HTTP URL specified"}

I kind of need to send three files students10,students11,students12 but I don't know what to do and along with files a caption as Total students in 10: len of file students10 Total students in 11: len of file students11 Total students in 12: len of file students12

1

There are 1 best solutions below

3
elebur On BEST ANSWER

Are you allowed to use other libraries? With python-telegram-bot it would be as easy as:

Sending each file as a separate message

from telegram import Bot
# Put your token.
BOT_TOKEN = ""
# Put the destination chat id.
CHAT_ID = 123


def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "students10.txt",
        "students11.txt",
        "students12.txt"
    )

    for f in file_paths:
        with open(f, "rb") as fin:
            count = len(fin.readlines())
            # After the len(fin.readlines()) file's current position
            # will be at the end of the file. seek(0) sets the position
            # to the begining of the file so we can read it again during
            # sending.
            fin.seek(0)
            bot.send_document(
                CHAT_ID,
                document=fin,
                # Up to 1024 characters.
                # https://core.telegram.org/bots/api#inputmediadocument
                caption=f"Total students in {f}: {count}"
            )


if __name__ == "__main__":
    main()

Sending all files in one message

from telegram import Bot, InputMediaDocument

BOT_TOKEN = ""
CHAT_ID = 1234567890


def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "students10.txt",
        "students11.txt",
        "students12.txt"
    )
    # From 2 to 10 items in one media group
    # https://core.telegram.org/bots/api#sendmediagroup
    media_group = list()
    for f in file_paths:
        with open(f, "rb") as fin:
            # Up to 1024 characters.
            # https://core.telegram.org/bots/api#inputmediadocument
            caption = f"Total students in {f}: {len(fin.readlines())}\n"
            # After the len(fin.readlines()) file's current position
            # will be at the end of the file. seek(0) sets the position
            # to the begining of the file so we can read it again during
            # sending.
            fin.seek(0)
            media_group.append(InputMediaDocument(fin, caption=caption))

    bot.send_media_group(CHAT_ID, media=media_group)


if __name__ == "__main__":
    main()

Note that these snippets use v13.15 or below of the python-telegram-bot library. It won't work with v20.x and above. Thanks, @CallMeStag