Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telebot Telegram Message with Buttons

I'm trying create a news telegram bot. But iI can't send messages with interactive buttons.

The reason why I use the buttons is to make a menu, I would appreciate if you could show an example of making an interactive menu instead of just adding it.

Using Language : Python 3.9 & Telebot Library

Like this:

enter image description here

like image 577
qraxiss Avatar asked Feb 28 '26 06:02

qraxiss


2 Answers

Telebot provides the following types for InlineKeyboard:

  • InlineKeyboardMarkup; The keyboard
  • InlineKeyboardButton; The markup button

An basic example would look like

from telebot import TeleBot
from telebot import types

bot = TeleBot("859163076:A......")
chat_id = 12345

button_foo = types.InlineKeyboardButton('Foo', callback_data='foo')
button_bar = types.InlineKeyboardButton('Bar', callback_data='bar')

keyboard = types.InlineKeyboardMarkup()
keyboard.add(button_foo)
keyboard.add(button_bar)

bot.send_message(chat_id, text='Keyboard example', reply_markup=keyboard)

Which will result in the following message send to chat_id:

enter image description here

like image 50
0stone0 Avatar answered Mar 02 '26 11:03

0stone0


In this example, we create a menu with two buttons, "Button 1" and "Button 2", and process each button separately. When the user selects one of the buttons, the bot sends the corresponding message.

    import telebot
    from telebot import types
    
    bot = telebot.TeleBot('YOUR_BOT_TOKEN')
    
    #Command /start
    @bot.message_handler(commands=['start'])
    def start(message):
        markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
        item1 = types.KeyboardButton("Bottom 1")
        item2 = types.KeyboardButton("Bottom 2")
        markup.add(item1, item2)
    
        bot.send_message(message.chat.id, "Select option:", reply_markup=markup)
    
    #Bottom 1
    @bot.message_handler(func=lambda message: message.text == "Bottom 1")
    def button1(message):
        bot.send_message(message.chat.id, "U select bottom 1")
    
    #Bottom 2
    @bot.message_handler(func=lambda message: message.text == "Bottom 2")
    def button2(message):
        bot.send_message(message.chat.id, "U select bottom 2")
    
    #Start bot
    if __name__ == '__main__':
        bot.polling(none_stop=True)
like image 45
VirbickasGytautas Avatar answered Mar 02 '26 11:03

VirbickasGytautas