【问题标题】:Python Telegram Bot Edit Custom KeyboardPython Telegram Bot 编辑自定义键盘
【发布时间】:2020-03-01 08:28:04
【问题描述】:

我正在使用 Python Telegram BOT API 来构建演示应用程序。我要完成的任务是创建一个自定义键盘,然后在每次与用户交互时继续编辑键盘键。我尝试使用“edit_message_reply_markup”,但收到错误“无法编辑消息”。不能编辑自定义键盘吗?

这是我为我的任务编写的示例代码。

初始任务:

FirstKeyboard = [[KeyboardButton(text = "FRUITS"), KeyboardButton(text = "VEGITABLES"), KeyboardButton(text = "DRINKS")],[KeyboardButton(text = "SNACKS"), KeyboardButton(text = "CHIPS"), KeyboardButton(text = "NOTHING")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(FirstKeyboard)
KeyboardMessageID = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)

编辑任务:

SecondKeyBoard = [[KeyboardButton(text = "APPLE"), KeyboardButton(text = "BANANA"), KeyboardButton(text = "PUMPKIN")],[KeyboardButton(text = "ORANGES"), KeyboardButton(text = "GRAPES"), KeyboardButton(text = "WINE")],[KeyboardButton(text = "DONE")]]
menu = ReplyKeyboardMarkup(SecondKeyBoard)
KeyboardMessageID = context.bot.edit_message_reply_markup(chat_id = chatID, message_id = KeyboardMessageID, reply_markup = menu)

我收到错误消息“无法编辑消息”

【问题讨论】:

  • 使用 context.message.reply_text("你的文字", reply_markup=你的键盘标记)。并确保至少有一个文本或标记发生了更改,然后您在相同的输入中遇到错误。

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


【解决方案1】:

https://core.telegram.org/bots/api#updating-messages

目前看来我们可以编辑内联键盘,但无法编辑回复键盘

【讨论】:

    【解决方案2】:

    您可以使用新的 KeyboardButton 发送新消息,而不是编辑以前的消息。新的回复键盘标记将自动替换为旧的回复键盘标记。 使用:

    context.bot.send_message(chat_id = chatID, text = "Select What you 
    Like", reply_markup = NEW_Menu)
    

    或回复您的用户:

    update.message.reply_text(text = "Select What you Like", reply_markup = NEW_Menu)
    

    您可以更改新按摩中的文字或将其放在上一个。

    【讨论】:

      【解决方案3】:

      作为一种解决方法,我删除了以前的消息并将其复制,但 reply_markup 参数除外。在你的情况下是这样的:

      context.user_data['last_message'] = context.bot.send_message(chat_id = chatID, text = "Select What you Like", reply_markup = menu)
      
      # changing reply markup
      last_message = context.user_data['last_message']
      context.bot.delete_message(chat_id=update.effective_chat.id, message_id=last_message.message_id)
      # create similar message except new menu
      context.user_data['last_message'] = context.bot.send_message(chat_id = last_message.chat_id, text = last_message.text, reply_markup = new_menu)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-14
        • 2016-04-26
        • 1970-01-01
        • 1970-01-01
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多