【问题标题】:Updating messages with inline keyboards using callback queries使用回调查询使用内联键盘更新消息
【发布时间】:2016-08-24 11:09:16
【问题描述】:

我想使用内联键盘更新聊天消息,但不明白如何接收 inline_message_id 或者如果它仅用于内联查询,我如何确定 chat_idmessage_id 用于在类 telegram.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'"

我检查了 var update.callback_query.inline_message_id,它是空的。当我尝试使用硬编码变量 chat_idmessage_idbot.editMessageText 时效果很好。

当他们运行命令 /light 然后当他们按下内联按钮时,我是否需要保存在数据库中(对于所有用户)vars chat_idmessage_id 我需要从数据库中读取这个值还是我可以使用一些更简单的方法来编辑消息?

【问题讨论】:

  • 我认为你不需要尝试接收inline_message_id。从内联键盘回调中,您会收到 message_id。你能列出你的 update.callback_query 吗?我想update.callback_query.message.message_id 会在那里
  • @Dmitry 是的,message_id 在那里,但我也需要 chat_id 来接收非内联消息。

标签: python telegram telegram-bot python-telegram-bot


【解决方案1】:

您应该将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"
)

【讨论】:

  • 谢谢,message_id 没问题,但是当我尝试使用message_id 而不是inline_message_id 时,Bad Request: Wrong inline message identifier specified 出现错误,我有一个关于空chat_id 的错误。如何确定chat_id?我使用的机器人不是内联模式。
  • @Alex btw RTFM :) 参考callbackquerymessage,所有信息都在那里。
  • 是的,抱歉,我是 python 中的 OOP 新手,不知道如何找到可用的类型。我试图在库文档和 python dir() 中找到它。现在很清楚了,谢谢。
【解决方案2】:

你总是可以这样做

    try:
        query.edit_message_reply_markup(reply_markup=InlineKeyboardMarkup(reply_keyboard))
    except:
        return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2020-01-05
    • 2015-08-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多