B
B
BadCats2021-10-16 17:37:33
Python
BadCats, 2021-10-16 17:37:33

Aiogram FSM not changing state?

There are states:

class FSM_states(StatesGroup):
    start_chat_state=State()
    leave_chat_state=State()
    see_event_state=State()
    wait_chat_msg_state=State()
    resend_to_owner_state=State()


Functions:
async def start_chat_handler(message: types.Message,state: FSMContext):
    pass

async def leave_chat_handler(message: types.Message, state: FSMContext):
    pass
async def see_event_handler(message: types.Message, state: FSMContext):
    #some code

async def wait_chat_msg_handler(message: types.Message, state: FSMContext):
    pass

async def resend_to_owner_handler(message: types.Message, state: FSMContext):
    pass


And the binding code:
async def start_fsm(dsip:Dispatcher,states:dict,state):
    for s in states:
        dsip.register_message_handler(states[s],state=s)
    await  state.set_state(FSM_states.start_chat_state)

Where states -
fsm_states_dist={
    FSM_handlers.FSM_states.leave_chat_state: FSM_handlers.leave_chat_handler,
    FSM_handlers.FSM_states.start_chat_state:FSM_handlers.see_event_handler,
    FSM_handlers.FSM_states.wait_chat_msg_state:FSM_handlers.wait_chat_msg_handler,
    FSM_handlers.FSM_states.start_chat_state:FSM_handlers.start_chat_handler}

state-
state = bot_dispatcher.current_state(user=usr_msg.from_user.id)

The problem is that after the line has completed
await  state.set_state(FSM_states.start_chat_state)
- the method previously bound in the loop is not called.

even if you write like this:
import aiogram.types as types
from aiogram.dispatcher import FSMContext
FSM_handlers.FSM_states.start_chat_state.set()
@bot_dispatcher.message_handler(state=FSM_handlers.FSM_states.start_chat_state)
async def test_m(message: types.Message, state: FSMContext):
    print('d')
- the method is not called

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LXSTVAYNE, 2021-10-16
@lxstvayne

You can't just state the FSM like you did in the last example. It must be changed in the context of the event handler. Try to catch any state (state='*') and change the state in the namespace of the processing function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question