1

I am trying to send a html document to a bot using requests in python.

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

I get <Response [400]>. I followed this link and able to post html document to the bot using curl but not using requests.

What am I doing wrong here.

neelabalan
  • 27
  • 1
  • 4

1 Answers1

2

Here is how you send local document using telegram 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())
Tibebes. M
  • 6,940
  • 5
  • 15
  • 36