0

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

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
Ajesh
  • 43
  • 10
  • thanks its super helpful .. worked perfectly Btw If you don't mind above script send those 3 different files as three different messages it possible to send in one message – Ajesh Dec 19 '22 at 17:18
  • `ImportError: cannot import name 'Bot' from partially initialized module 'telegram' (most likely due to a circular import)` This error occurs when I tried to run this run in a vm @CallMeStag – Ajesh Dec 19 '22 at 18:44
  • What is the name of the file with your code? If it is telegram.py then rename it to something else. – elebur Dec 19 '22 at 19:03

1 Answers1

0

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

elebur
  • 465
  • 1
  • 5
  • 1
    Note that this snippet uses v13.15 or below of the `python-telegram-bot` library. It won't work with v20.x and above. – CallMeStag Dec 19 '22 at 16:09
  • thanks its super helpful .. worked perfectly Btw If you don't mind above script send those 3 different files as three different messages it possible to send in one message – Ajesh Dec 19 '22 at 16:59
  • @SirdLay, updated message. Also added the message from `CallMeStag`. – elebur Dec 19 '22 at 17:58