skip to Main Content

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.

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

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. Find codes in the image below to embed an inline keyboard inside a reply keyboard

    enter image description here

    from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, KeyboardButton, InlineKeyboardButton, InlineKeyboardMarkup
    
    reply2_keyboard = [
        [
            InlineKeyboardButton("Option 1", callback_data="1"),
            InlineKeyboardButton("Option 2", callback_data="2"),
        ],
            [InlineKeyboardButton("Option 3", callback_data="3")],
        ]
    
    
    markup1 = InlineKeyboardMarkup(reply2_keyboard)
    
    def start_command(message):
        bot.send_message(
            message.chat.id,
            'Hello!',
            reply_markup=markup1
        )
        ```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search