首页 > 解决方案 > pyTelegramBotAPI - 如何创建使用按钮更新的消息?

问题描述

我最近尝试创建一条消息,当按下内联键盘中的按钮时更新,但没有成功。

我正在使用 pyTelegramBotAPI,我可以让机器人用键盘发送消息,但我无法让各种按钮工作。

你能帮助我吗?:<

标签: telegram-botpy-telegram-bot-api

解决方案


为了创建多选项选择(即按钮),您使用InlineKeyboardButton对象

    options = []

    # buttons
    options.append(InlineKeyboardButton('One', callback_data='1'))
    options.append(InlineKeyboardButton('Two', callback_data='2'))
    options.append(InlineKeyboardButton('Three', callback_data='3'))

    reply_markup = InlineKeyboardMarkup([options])

    update.message.reply_text(response.message, reply_markup=reply_markup)

确保设置对应CallbackQueryHandler的处理用户选择

    updater.dispatcher.add_handler(CallbackQueryHandler(main_handler, pass_chat_data=True, pass_user_data=True))

在上面的示例中,该方法main_handler(update, context)将负责处理用户输入。

随意查看TelegramBotDemo GitHub 存储库以查看完整实现


推荐阅读