qraxiss
qraxiss

Reputation: 126

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

Upvotes: 3

Views: 16609

Answers (3)

VirbickasGytautas
VirbickasGytautas

Reputation: 21

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)

Upvotes: 1

Seruios
Seruios

Reputation: 11

use callback query handler, there is example here:

https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/inline_keyboard_example.py

Upvotes: -1

0stone0
0stone0

Reputation: 43904

Telebot provides the following types for InlineKeyboard:


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

Upvotes: 3

Related Questions