Not called register_next_step_handler

Question:

I’m just beginner at python.

I need to create a bot in telegram.
I ran into a problem that no register_next_step_handler is called in a_handler. It seems that I need to send a new message to the user in order to pass a new message parameter, BUT

  1. This is nonsense.
  2. It didn’t work out that way either

Here is example of my code:

import telebot
from telebot import types

#main variables
TOKEN = "token"
bot = telebot.TeleBot(TOKEN)


@bot.message_handler(commands=['start', 'go'])
def start_handler(message):
    bot.send_message(message.chat.id, 'Hi! It doesn't work yet.')

@bot.message_handler(commands=['set'])
def begin_init(message) :
    cat_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    cat_markup.row('Button 1', 'Button 2')
    cat_markup.row('Button 3', 'Button 4')

    msg = bot.send_message(message.chat.id, 'Button options', reply_markup=cat_markup)

    bot.register_next_step_handler(msg, a_handler)
    return

def a_handler(message):
    a = message.text.lower()

    
    if a == 'button 1' :
        b = 100499
        return bot.register_next_step_handler(message, c_handler, b)
    elif a == 'button 2'  :
        ...
    elif a == 'button 3' :
        b = 100500
        return bot.register_next_step_handler(message, c_handler, b)
    elif a == 'button 4' :
        # msg = bot.send_message(message.chat.id, 'Some kind of message')
        return bot.register_next_step_handler(message, b_handler)

    else :
        msg = bot.send_message(message.chat.id, 'Incorrect data.nTry again!')
        return bot.register_next_step_handler(msg, begin_init)

    return


def b_handler(message):
    msg = bot.send_message(message.chat.id, 'Enter b')
    b = message.text
    return bot.register_next_step_handler(msg, c_handler, b)

def c_handler(message, b) :
    msg = bot.send_message(message.chat.id, 'Enter c')
    c = message.text
    return bot.register_next_step_handler(msg, create_smth, b, c)


def create_smth(message, b, c) :
    #some kind of magic
    a = 100500


@bot.message_handler(content_types=['text'])
def text_handler(message):
    text = message.text.lower()
    chat_id = message.chat.id
    if text == "Hi":
        bot.send_message(chat_id, 'Hi, I'm useless.')
    else:
        bot.send_message(chat_id, 'Sorry, I did not get you :(')



bot.polling()

I thought that the keyboard that I provide might interfere, but I entered the text manually, not through the buttons.

I will be glad of any help.
python 3.10
win 11

Asked By: Ivan Lebedev

||

Answers:

register_next_step_handler works correctly – if you add print('b_handler') in function b_handler() then you will see it is executed.

Problem is: you display Enter c, Enter b in wrong moments.

You have to add message Enter b when you register b_handler, not inside function b_handler

    if a == 'button 1' :
        b = 100499
        msg = bot.send_message(message.chat.id, 'Enter c')
        bot.register_next_step_handler(msg, c_handler, b)
    elif a == 'button 3' :
        b = 100500
        msg = bot.send_message(message.chat.id, 'Enter c')
        bot.register_next_step_handler(message, c_handler, b)
    elif a == 'button 4' :
        msg = bot.send_message(message.chat.id, 'Enter b')
        bot.register_next_step_handler(msg, b_handler)

And the same problem is in b_handler when you register c_handler

def b_handler(message):
    print('[b_handler]')

    b = message.text
    
    msg = bot.send_message(message.chat.id, 'Enter c')
    bot.register_next_step_handler(msg, c_handler, b)

And some kind of magic you should do inside c_handler, not in create_smth


Full working code:

import telebot
from telebot import types

TOKEN = "xxx"
bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start', 'go'])
def start_handler(message):
    bot.send_message(message.chat.id, 'Hi! It doesn't work yet.')

@bot.message_handler(commands=['set'])
def begin_init(message) :
    print('[begin_init]')

    cat_markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    cat_markup.row('Button 1', 'Button 2')
    cat_markup.row('Button 3', 'Button 4')

    msg = bot.send_message(message.chat.id, 'Button options', reply_markup=cat_markup)

    bot.register_next_step_handler(msg, a_handler)

def a_handler(message):
    print('[a_handler]')
    
    a = message.text.lower()

    if a == 'button 1' :
        b = 100499
        msg = bot.send_message(message.chat.id, 'Enter c')
        bot.register_next_step_handler(msg, c_handler, b)
    elif a == 'button 3' :
        b = 100500
        msg = bot.send_message(message.chat.id, 'Enter c')
        bot.register_next_step_handler(message, c_handler, b)
    elif a == 'button 4' :
        msg = bot.send_message(message.chat.id, 'Enter b')
        bot.register_next_step_handler(msg, b_handler)
    else:
        msg = bot.send_message(message.chat.id, 'Incorrect data.nTry again!')
        bot.register_next_step_handler(msg, begin_init)

def b_handler(message):
    print('[b_handler]')

    b = message.text
    
    msg = bot.send_message(message.chat.id, 'Enter c')
    bot.register_next_step_handler(msg, c_handler, b)

def c_handler(message, b) :
    print('[c_handler]')
    
    c = message.text
    
    #some kind of magic
    result = int(b) + int(c)
    
    bot.send_message(message.chat.id, f'{b} + {c} = {result}')

@bot.message_handler(content_types=['text'])
def text_handler(message):
    text = message.text.lower()
    chat_id = message.chat.id
    if text == "Hi":
        bot.send_message(chat_id, "Hi, I'm useless.")
    else:
        bot.send_message(chat_id, 'Sorry, I did not get you :(')

bot.polling()
Answered By: furas
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.