如何通过HTTP将文件发送到Telegram机器人?

时间:2019-02-21 14:27:56

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

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

text <- "This funding would help us create a new website and hire talented people."

并应要求提供此类错误:

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()

我应该怎么发送文件?

1 个答案:

答案 0 :(得分: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