我想通过http电报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'}
我应该怎么做才能发送文件?
2条答案
按热度按时间egmofgnx1#
这里的问题是 requests 库的错误用法,如果你发送
multipart/form-data
和文件,你应该使用参数files
。例如
文档的链接-http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
tag5nh1u2#
我决定在上面的答案之外再写一个,因为它对我有帮助,但是因为我是个新手,所以我花了一些时间来理解它。所以我希望我的代码示例能帮助那些试图解决同样问题的人保存时间:
答复示例:
结果,bot通过我们指定的
chat_id
将文件发送给用户另请注意:要发送的文件必须与执行此代码的
.py
文件位于同一位置