【问题标题】:Telegram Bot API - Upload document with filenameTelegram Bot API - 上传带有文件名的文档
【发布时间】:2018-10-09 19:35:13
【问题描述】:

如何在 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"}

有人知道如何为文件设置文件名吗?

【问题讨论】:

  • 为什么需要自定义文档名称?
  • 这是关于 openvpn 配置文件的。它们应该具有与应用关联的结尾 .ovpn,并且不必由用户手动重命名。

标签: python telegram telegram-bot


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 2021-06-28
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多