Search code examples
pythonpython-requeststelegram

Telegram bot error Bad Request: wrong remote file identifier specified: Wrong character in the string


I have a .ts file and I want send this file to telegram.

My code:

from requests import post

data = open("name.ts", "rb").read()

results = post(
    f"https://api.telegram.org/bot{telegram_token}/sendDocument?chat_id={chat_id}&document={data}")

and:

from requests import post

data = open("name.ts", "rb").read()

results = post(
    f"https://api.telegram.org/bot{telegram_token}/sendVideo?chat_id={chat_id}&video={data}")

result:

{'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong remote file identifier specified: Wrong character in the string'}

please help me


Solution

  • Passing the document should be done with the files param on requests.post. Also, you don't need the read() on the open.

    Use the following snippet:

    data = open("/private/tmp/test.ts", "rb")
    
    results = post(
        f"https://api.telegram.org/bot{TOKEN}/sendDocument?chat_id={chat_id}", files={ 'document': data })