【问题标题】:send binary with sendDocument in python-telegram-bot在 python-telegram-bot 中使用 sendDocument 发送二进制文件
【发布时间】:2017-12-05 04:35:33
【问题描述】:

这项工作:

mybot.sendDocument(chat_id=chatid, document=open('bla.pdf', rb'))

但如果我以前这样做过:

with open('bla.pdf', 'rb') as fp:
    b = fp.read()

我做不到:

mybot.sendDocument(chat_id=chatid, document=b)

错误是:

TypeError: 'bytes' 类型的对象不是 JSON 可序列化的

我使用 python 3.5.2 win 或 linux

感谢回答

【问题讨论】:

    标签: python-telegram-bot


    【解决方案1】:

    尝试只发送一个文件对象:

    mybot.sendDocument(chat_id=chatid, document=open('bla.pdf', 'rb'))
    

    【讨论】:

      【解决方案2】:

      对不起,我没有看到你的答案。

      我的问题是我想发送下载的文档,而不是磁盘上的文档。

      我是这样解决的:

      mybot.sendDocument(chat_id=chatid,document=io.BytesIO(self.downloaded_file))
      

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        • 2018-08-25
        • 2019-02-16
        • 1970-01-01
        相关资源
        最近更新 更多