使用sendDocument方法将文件发布到bot

时间:2020-09-21 09:56:57

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

我正在尝试使用python中的requests将html文档发送给机器人。

url = 'https://api.telegram.org/bot******/sendDocument'
response = requests.post( url = url, 
                          data = { 'chat_id' : chat_id, 
                                   'document': open('/home/user/page.html', 'rb'),
                          }
           )

我得到<Response [400]>。我遵循了这个link,并且能够使用curl而不是使用requests将html文档发布到漫游器。

我在这里做什么错了。

1 个答案:

答案 0 :(得分:1)

这是您使用电报bot API发送本地文档的方式

import requests

BOT_TOKEN = " ... "
CHAT_ID = " ... "

url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendDocument"


response = requests.post(url=url,
                         data={
                             'chat_id': CHAT_ID,
                             'document': 'attach://file',
                         },
                         files={
                             'file': open('./page.html', 'rb'),
                         }
                      )

print(response.json())