gpt4 book ai didi

python - 在电报机器人中使用 bot.register_next_step_handler 我得到 TypeError : 'NoneType' object is not callable

转载 作者:行者123 更新时间:2023-12-05 05:50:35 26 4
gpt4 key购买 nike

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

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()

关于python - 在电报机器人中使用 bot.register_next_step_handler 我得到 TypeError : 'NoneType' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70502501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com