How to send file to Telegram bot via HTTP?
Question:
I want to send file via http Telegram API and try this code :
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()
And have such error on request:
{'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong URL host'}
What should i do to send a file?
Answers:
The problem here is a wrong usage of requests library, if you’re sending multipart/form-data
and files you should use parameter files
.
E.g.
requests.post(self.api_url + method, data={'chat_id': chat_id}, files={'document': document})
The link to the documentation – http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
Decided to write it in addition to the top answer, as it helped me, but as I’m noob it took some time for me to understand. So I hope my example of code will help to save the time for those, who will try to solve the same issue:
document = open("<filename, like: 'test.txt'>", "rb")
url = f"https://api.telegram.org/bot{YOUR_BOT_TOKEN}/sendDocument"
response = requests.post(url, data={'chat_id': chat_id}, files={'document': document})
# part below, just to make human readable response for such noobies as I
content = response.content.decode("utf8")
js = json.loads(content)
print(js)
Example of response:
{'ok': True, 'result': {'message_id': 865, 'from': {'id': 111111, 'is_bot': True, 'first_name': 'BotName', 'username': 'LongBotName'}, 'chat': {'id': 111111, 'first_name': 'Name', 'last_name': 'Surname', 'username': 'user', 'type': 'private'}, 'date': 1672056973, 'document': {'file_name': 'test.txt', 'mime_type': 'text/plain', 'file_id': 'BQACAgIAAxkDAAIDYWOpkI1wkcuXMTy8hlob6Vr46UFxAALLJAACG1pJSX0tj7Ubt_sTLAQ', 'file_unique_id': 'AgADyyQAAhtaSUk', 'file_size': 239}}}
As a result bot sent our file to the user by chat_id
that we’ve specified
Also important note: file, that you want to send must be in the same location as .py
file that executes this code
I want to send file via http Telegram API and try this code :
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()
And have such error on request:
{'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong URL host'}
What should i do to send a file?
The problem here is a wrong usage of requests library, if you’re sending multipart/form-data
and files you should use parameter files
.
E.g.
requests.post(self.api_url + method, data={'chat_id': chat_id}, files={'document': document})
The link to the documentation – http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
Decided to write it in addition to the top answer, as it helped me, but as I’m noob it took some time for me to understand. So I hope my example of code will help to save the time for those, who will try to solve the same issue:
document = open("<filename, like: 'test.txt'>", "rb")
url = f"https://api.telegram.org/bot{YOUR_BOT_TOKEN}/sendDocument"
response = requests.post(url, data={'chat_id': chat_id}, files={'document': document})
# part below, just to make human readable response for such noobies as I
content = response.content.decode("utf8")
js = json.loads(content)
print(js)
Example of response:
{'ok': True, 'result': {'message_id': 865, 'from': {'id': 111111, 'is_bot': True, 'first_name': 'BotName', 'username': 'LongBotName'}, 'chat': {'id': 111111, 'first_name': 'Name', 'last_name': 'Surname', 'username': 'user', 'type': 'private'}, 'date': 1672056973, 'document': {'file_name': 'test.txt', 'mime_type': 'text/plain', 'file_id': 'BQACAgIAAxkDAAIDYWOpkI1wkcuXMTy8hlob6Vr46UFxAALLJAACG1pJSX0tj7Ubt_sTLAQ', 'file_unique_id': 'AgADyyQAAhtaSUk', 'file_size': 239}}}
As a result bot sent our file to the user by chat_id
that we’ve specified
Also important note: file, that you want to send must be in the same location as .py
file that executes this code