【问题标题】:Making inline keyboard inside reply kerboard在回复键盘内制作内联键盘
【发布时间】:2019-11-22 05:27:33
【问题描述】:

所以我正在用 pyTelegramBotApi 制作一个电报机器人 我做了一个回复键盘,它出现在 /start 命令之后,问题是当用户按下按钮时,他们应该收到一些消息和一个内联键盘。而且我不知道如何将内联键盘(有点)放在回复键盘中。

def start_command(message):
    keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    button1 = types.KeyboardButton(text="Button 1", FUNC1)
    button2 = types.KeyboardButton(text="Button 2", FUNC2)
    button3 = types.KeyboardButton(text="Button 3", FUNC3)

    keyboard.add(button1, button2, button3)
    bot.send_message(
        message.chat.id,
        'Hello!',
        reply_markup=keyboard
  )

FUNC1、FUNC2、FUNC3 表示应该使用内联键盘发送文本的事物(函数)

【问题讨论】:

    标签: python telegram telegram-bot


    【解决方案1】:

    不要与“键盘”这个词混淆,当你看到一个“经典”键盘粘在聊天底部时,这只是一堆消息模板,没有别的(有 2 个特殊按钮,一个要求 Geo,另一个要求 Phone,但暂时不要介意)。

    那么,当用户按下带有“按钮 1”文本的按钮时会发生什么?带有该文本的消息将发送到您的机器人。为了正确响应该文本,编写一个处理程序来检查message.text 是否完全匹配。为了您的方便,我稍微更新了您的代码,并为用户按下带有“按钮 1”文本的按钮的情况编写了一个处理程序。

    当用户按下“按钮 1”时,您的机器人将使用包含一个内联按钮的内联键盘向他们发送一条消息,从而引导至 stackoverflow.com。

    # This is your original function, however, I removed "FUNC 1", "FUNC 2" and "FUNC 3"
    # from buttons' properties.
    @bot.message_handler(commands=["start"])
    def start_command(message):
        keyboard = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
        button1 = types.KeyboardButton(text="Button 1")
        button2 = types.KeyboardButton(text="Button 2")
        button3 = types.KeyboardButton(text="Button 3")
    
        keyboard.add(button1, button2, button3)
        bot.send_message(message.chat.id, 'Hello!', reply_markup=keyboard)
    
    # Here's a simple handler when user presses button with "Button 1" text
    @bot.message_handler(content_types=["text"], func=lambda message: message.text == "Button 1")
    def func1(message):
        keyboard = types.InlineKeyboardMarkup()
        url_btn = types.InlineKeyboardButton(url="https://stackoverflow.com", text="Go to StackOverflow")
        keyboard.add(url_btn)
        bot.send_message(message.chat.id, "Button 1 handler", reply_markup=keyboard)
    

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 2017-06-28
      • 2020-11-02
      • 2019-06-30
      • 2010-11-09
      • 2020-02-03
      相关资源
      最近更新 更多