使用回调查询使用内联键盘更新消息

Ale*_*lex 1 python telegram python-telegram-bot telegram-bot

我想在内联键盘的聊天中更新消息但是无法理解如何接收inline_message_id,或者它是否仅用于内联查询我如何确定chat_idmessage_id在类电报中的editMessageText(*args,**kwargs)上使用它.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()
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我有一个错误:

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'"
Run Code Online (Sandbox Code Playgroud)

我检查了var update.callback_query.inline_message_id,它是空的.当我用硬编码的vars chat_idmessage_id尝试bot.editMessageText时效果很好.

我是否需要保存在DB(对于所有用户)vars chat_idmessage_id,当他们运行命令/灯,然后当他们按内联按钮我需要从DB读取此值或我可以使用一些更简单的方法来编辑消息?

Naz*_*zar 5

你应该通过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"
)
Run Code Online (Sandbox Code Playgroud)