【问题标题】:How to send file to Telegram bot via HTTP?如何通过 HTTP 将文件发送到 Telegram 机器人?
【发布时间】:2019-02-21 14:27:56
【问题描述】:

我想通过 http Telegram API 发送文件并尝试以下代码:

def send_media(self, chat_id, doc):
    method = 'sendDocument'
    params = {'chat_id': chat_id, 'document': doc}
    resp = requests.post(self.api_url + method, params)
    return resp
 document = open('table.csv', 'rb')
 doc = InputFile(document)
 bot.send_media(last_chat_id, doc).json()
 document.close()

根据要求有这样的错误:

 {'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong URL host'}

我应该怎么做才能发送文件?

【问题讨论】:

    标签: python python-3.x python-requests telegram


    【解决方案1】:

    这里的问题是 requests 库的错误使用,如果你发送multipart/form-data 和文件你应该使用参数files

    例如

    requests.post(self.api_url + method, data={'chat_id': chat_id}, files={'document': document})
    

    文档链接 - http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 2019-08-23
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 2021-05-09
      相关资源
      最近更新 更多