为什么 Aiogram FSM 中的功能不起作用?



我需要帮助,我想让我的函数在状态机中工作。当我运行代码时,它到达了"输入检查"的时刻。我进去了,什么也没发生。没有状态机,一切都可以工作,我希望函数接受消息的文本,然后处理它并显示结果。提前感谢!

from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram import types, Dispatcher
from create_bot import dp, bot
from func import function # this function itself
from keyboard import func_kb

async def start_command(message : types.Message):
await message.answer("Greetings! Select the function you need.", reply_markup=func_kb)

class Form(StatesGroup):
seturl = State()

@dp.message_handler(commands=['Test'])
async def test(message: types.Message):
await message.reply('Enter to check:')
await Form.seturl.set()

@dp.message_handler(state=Form.seturl)
async def monet(message : types.Message, state: FSMContext):
get_result = function(message.text) #get value from function
if get_result == "true":
await message.answer("Yes")
else:
await message.answer('Not')
await state.finish()


def register_handlers(dp : Dispatcher):
dp.register_message_handler(start_command, commands=['start'])

我尝试在本地运行您的代码,它为我工作。我可以假设您没有初始化处理程序或初始化错误。function不返回任何值,或者它返回但不返回布尔值。

我还注意到你有"true"写在if语句中,如果你的function返回return True or False,那么很可能这是一个错误。你需要这样写:

if get_result:
await message.answer("Yes")
else:
await message.answer('Not')

最新更新