Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
JuliB Avatar asked Jan 28 '26 00:01

JuliB


1 Answers

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.

like image 182
Ivan Vinogradov Avatar answered Jan 30 '26 15:01

Ivan Vinogradov