pyTelegramBotAPI - 如何创建使用按钮更新的消息?

问题描述

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

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

你能帮我吗? :

解决方法

为了创建多选项(即按钮),您使用 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 存储库以查看完整实现