'Can't change a state using Aiogram python
I need to change state in callback handled
@dp.callback_query_handler(text="ru", state=States.lang)
async def change_lang_ru(call: types.CallbackQuery, state: FSMContext):
global menu_keyboard
async with state.proxy() as data:
data['lang'] = 'ru'
await call.answer(text="Язык поменялся", show_alert=True)
print(States.all())
await States.menu.set()
Actually no error appears but next state doesn't start
INFO:aiogram.contrib.middlewares.logging:Received callback query [ID:1631380781948190838] from user [ID:379835437] for message [ID:123] in chat [private:379835437] with data: ru originally posted by user [ID:5151482169]
INFO:aiogram.contrib.middlewares.logging:Unhandled callback query [ID:1631380781948190838] from user [ID:379835437] for message [ID:123] in chat [private:379835437] with data: ru originally posted by user [ID:5151482169]
INFO:aiogram.contrib.middlewares.logging:Process update [ID:41723130]: [success] (in 6 ms)
This is how my next state is supposed to start
@dp.message_handler(state=States.menu)
async def first_test_state_case_met(message: types.Message):
await message.reply('First!', reply=False)
And these are my states
class States(StatesGroup):
lang = State()
menu = State()
I could not find any answers on this in documentation
Solution 1:[1]
In Your example i have an error here:
print(States.all())
AttributeError: type object 'States' has no attribute 'all'
@dp.callback_query_handler(text="ru", state=States.lang)
async def change_lang_ru(call: types.CallbackQuery, state: FSMContext):
global menu_keyboard
async with state.proxy() as data:
data['lang'] = 'ru'
await call.answer(text="???? ?????????", show_alert=True)
# print(States.all())
await States.menu.set()
except this, all other states works, but after States changed to menu state:
await States.menu.set()
you should send message to see the response in the next function, because your next function is a message handler, so it expects a message from you
@dp.message_handler(state=States.menu)
async def first_test_state_case_met(message: types.Message):
await message.reply('First!', reply=False)
And don't forget to finish your state later
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 | Maksim K. |