A
A
Alexander Karadiaur2021-09-09 10:44:22
Python
Alexander Karadiaur, 2021-09-09 10:44:22

bot.register_next_step_handler doesn't work. How to fix?

After entering data for the phone number, it gives an error
Code:

user = {
    'name': ' ',
    'phone': ' ',
    'email': ' ',
}


@bot.message_handler(commands=['start'])
def name(msg):
    bot.send_message(msg.chat.id, 'Введите ФИО')

    @bot.message_handler(content_types=['text'])
    def data_name(msg):
        user['name'] = msg.text
        bot.register_next_step_handler(msg, phone(msg))


def phone(msg):
    bot.send_message(msg.chat.id, 'Введите номер телефона')

    @bot.message_handler(content_types=['text'])
    def data_phone(msg):
        user['phone'] = msg.text
        bot.register_next_step_handler(msg, email(msg))


def email(msg):
    bot.send_message(msg.chat.id, 'Введите Email')

    @bot.message_handler(content_types=['text'])
    def data_email(msg):
        user['email'] = msg.text
        bot.register_next_step_handler(msg, send_data(msg))


def send_data(msg):
    bot.send_message(
        msg.chat.id, f"ФИО: {user['name']}, телефон: {user['phone']}, email: {user['email']}")


Mistake:
Traceback (most recent call last):
  File "C:\Users\sasha\Desktop\проекты\боевая техничка\bot.py", line 44, in <module>
    bot.polling()
  File "C:\Users\sasha\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 619, in polling
    self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "C:\Users\sasha\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 678, in __threaded_polling
    raise e
  File "C:\Users\sasha\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\__init__.py", line 641, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "C:\Users\sasha\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 130, in raise_exceptions
    raise self.exception_info
  File "C:\Users\sasha\AppData\Local\Programs\Python\Python39\lib\site-packages\telebot\util.py", line 82, in run
    task(*args, **kwargs)
TypeError: 'NoneType' object is not callable

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Karadiaur, 2021-09-09
@twiwter

I solved the problem by rewriting the code a bit. It was necessary to remove the lines @bot.message_handler(content_types=['text']). they are of no use here. Here is the actual code:

user_data= {
    'name': '',
    'phone': '',
    'email': '',
    'message': ''
}

@bot.message_handler(commands=['start', 'go'])
def start_handler(message):
    msg = bot.send_message(message.chat.id, 'Введите Ваше ФИО')
    bot.register_next_step_handler(msg, user_phone)


def user_phone(message):
    user_data['name'] = message.text
    msg = bot.send_message(message.chat.id, 'Введите свой номер телефона')
    bot.register_next_step_handler(msg, user_email)

def user_email(message):
    user_data['phone'] = message.text
    msg = bot.send_message(message.chat.id, 'Введите Вашу эл. почту')
    bot.register_next_step_handler(msg, user_message)

def user_message(message):
    user_data['email'] = message.text
    msg = bot.send_message(message.chat.id, 'Введите ваше сообщение')
    bot.register_next_step_handler(msg, getresults)

def getresults(message):
    user_data['message'] = message.text
    bot.send_message(message.chat.id, f"ФИО: {user_data['name']} \nТел: {user_data['phone']} \nПочта: {user_data['email']} \nСообщение: {user_data['message']}")
    bot.clear_step_handler(message)

6139e3ace61ff086220998.png

M
Mikhail Krostelev, 2021-09-09
@twistfire92

very strange code.
Specifically for your question. register_next_step_handler
is passed a message and a function object, not the result of its work. Below are the same errors. Pull the handlers out of the body of the functions, otherwise nothing will work properly. the send_data function refers to the global user, which is declared at the very beginning. Do you understand that this variable exists while the bot is running and each user will overwrite it with their own data? Learn the basics, then get back to bots. You wrote a lot of redundant and broken code
bot.register_next_step_handler(msg, phone)

M
mokrievqa, 2021-12-24
@mokrievqa

Also a problem with register_next_step_handler - the same error takes off. The main idea of ​​the code is to collect data, produce a result and immediately start again. The bot goes to the second round, but after the first data input it crashes with the error TypeError: 'NoneType' object is not callable.
Here is the code:

from telebot import TeleBot

TOKEN = "" #token was removed, but I use it on myside

bot = TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def start(message):
    bot.send_message(message.chat.id, 'Enter var1: ')
    bot.register_next_step_handler(message, tmf1)

def tmf1(message):
    var1 = message.text
    bot.send_message(message.chat.id, 'Enter var2: ')
    bot.register_next_step_handler(message, tmf2, var1)

def tmf2(message, var1):
    var2 = message.text
    bot.send_message(message.chat.id, f"String sum =  {var1 + var2} !")
    bot.register_next_step_handler(message, start(message))


bot.polling()

Here is the result:
61c5dd760b7c1313933863.jpeg
Here is the error:
Traceback (most recent call last):
File "D:\python\course\teletest.py", line 24, in
bot.polling()
File "C:\Python39\lib\site-packages\ telebot\__init__.py", line 664, in polling
self.__threaded_polling(non_stop, interval, timeout, long_polling_timeout, allowed_updates)
File "C:\Python39\lib\site-packages\telebot\__init__.py", line 726, in __threaded_polling
raise e
File "C:\Python39\lib\site-packages\telebot\__init__.py", line 686, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Python39\lib\site-packages\telebot\ util.py", line 135, in raise_exceptions
raise self.exception_info
File "C:\Python39\lib\site-packages\telebot\util.py", line 87, in run
task(*args, **kwargs)
TypeError: 'NoneType' object is not callable
Process finished with exit code 1
Error crashes immediately after the bot sends 'Enter var2: ' .
Please tell me how this can be corrected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question