The documentation (https://core.telegram.org/bots/api#editmessagetext) says that I need to specify a message ID to edit, but I have no idea how to go about getting that message ID.
I’ve tried setting the ID as a variable:
import telepot
from telepot.namedTuple import InlineKeyboardMarkup, InlineKeyboardButton
messageEditID = bot.sendMessage(<location ID>, "Test", reply_markup=InlineKeyboardMarkup(inline_keyboard=[[InlineKeyboardButton(text="Testing", callback_data="test"]]))['message_id']
because the POST data for such a message would be
{"message_id":1140126,"chat":{"title":"<location name>","type":"supergroup","id":<location ID>},"date":1477960655,"from":{"username":"<bot username>","first_name":"<bot name>","id":<bot ID>},"text":"Test"}
and then calling it back based on the inline reply data
if msg['data'] == 'test':
bot.editMessage(messageEditID, "Did it work?")
but it throws an Exception with the following message:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 738, in collector
callback(item)
File "testingStuff.py", line 150, in handle
bot.editMessageText(messageEditID, "Does it work?")
File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 592, in editMessageText
return self._api_request('editMessageText', _rectify(p))
File "/usr/local/lib/python3.5/dist-packages/telepot/__init__.py", line 398, in _api_request
return api.request((self._token, method, params, files), **kwargs)
File "/usr/local/lib/python3.5/dist-packages/telepot/api.py", line 131, in request
return _parse(r)
File "/usr/local/lib/python3.5/dist-packages/telepot/api.py", line 126, in _parse
raise exception.TelegramError(description, error_code, data)
telepot.exception.TelegramError: ('Bad Request: Wrong inline message identifier specified', 400, {'description': 'Bad Request: Wrong inline message identifier specified', 'ok': False, 'error_code': 400})
It threw the same error when I tried to edit the message based on the response,
if msg['data'] == 'test':
msgID = msg['message']['message_id']
bot.editMessageText(msgID, "OBOY")
since the data for an incoming Inline Reply is:
{'chat_instance': '112564336693044113',
'data': 'test',
'from': {'first_name': 'B(', 'id': <user ID>, 'username': '<username>'},
'id': '852908411206280027',
'message': {'chat': {'id': <chat ID>,
'title': 'We da bes',
'type': 'supergroup',
'username': '<chat username>'},
'date': 1477961765,
'from': {'first_name': 'Testing For James',
'id': <bot ID>,
'username': '<bot username>'},
'message_id': 63180,
'text': 'Test'}}
Can anyone help me figure out where I’ve gone wrong?
3
Answers
Update: Telepot has a message_identifier method that you can call by setting the sendMessage method to a variable and then calling the message_identifier message
EDIT: In my answer, I am talking about
python-telegram-bot
and not abouttelepot
so it’s not relevant, sorry.You can only use the
edit
function if you are using messages with buttons (it is calledInlineKeyboard
). Here is an exampleHere is a simple example to edit a message without using
telepot
Demo: Before edit After edited