N
N
nzarchii602020-05-02 12:22:48
Python
nzarchii60, 2020-05-02 12:22:48

Sending photo with caption option telegram api?

I try to fasten to such code, the description of a file.
caption (str, optional) msg ​​= {'caption' : 'hello'}

url = «https://api.telegram.org/bot<token>/sendPhoto»;
files = {‘photo’: open(‘avatar1459.jpg’, ‘rb’)}
data = {‘chat_id’ : «-100134»}
msg = {‘caption’ : ‘hello’}
r= requests.post(url, files=files, data=data, msg=msg)
print(r.json())


Without caption, the shipment is carried out.
The /sendPhoto method supports Caption (File description up to 200 characters)
Without using /sendMessage.
You need to send the file using the /sendPhoto method.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LookingForSi, 2021-11-20
@LookingForSi

To resolve the issue, it is enough to remove the msg parameter and add the required 'caption' parameter to the data dictionary.
Final solution:
url = ' https://api.telegram.org/bot ' + token + '/sendPhoto'
file = {'photo': open('avatar1459.jpg', 'rb')}
data = {'chat_id ': -100134, 'caption': 'hello'}
r = req.post(url, files=file, data=data)
By the way, thanks for the hint with the question))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question