0

I am trying to edit messages on telegram bot with the editMessageText, but it requires a message_id integer, so i need to somehow parse the telegram response when i send a message with

https://api.telegram.org/bot12345:abcdefghijk-lmnopqrstuvwxyz/sendMessage?text=Some%20Text&chat_id=123456789

it will respond with something like this :

{"ok":true,"result":{"message_id":213557,"from":{"id":bot_id,"is_bot":true,"first_name":"BotName","username":"SpaceTheBot"},"chat":{"id":123456789,"title":"A Group","type":"supergroup"},"date":1612928163,"text":"Some text"}}

so i wanna parse the message_id, so i can edit it later on.

Jignasha Royala
  • 1,032
  • 10
  • 27
Quebeh
  • 134
  • 10
  • Try this. Although this is for deleting messages, you can apply to edit messages. https://stackoverflow.com/questions/65761031/delete-outgoing-message-sent-by-telegram-bot-telegram-python/65919524#65919524 – Rafael Colombo Feb 10 '21 at 10:50
  • 1
    Hey! thank you for your response but i am **not** using a long polling method, i appreciate your help. – Quebeh Feb 11 '21 at 11:42

1 Answers1

1

You will need to deserialize the response from Telegram. After that it will be a Python object. In this case because it is a JSON object it will be turned into a dict and can be accessed as such.

import json

response = '{"ok":true,"result":{"message_id":213557,"from":{"id":"bot_id","is_bot":true,"first_name":"BotName","username":"SpaceTheBot"},"chat":{"id":123456789,"title":"A Group","type":"supergroup"},"date":1612928163,"text":"Some text"}}'
result = json.loads(response)

print(result["result"]["message_id"])
>>> 213557

If you are using requests, it has its own JSON encorder/decoders

im_baby
  • 912
  • 1
  • 8
  • 14