'Why doesn't the function in the Aiogram FSM work?
I need help, I want to make my function work in the state machine. When I run the code, it gets to the moment "Enter to check:". I enter and nothing happens. Without a state machine, everything works, I want the function to accept the text of the message and then process it and display the result. Thanks in advance!
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'])
Solution 1:[1]
I tried to run your code locally and it worked for me. I can assume that you did not initialize the handlers or did it incorrectly. Either function returns nothing, or it returns, but not boolean values.
And I also noticed that you have "true" written in the if statement, if your function returns return True or False, then most likely this is an error. You need to write like this:
if get_result:
await message.answer("Yes")
else:
await message.answer('Not')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DavidRomanov |
