Telegram Bot API - 使用文件名上传文档

时间:2018-04-29 15:14:38

标签: python telegram telegram-bot

如何在python中发送带有“文档”的其他文件名的电报文件? 我正在使用Python3。 “doc”是我想要发送为txt文件的明文。

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

收到的文件名为“document”,没有文件扩展名。当我将文件重写为

files={'document.txt':doc}
电报回复

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

有谁知道如何设置文件的文件名?

1 个答案:

答案 0 :(得分:0)

根据API页面,您不能这样做。您必须为此类API方法使用预定义名称(documentphotoaudio等。)。

即使你可以 - 你也无法使用自定义名称来查找文件,因为Telegram bot API为此目的使用了自己的file_id标识符。

作为一种解决方法,您可以在后端存储file_id - your_custom_document.txt关系,并使用file_id与电报服务器进行通信。

相关问题