可以使用AIPROMPOM PYTHON更改状态

发布于 2025-01-18 00:36:35 字数 1282 浏览 4 评论 0原文

我需要更改回调中的状态

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

实际上没有出现错误,但是下一个状态尚未开始,

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)

这是我下一个状态的开始

@dp.message_handler(state=States.menu)
async def first_test_state_case_met(message: types.Message):
    await message.reply('First!', reply=False)

,这些是我的状态,

class States(StatesGroup):
    lang = State()
    menu = State()

我在文档中找不到任何答案

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2025-01-25 00:36:36

在你的例子中我有一个错误:

print(States.all())

AttributeError:类型对象“States”没有属性“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()

除此之外,所有其他状态都有效,但在 States 更改为菜单状态后:

await States.menu.set()

您应该发送消息以在下一个函数中查看响应,因为您的下一个函数是消息处理程序,因此它需要您发送消息

@dp.message_handler(state=States.menu)
async def first_test_state_case_met(message: types.Message):
    await message.reply('First!', reply=False)

并且不要忘记稍后完成您的状态

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文