skip to Main Content

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


  1. Chosen as BEST ANSWER

    After many attempts, I figured out how to proceed for serialize the string and then edit:

    import requests
    import json
    
    BOT_TOKEN = 'AAAAAAAAAA'
    chat_telegram_channel = 'BBBBBBBBBB'
    idmessage = 16967
    
    image_file = 'jogos_de_hoje_na_tv_plus_watermark.png'
    urlalert = f'https://api.telegram.org/bot{BOT_TOKEN}/editMessageMedia'
    textalert = f'Programação de jogos na TVnPowered by:n@mattosxperiences'
    
    files = {
        'media': open(image_file, "rb")
    }
    media = json.dumps({
        'caption':textalert,
        'type': 'photo',
        'media': 'attach://media'
    })
    params = { 
        'chat_id':chat_telegram_channel, 
        'message_id':idmessage, 
        'media': media,
        'parse_mode':'HTML'
        }
    
    requests.get(urlalert, params=params, files=files)
    

  2. remove photourl from data and put it in request files parameter as below :

    photourl = open(image_file, "rb")
    data = {
    'caption':textalert, 'chat_id':chat_telegram_channel, 'message_id':idmessage,
    'parse_mode':'HTML'
    }
    files = {'media':photourl}
    requests.post(urlalert, headers=headers, data=data, files=files)
    
    Login or Signup to reply.
  3. You should upload files using POST request.

    This is how it’s done, sending and editing a photo using post requests:

    import requests
    import json
    
    botToken = 'REPLACE_TOKEN'
    chatId = 0 # REPLACE CHAT ID
    
    
    def send_photo():
        image = "image.jpg"
        address = f'https://api.telegram.org/bot{botToken}/sendPhoto'
        data = {"chat_id": chatId, "caption": "before edit"}
        with open(image, "rb") as imageFile:
            result = requests.post(address, files={"photo": imageFile}, data=data).json()
            if result["ok"]:
                return result["result"]["message_id"]
            else:
                raise Exception(result["description"])
        return
    
    
    def edit_photo(message_id):
        image = "image2.jpg"
        address = f'https://api.telegram.org/bot{botToken}/editMessageMedia'
        media = {"type": "photo", "media": "attach://photo", "caption": "after edit"}
        data = {"chat_id": chatId, "message_id": message_id, "media": json.dumps(media)}
        with open(image, "rb") as imageFile:
            requests.post(address, files={"photo": imageFile}, data=data)
        return
    
    
    edit_photo(send_photo())
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search