JuliB
JuliB

Reputation: 73

Telegram Bot API - Upload document with filename

How is it possible to send a document in telegram in python with an other filename that "document"? I'm using the Python3. "doc" is a clear text i want to send as txt file.

url = baseurl + token + '/sendDocument'
dict = {'chat_id':chat_id}
r = requests.post(url, params=dict, files={'document':doc})

The received file is named "document", without a file extension. When I rewrite the files to

files={'document.txt':doc}

the Telegram replies

{"ok":false,"error_code":400,"description":"Bad Request: there is no document in the request"}

Does anyone know how to set a file name for the file?

Upvotes: 2

Views: 4059

Answers (2)

Vladimir Sviridov
Vladimir Sviridov

Reputation: 21

You may try something like this:

url = f'https://api.telegram.org/bot{settings.TG_TOKEN}/sendDocument'
response = requests.post(url, data=data, files = {'document': ('filename.html', file.read())})

Upvotes: 2

Ivan Vinogradov
Ivan Vinogradov

Reputation: 4473

According to the API page, you cannot do that. You must use predefined names for such types of API methods (document, photo, audio, etc.).

And even if you could - you won't be able to use your custom name to find your files because Telegram bot API uses its own file_id identifier for this purpose.

As a workaround you may store file_idyour_custom_document.txt relation on your backend and use file_id to communicate with Telegram servers.

Upvotes: 3

Related Questions