python - Aiogram -- 为特定用户设置状态
问题描述
我正在用 python 和 aiogram 编写一个机器人。关键是管理员接受(或拒绝)用户请求。因此,当管理员在他的聊天中单击按钮时,我需要更改用户的状态(他的 uid 是已知的)。我没有找到如何在任何地方做到这一点。
我正在寻找类似的东西
dp.set_state(uid, User.accepted))
谢谢!
解决方案
我有同样的问题
在基类 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 并使用该提示。
推荐阅读
- php - 如何从 php 代码中的 php 代码访问 $nev 变量?
- java - 为什么我会在这段代码中得到 nullPointerException?
- c++ - 对 MasterController::StaticMetaObject 的未定义引用
- algorithm - 如何识别代码片段的 Big-O 表示法是否为对数时间 O(logn)?
- php - 为什么我的程序不显示用户名?
- python - 同时发送/接收消息套接字python
- python - TypeError:图像数据在 tf.image.per_image_standardization(x) 之后无法转换为浮点数
- java - 如何使用 OpenSAML 2 和 Java 测试/调试解密加密断言?
- java - 如何一次获取 100 条记录,直到使用 Spring JPA 的 oracle 中的所有记录?
- jenkins - 如何在詹金斯管道脚本中参数化诱惑结果目录?