2016-11-01 218 views
2

文档(https://core.telegram.org/bots/api#editmessagetext)表示我需要指定要编辑的消息ID,但我不知道如何获取该消息ID。如何使用Telegram的python bot API编辑发送的消息?

我试图设置ID作为变量:

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'] 

,因为这样的消息的POST数据将是

{"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"} 

,然后根据内联答复数据调用它回

if msg['data'] == 'test': 
    bot.editMessage(messageEditID, "Did it work?") 

但它会抛出一个异常并显示以下消息:

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}) 

它扔了同样的错误,当我试图编辑基于响应消息,

if msg['data'] == 'test': 
    msgID = msg['message']['message_id'] 
    bot.editMessageText(msgID, "OBOY") 

,因为对于进入在线回复的数据是:

{'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'}} 

任何人都可以帮我弄清楚我哪里出错了?

回答

0

更新:Telepot有,你可以通过的sendMessage方法设置为一个变量,然后调用message_identifier消息

sent = bot.sendMessage(9999999, "Testing") 
edited = telepot.message_identifier(sent) 
bot.editMessageText(edited, "Tested") 
0

,如果你使用的是带有按钮的消息只能使用编辑功能调用message_identifier方法(它被称为“InlineKeyboard”)。这是一个example

相关问题