无法使用set_state函数设置状态

问题描述 投票:0回答:1

回调函数响应按下内联按钮,但在进入任何 state.set_state 块后它不起作用

@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())
显示
await print(await state.get_state()) TypeError: NoneType object cannot be used in expect expression

我希望看到 Form.user 或类似的东西 如果您需要这些状态的处理程序,请告诉我

python callback state fsm aiogram
1个回答
0
投票

您需要在装饰器中设置状态。 例如:

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

所以在你的情况下是:

@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')
© www.soinside.com 2019 - 2024. All rights reserved.