如何使用python的python-telegram-bot库将inlineKeyboardButton替换为另一个inlineKeyboardButton?

时间:2019-02-12 05:49:04

标签: telegram python-telegram-bot

我是编程新手,现在正尝试创建一个简单的菜单电报机器人。

如何将当前的inlinekeyboardbutton替换为另一个?我只想构建一个带有按钮的菜单,具体取决于我单击的内容,因此会显示不同的inline键盘按钮。通过模仿“快乐”,我希望出现三种不同的快乐状态。有人可以建议简单的方法吗?

def start(bot, update):
    keyboard = [
                [InlineKeyboardButton("Happy", callback_data='1'),
                 InlineKeyboardButton("Whatever", callback_data='2')],
                [InlineKeyboardButton("Sad", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)

    update.message.reply_text('Hey there! How do you feel today?', reply_markup=reply_markup)




def button(bot, update):
    query = update.callback_query
    if query.data == "1":

        bot.editMessageText(text="wowowowow",
                       chat_id=query.message.chat_id,
                       message_id=query.message.message_id)

    if query.data == "2":
        em = emoji.emojize(':expressionless:', use_aliases=True)
        bot.editMessageText(text="Does it matter? %s " % em,
                        chat_id=query.message.chat_id,
                        message_id=query.message.message_id)

    if query.data == "3":
        em = emoji.emojize(':disappointed:', use_aliases=True)
        bot.editMessageText(text="Oh man! %s " % em,
                        chat_id=query.message.chat_id,
                        message_id=query.message.message_id)

2 个答案:

答案 0 :(得分:0)

要替换InlineKeyboard,可以使用bot.edit_message_reply_markup

会是这样的:

def button(bot,update):
   query=update.callback_query

   if query.data == "1":
      keyboard=[[InlineKeyboardButton(' Just Happy',callback_data='0')],
                [InlineKeyboardButton('Very Happy',callback_data='1')]]

      reply_markup=InlineKeyboardMarkup(keyboard)

      bot.edit_message_reply_markup(
         chat_id=query.message.chat_id,
         message_id=query.message.message_id,
         reply_markup=reply_markup)

希望这很有用!

答案 1 :(得分:0)

要替换嵌入式键盘,只需使用bot.editMessageText或bot.editMessageReplyMarkup方法

I am using bot.editMessageText for this problem:

def button(bot,update):
    query=update.callback_query
    if query.data=='1':
        keyboard=[[InlineKeyboardButton('Happy 1',callback_data='1')],[InlineKeyboardButton('Happy 2',callback_data='2')],[InlineKeyboardButton('Happy 3',callback_data='3')
        reply_markup=InlineKeyboardMarkup(keyboard)
          bot.editMessageText(chat_id=query.message.chat_id,message_id=query.message.message_id,reply_markup=reply_markup)