【问题标题】:How can i make a inline keyboardbutton with telebot for a chatterbot?我如何为聊天机器人制作带有远程机器人的内联键盘按钮?
【发布时间】:2023-01-25 06:16:23
【问题描述】:

我正在制作一个聊天机器人,我想知道如何放置内联键盘按钮,我该怎么做? 我需要一个可以处理大型对话的键盘按钮代码,谢谢。

【问题讨论】:

    标签: python telegram-bot py-telegram-bot-api telebot


    【解决方案1】:
    import telebot
    from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
    
    TELEGRAM_TOKEN = '<TOKEN>'
    
    bot = telebot.TeleBot(TELEGRAM_TOKEN)
    
    def gen_markup():
        markup = InlineKeyboardMarkup()
        markup.row_width = 2
        markup.add(InlineKeyboardButton("Yes", callback_data="cb_yes"),
                                InlineKeyboardButton("No", callback_data="cb_no"))
        return markup
    
    @bot.callback_query_handler(func=lambda call: True)
    def callback_query(call):
        if call.data == "cb_yes":
            bot.answer_callback_query(call.id, "Answer is Yes")
        elif call.data == "cb_no":
            bot.answer_callback_query(call.id, "Answer is No")
    
    @bot.message_handler(func=lambda message: True)
    def message_handler(message):
        bot.send_message(message.chat.id, "Yes/no?", reply_markup=gen_markup())
    
    bot.infinity_polling()
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
    【解决方案2】:

    您可以通过以下命令创建按钮

    从远程机器人导入类型

    markup = InlinKeyboardMarkup(row_width=1) # row_width 是按钮的数量

    some_item = types.InlineKeyboardButton("你的按钮的文本", callback_data="main info")

    markup.add(some_item) # 你需要通过 , 列出按钮

    【讨论】: