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?
2
Answers
The problem here is a wrong usage of requests library, if you’re sending
multipart/form-data
and files you should use parameterfiles
.E.g.
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:
Example of response:
As a result bot sent our file to the user by
chat_id
that we’ve specifiedAlso important note: file, that you want to send must be in the same location as
.py
file that executes this code