Using bot.register_next_step_handler in telegram bot I get TypeError: 'NoneType' object is not callable

Using bot.register_next_step_handler in telegram bot I get TypeError: 'NoneType' object is not callable

想法是让电报机器人从用户那里接收一些数据,处理它,显示结果,然后重复整个循环而不等待用户的命令。这是代码:

from telebot import TeleBot

TOKEN = ""

bot = TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def regata(message):
    bot.send_message(message.chat.id, 'Enter tmfs separated with space: ')
    bot.register_next_step_handler(message, bla)


def bla(message):
    data_from_tg = message.text.split()
    tmf_1 = data_from_tg[0]
    tmf_2 = data_from_tg[1]
    bot.send_message(message.chat.id, f'Result: {tmf_1 + tmf_2}')
    bot.register_next_step_handler(message, regata(message))


bot.polling()

所以机器人正在工作,但不幸的是,在将第二个结果发送给用户后,它失败了:

Traceback (most recent call last):
  File "D:\python\bot\testbot.py", line 21, in <module>
    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

我是否需要使用其他一些 pytelegrambotapi 方法来实现这个想法?为什么两次都成功了,最后还是失败了? 预先感谢您的任何想法。

试试这个:

@bot.message_handler(commands=['start'])
def regata(message):
    msg = bot.send_message(message.chat.id, 'Enter tmfs separated with space: ')
    bot.register_next_step_handler(msg, bla)


def bla(message):
    data_from_tg = message.text.split()
    tmf_1 = data_from_tg[0]
    tmf_2 = data_from_tg[1]
    msg = bot.send_message(message.chat.id, f'Result: {tmf_1 + tmf_2}')
    regata(message)


bot.polling()

反正我建议不要用register_next_step_handler。您可以通过这种方式轻松地做同样的事情:

@bot.message_handler(commands=['start'])
def regata(message):
    bot.send_message(message.chat.id, 'Enter tmfs separated with space: ')

@bot.message_handler():
def bla(message):
    data_from_tg = message.text.split()
    tmf_1 = data_from_tg[0]
    tmf_2 = data_from_tg[1]
    bot.send_message(message.chat.id, f'Result: {tmf_1 + tmf_2}')
    regata(message)

bot.polling()