2016-11-01 217 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

相關問題