所以我正在使用 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/