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

2024-04-26 04:41:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用内联键盘更新chat中的消息,但不明白如何接收lined_message_id或如果它仅用于内联查询,我如何确定chat_idmessage_id在classtelegram.bot.bot中的editMessageText(*args, **kwargs)上使用它?

我的代码示例(其中一部分):

#!/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'"

我检查了varupdate.callback_query.inline_message_id并且它是空的。当我尝试使用硬编码varschat\u idmessage\u idbot.editMessageText时,效果很好。

我需要在数据库中保存(对于所有用户)varschat\u idmessage\u id当他们运行command/light时,然后当他们按下inline按钮时,我需要从数据库中读取该值还是可以使用一些更简单的方法来编辑消息?


Tags: ortextidmessageonbotcallbackchat
1条回答
网友
1楼 · 发布于 2024-04-26 04:41:11

您应该将message_id而不是inline_message_id传递给editMessageText

所以你的解决方案是:

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

相关问题 更多 >