python - 在回复键盘内制作内联键盘

标签 python telegram telegram-bot

所以我正在使用 pyTelegramBotApi 制作一个 Telegram Bot 。

我制作了一个回复键盘,它出现在/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 表示应使用内联键盘发送文本的事物(函数)。

最佳答案

不要与“键盘”这个词混淆,当您看到粘在聊天底部的“经典”键盘时,这只是一堆消息模板,没有其他东西(有 2 个特殊按钮,一个要求 Geo,另一个要求电话,但现在没关系)。

那么,当用户按下带有“Button 1”文本的按钮时会发生什么?一条包含该文本的消息将发送到您的机器人。为了正确地响应该文本,请编写一个处理程序来检查 message.text 是否完全匹配。为了您的方便,我对您的代码进行了一些更新,并为用户按下带有“Button 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)

关于python - 在回复键盘内制作内联键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58921336/

相关文章:

python - Pandas Dataframe 使用 Groupby 从其他两列的唯一值创建下一个 future 日期的列

python - Django def __str__ 列表索引超出范围

ios - iOS 机器人上的 Telegram 错误发送多次相同的消息

go - 检查哪些用户向 Telegram Bot 发送表情符号

python-3.x - 电报机器人 : how to ask for user input on button click

python - 如何在不使用外部库(例如 Numpy、Pandas)的情况下读取 CSV 文件?

python - 无法在 Ubuntu 14.04 上绘制直方图

python-3.x - 连接到 VPN 时出现 Python3 Telepot SSL 错误

telegram - Telegram 机器人有没有办法获取它在管理员的私有(private) channel 中发送的自己的消息?

telegram - 当它是一个组的一部分时如何隐藏 Telegram BOT 命令?