Answer the question
In order to leave comments, you need to log in
How to make a multi-level telegram bot menu (inline - callback) in python?
Good afternoon. There is a telegram bot code that displays 4 buttons and when you click on each of them, a message is displayed.
from telebot import types
import constants, os, re
bot = telebot.TeleBot(constants.token)
@bot.message_handler(commands=["start"])
def inline(message):
key = types.InlineKeyboardMarkup()
but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
but_4 = types.InlineKeyboardButton(text="Number4", callback_data="Number4")
key.add(but_1, but_2, but_3, but_4)
bot.send_message(message.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=key)
@bot.callback_query_handler(func=lambda c:True)
def inlin(c):
if c.data == 'NumberOne':
bot.send_message(c.message.chat.id, 'Это кнопка 1')
if c.data == 'NumberTwo':
bot.send_message(c.message.chat.id, 'Это кнопка 2')
if c.data == 'NumberTree':
bot.send_message(c.message.chat.id, 'Это кнопка 3')
if c.data == 'Number4':
bot.send_message(c.message.chat.id, 'Это кнопка 4')
# elif c.data == 'NumberTwo':
# bot.send_message(c.message.chat.id, 'Это мазафака, кнопка 2')
if __name__ == "__main__":
bot.polling(none_stop=True)
Answer the question
In order to leave comments, you need to log in
It is necessary to add an optional parameter reply_markup=key to bot.send_message (i.e., in addition to sending the text, the bot will create a menu), after specifying the necessary information.
Here's a quick example (change 3 buttons):
@bot.message_handler(commands=["start"])
def inline(message):
key = types.InlineKeyboardMarkup()
but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
key.add(but_1, but_2, but_3)
bot.send_message(message.chat.id, "ВЫБЕРИТЕ КНОПКУ", reply_markup=key)
@bot.callback_query_handler(func=lambda c:True)
def inline(c):
if c.data == 'NumberOne':
bot.send_message(c.message.chat.id, 'Это кнопка 1')
if c.data == 'NumberTwo':
bot.send_message(c.message.chat.id, 'Это кнопка 2')
if c.data == 'NumberTree':
key = types.InlineKeyboardMarkup()
but_1 = types.InlineKeyboardButton(text="NumberOne", callback_data="NumberOne")
but_2 = types.InlineKeyboardButton(text="NumberTwo", callback_data="NumberTwo")
but_3 = types.InlineKeyboardButton(text="NumberTree", callback_data="NumberTree")
key.add(but_1, but_2, but_3)
bot.send_message(c.message.chat.id, 'Это кнопка 3', reply_markup=key)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question