File size: 51.2 KB
Trying to send:
>>> send_img_url = 'https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXX/sendPhoto'
>>> img_name = 'C:/Users/Administrator/Downloads/WhatsApp Image 2019-05-30 at 20.54.40.jpeg'
>>> r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': open(img_name, 'rb')})
>>> r
<Response [413]>
>>> r.reason
'Request Entity Too Large'
>>> r.content
b''
>>>
Also i try some another requests like:
photo = open(('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg').encode('utf-8'), 'rb')
r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': photo})
and:
with io.open('C:/Users/Administrator/Downloads/WhatsAppImage.jpeg', encoding='utf-8', errors='ignore') as f:
r = requests.post(send_img_url, data={'chat_id': '-351543550', 'photo': f})
Last option give me next error:
>>> r
<Response [400]>
>>> r.reason
'Bad Request'
3
Answers
You’re probably doing it wrong.
As Bot API docs says:
In requests lib, by using
data=
keyword argument, you’re sending payload usingform-encoded
type, notmultipart/form-data
.Try to make your request like that:
P.S.: you can also send
chat_id
asform-encoded
param by usingIn my case the solution proposed by Ivan Vinogradov did not work due to Cyrillic filenames. Changing the paths to Latin fixed it.
Using a Local Bot API Server you can send a large file up to 2GB.
GitHub Source Code:
Official Documentation
You can
build and install
this to your server by following the instructions on this link https://tdlib.github.io/telegram-bot-api/build.htmlbasic setup :
./telegram-bot-api --api-id=<your-app-id> --api-hash=<your-app-hash> --verbosity=20
http://127.0.0.1:8081/bot<token>/METHOD_NAME
reference: https://core.telegram.org/bots/apiExample Code with: