首页 > 解决方案 > Aiogram -- 为特定用户设置状态

问题描述

我正在用 python 和 aiogram 编写一个机器人。关键是管理员接受(或拒绝)用户请求。因此,当管理员在他的聊天中单击按钮时,我需要更改用户的状态(他的 uid 是已知的)。我没有找到如何在任何地方做到这一点。

我正在寻找类似的东西

dp.set_state(uid, User.accepted))

谢谢!

标签: pythonpython-3.xtelegramtelegram-bot

解决方案


我有同样的问题

在基类 State 中找到方法 set():

class State:
    ...
    async def set(self):
        state = Dispatcher.get_current().current_state()
        await state.set_state(self.state)

所以我从 State 创建了新类并以这种方式覆盖了方法:

    async def set(self, user=None):
        """Option to set state for concrete user"""
        state = Dispatcher.get_current().current_state(user=user)
        await state.set_state(self.state)

用法:

@dp.message_handler(state='*')
async def example_handler(message: Message):
    await SomeStateGroup.SomeState.set(user=message.from_user.id)

如果您想要一些邮件内容,请收集用户 ID 并使用该提示。


推荐阅读