C
C
chelix2020-05-19 14:58:27
Python
chelix, 2020-05-19 14:58:27

How to make inline menu chain in Telegram bot in Python?

I am writing a telegram bot in Python using the telebot library.
I want to build a menu chain like this:
/start -> ReplyKeyboardMarkup "Choose me" button -> Inline button "First" -> Inline button "Third" -> Message "End"
5ec3ca5529bde760985712.png

Everything works fine until the "Third" button. As I understand it, the step3 function no longer receives parameters. Tell me what's wrong? Decorator added, does not help. Sorry if this is a dumb question, I'm new.
My code:

import telebot
from config import TOKEN

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def process_start(message):
    keyboard = telebot.types.ReplyKeyboardMarkup(True)
    keyboard.row('Выбери меня')
    msg = bot.send_message(message.chat.id, text = 'Нажми кнопку в меню', reply_markup = keyboard )


@bot.message_handler(content_types = ['text'])
def step1(message):
    menu1 = telebot.types.InlineKeyboardMarkup()
    menu1.add(telebot.types.InlineKeyboardButton(text = 'Первая кнопка', callback_data ='first'))
    menu1.add(telebot.types.InlineKeyboardButton(text = 'Вторая кнопка', callback_data ='second'))

    if message.text == 'Выбери меня':
        msg = bot.send_message(message.chat.id, text ='Нажми первую inline кнопку', reply_markup = menu1)
        bot.register_next_step_handler(msg, step2)


@bot.callback_query_handler(func=lambda call: True)
def step2(call):
    menu2 = telebot.types.InlineKeyboardMarkup()
    menu2.add(telebot.types.InlineKeyboardButton(text = 'Третья кнопка', callback_data ='third'))
    menu2.add(telebot.types.InlineKeyboardButton(text = 'Четвертая кнопка', callback_data ='fourth'))

    if call.data == 'first':
        msg = bot.send_message(call.message.chat.id, 'Нажми третью кнопку', reply_markup = menu2)
        bot.register_next_step_handler(msg, step3)

def step3(call):
    if call.data == 'third':
        msg = bot.send_message(call.message.chat.id, 'Конец')
    else:
        pass
  

bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheAngryPython, 2020-05-19
@chelix

import telebot
from config import TOKEN

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def process_start(message):
    keyboard = telebot.types.ReplyKeyboardMarkup(True)
    keyboard.row('Выбери меня')
    msg = bot.send_message(message.chat.id, text = 'Нажми кнопку в меню', reply_markup = keyboard )


@bot.message_handler(content_types = ['text'])
def step1(message):
    menu1 = telebot.types.InlineKeyboardMarkup()
    menu1.add(telebot.types.InlineKeyboardButton(text = 'Первая кнопка', callback_data ='first'))
    menu1.add(telebot.types.InlineKeyboardButton(text = 'Вторая кнопка', callback_data ='second'))

    if message.text == 'Выбери меня':
        msg = bot.send_message(message.chat.id, text ='Нажми первую inline кнопку', reply_markup = menu1)

@bot.callback_query_handler(func=lambda call: True)
def step2(call):
    menu2 = telebot.types.InlineKeyboardMarkup()
    menu2.add(telebot.types.InlineKeyboardButton(text = 'Третья кнопка', callback_data ='third'))
    menu2.add(telebot.types.InlineKeyboardButton(text = 'Четвертая кнопка', callback_data ='fourth'))

    if call.data == 'first':
        msg = bot.send_message(call.message.chat.id, 'Нажми третью кнопку', reply_markup = menu2)
    elif call.data == 'third':
        msg = bot.send_message(call.message.chat.id, 'Конец')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question