1
votes

I am having a real headache with the way of sending a pdf file to a Telegram Bot. Apparently I am following the documentation but never get it sent. I am using the url: https://api.telegram.org/botBOTID/sendDocument?chat_id=CHATID&document=/home/lix/Downloads/2.pdf

It is a pdf file storaged locally, but I think it is just the way I am presenting it. The error getting is: {"ok":false,"error_code":400,"description":"Bad Request: URL host is empty"} Does anybody knows how to send a pdf local file? Many thanks

1

1 Answers

2
votes

You should send a POST request, with the PDF as a payload, using the Python Requests library, your code should look something like this:

import requests

# Url with bot token + user id
url = "https://api.telegram.org/bot<MY-BOT-TOKEN>/sendDocument?chat_id=<MY_CHAT_ID>"

# Create payload with PDF file
payload = {}
files = [
    ('document', open('/home/lix/Downloads/2.pdf','rb'))
]
headers= {}

# Request
response = requests.request("POST", url, headers=headers, data = payload, files = files)

# Log reponse as UTF-8
print(response.text.encode('utf8'))