Can't set state using set_state function

39 views Asked by At

The callback function responds to pressing an inline button, but after entering any of the state.set_state blocks it does not work

@router.callback_query()
async def callback(call: CallbackQuery, state: FSMContext):
    match call.data:
        case 'first': 
            await state.set_state(Form.user)
            await call.message.answer('Введите пользователя следуя инструкциям')

        case 'second': 
            await state.set_state(Form.item_photo)
            await call.message.answer('Пришлите фото')
            
        case 'third': await call.message.answer('later')
        case 'back': await call.message.answer('later')

await print(await state.get_state()) shows await print(await state.get_state()) TypeError: NoneType object cannot be used in expect expression

I expected to see Form.user or something like that If you need handlers for these states, please let me know

1

There are 1 answers

0
say8hi On

You need to set state in the decorator. For example:

class Broadcast(StatesGroup):
    agree = State()


@router.callback_query(F.data == "set_state")
async def set_state_handler(call: CallbackQuery, state: FSMContext):
    await state.set_state(Broadcast.agree)
    await call.message.edit_text("You have agreed to broadcast!")


@router.callback_query(Broadcast.agree)
async def agree_to_broadcast(call: CallbackQuery, state: FSMContext):
    await call.message.edit_text("You have agreed to broadcast!")
    await state.clear()

So in your case it's:

@router.callback_query(Form.user)
async def callback(call: CallbackQuery, state: FSMContext):
    match call.data:
        case 'first': 
            await state.set_state(Form.user)
            await call.message.answer('Введите пользователя следуя инструкциям')

        case 'second': 
            await state.set_state(Form.item_photo)
            await call.message.answer('Пришлите фото')
            
        case 'third': await call.message.answer('later')
        case 'back': await call.message.answer('later')