2
votes

So I'm making a telegram bot with pyTelegramBotApi I made a reply keyboard which appears after a /start command, the thing is that when user presses the button they should receive some message and an inline keyboard with that. And I don't know how do I put inline keyboard (kinda) in a reply keyboard.

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 represents that thing (function) that should send a text with an inline keyboard

1

1 Answers

1
votes

Don't be confused with the word "keyboard", when you see a "classic" keyboard which sticks to a bottom of the chat, this is just a bunch of message templates, nothing else (there're 2 special buttons, one asking for Geo and another one asking for Phone, but nevermind for now).

So, what happens when user presses your button with "Button 1" text? A message is sent to your bot with that text. And to properly react to that text, write a handler which checks for message.text exact match. For your convenience, I've updated your code a bit and wrote a handler for a case when user presses button with "Button 1" text.

When user press "Button 1", your bot will send them a message with an inline keyboard consisting of one inline button, leading to 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)