Method I’m using is based on the model I use to create a new image publication in the channel, with the modifications it currently looks like:
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
}
image_file = 'jogos_de_hoje_na_tv_plus_watermark.png'
chat_telegram_channel = ['-XXXXXXXXXXXXXXXXX']
idmessage = 16967
textalert = f'AAAAAAAAAA'
botalert = 'BBBBBBBBBBBB'
urlalert = f'https://api.telegram.org/bot{botalert}/editMessageMedia'
photourl = open(image_file, "rb")
params = {'caption':textalert, 'chat_id':chat_telegram_channel, 'message_id':idmessage, 'media':photourl, 'parse_mode':'HTML'}
requests.get(urlalert, headers=headers, params=params)
The request response is:
<html>
<head><title>414 Request-URI Too Large</title></head>
<body>
<center><h1>414 Request-URI Too Large</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
Trying to use POST
instead of GET
:
data = {'caption':textalert, 'chat_id':chat_telegram_channel, 'message_id':idmessage, 'media':photourl, 'parse_mode':'HTML'}
requests.post(urlalert, headers=headers, data=data)
The response is:
{
"ok":false,
"error_code":400,
"description":"Bad Request: can't parse input media JSON object"
}
According to this answer (https://stackoverflow.com/a/68689553/11462274), it is necessary to serialize the string, well, I tried to do this:
photourl = open(image_file, "rb")
data = {'caption':textalert, 'chat_id':chat_telegram_channel, 'message_id':idmessage, 'media':json.dumps(str(photourl)), 'parse_mode':'HTML'}
requests.post(urlalert, headers=headers, data=data)
But the response was:
{
"ok":false,
"error_code":400,
"description":"Bad Request: can't parse InputMedia: expected an Object"
}
The API part for this need is:
https://core.telegram.org/bots/api#editmessagemedia
What should I do to resolve this issue?
3
Answers
After many attempts, I figured out how to proceed for serialize the string and then edit:
remove
photourl
from data and put it in requestfiles
parameter as below :You should upload files using POST request.
This is how it’s done, sending and editing a photo using post requests: