2016-08-24 55 views
0

我想與內嵌鍵盤聊天更新消息,但無法理解如何接收inline_message_id或者如果它僅適用於嵌入式查詢我怎麼能​​確定chat_idMESSAGE_IDeditMessageText(*args, **kwargs)上使用telegram.bot.Bot使用回調內嵌鍵盤更新信息查詢

我的代碼示例(的一部分):

#!/usr/bin/python 
import telegram 
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, InlineQueryHandler, CallbackQueryHandler 

tokenid = "YOUR_TOKEN_ID" 

def inl(bot, update): 
    if update.callback_query.data == "k_light_on": 
     #func for turn on light res = k_light.on() 
     bot.answerCallbackQuery(callback_query_id=update.callback_query.id, text="Turning on light ON!") 
     bot.editMessageText(inline_message_id=update.callback_query.inline_message_id, text="Do you want to turn On or Off light? Light is ON") 
     #hardcoded vars variant 
     #bot.editMessageText(message_id=298, chat_id=174554240, text="Do you want to turn On or Off light? Light is ON") 
    elif update.callback_query.data == "k_light_off": 
     #func for turn on light res = k_light.off() 
     bot.answerCallbackQuery(callback_query_id=update.callback_query.id, text="Turning off light OFF!") 
     bot.editMessageText(inline_message_id=update.callback_query.inline_message_id, text="Do you want to turn On or Off light? Light is ON") 
     #hardcoded vars variant 
     #bot.editMessageText(message_id=298, chat_id=174554240, text="Do you want to turn On or Off light? Light is OFF") 
    else: 
     print "Err" 

def k_light_h(bot, update): 
    reply_markup = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("On", callback_data="k_light_on"), telegram.InlineKeyboardButton("Off", callback_data="k_light_off")]]) 
    ddd = bot.sendMessage(chat_id=update.message.chat_id, text="Do you want to turn On or Off light?", reply_markup=reply_markup) 

if __name__ == "__main__": 
    # 
    updater = Updater(token=tokenid) 
    ### Handler groups 
    dispatcher = updater.dispatcher 
    # light 
    k_light_handler = CommandHandler('light', k_light_h) 
    dispatcher.add_handler(k_light_handler) 
    # errors 
    updater.dispatcher.add_error_handler(error) 
    updater.start_polling() 
    # Run the bot until the user presses Ctrl-C or the process receives SIGINT, 
    # SIGTERM or SIGABRT 
    updater.idle() 

當我運行它,我有一個錯誤:

telegram.ext.dispatcher - WARNING - A TelegramError was raised while processing the Update. 
root - WARNING - Update ... 
... caused error "u'Bad Request: message identifier is not specified'" 

我查無功update.callback_query.inline_message_id,它是空的。當我嘗試bot.editMessageText與硬編碼的變裝chat_idmessage_id它運作良好。

我需要保存在DB(對所有用戶)瓦爾chat_idMESSAGE_ID當它們運行命令/光,然後,當他們按內嵌按鈕,我需要從數據庫讀取這個值還是可以用一些簡單的方法用於編輯消息?

+1

我覺得你並不需要嘗試接受'inline_message_id'。 從內聯鍵盤迴調中收到'message_id'。 你能列出你的update.callback_query嗎?我認爲'update.callback_query.message.message_id'將在那裏 – Dmitry

+0

@Dmitry是的,'message_id'那裏,但我也需要'chat_id'非內聯消息。 – Alex

回答

1

您應該通過message_id而不是inline_message_ideditMessageText

所以你的解決方案是這樣的:

bot.editMessageText(
    message_id = update.callback_query.message.message_id, 
    chat_id = update.callback_query.message.chat.id, 
    text = "Do you want to turn On or Off light? Light is ON" 
) 
+0

謝謝,message_id是可以的,但是當我嘗試使用'message_id'而不是'inline_message_id'時,錯誤的請求:錯誤的內聯消息標識符指定了'我有一個關於空'chat_id'的錯誤。如何確定'chat_id'?我使用的不是內聯模式的機器人。 – Alex

+0

@Alex使用'update.callback_query.message.chat.id' – Nazar

+0

@Alex btw [RTFM](https://core.telegram.org/bots/api#available-types):) 參考[callbackquery] (https://core.telegram.org/bots/api#callbackquery)和[message](https://core.telegram.org/bots/api#message),所有信息都在那裏。 – Nazar