我正在用python和aigram编写一个bot。关键是管理员接受(或拒绝)用户请求。因此,当管理员点击聊天中的按钮时,我需要更改用户的状态(他的uid是已知的)。我没有找到如何在任何地方做到这一点。我在找一个
dp.set_state(uid, User.accepted))
字符串谢谢你,谢谢
sz81bmfz1#
我有同样的问题在基类State中找到方法set():
class State: ... async def set(self): state = Dispatcher.get_current().current_state() await state.set_state(self.state)
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)
async def set(self, user=None):
"""Option to set state for concrete user"""
state = Dispatcher.get_current().current_state(user=user)
型使用方法:
@dp.message_handler(state='*')async def example_handler(message: Message): await SomeStateGroup.SomeState.set(user=message.from_user.id)
@dp.message_handler(state='*')
async def example_handler(message: Message):
await SomeStateGroup.SomeState.set(user=message.from_user.id)
型如果你想要一些邮件的东西,收集用户ID和使用提示。
camsedfj2#
from aiogram.dispatcher import FSMContext@dp.message_handler(state='*')async def example_handler(message: types.Message, state: FSMContext): new_state = FSMContext(storage, chat_id, user_id)
from aiogram.dispatcher import FSMContext
async def example_handler(message: types.Message, state: FSMContext):
new_state = FSMContext(storage, chat_id, user_id)
字符串然后set_state()和set_data()在new_state上。storage是FSM存储。
set_state()
set_data()
new_state
storage
3pvhb19x3#
对于新的Aiogram 3.1.0
由于Aiogram 3.1.0访问存储和上下文的方法略有不同,我想分享我的想法:
from aiogram.fsm.storage.base import StorageKey from aiogram.fsm.state import State, StatesGroup from aiogram.fsm.context import FSMContext class UserState(StatesGroup): # any other states EXPIRED = State() bot = Bot("your_token", parse_mode=ParseMode.HTML) storage = MemoryStorage() # for clarity it is created explicitly dp = Dispatcher(storage=storage) # here goes the desired userId and chatId (they are same in private chat) new_user_storage_key = StorageKey(bot.id,user_id,user_id) new_user_context = FSMContext(storage=storage,key=new_user_storage_key) await new_user_context.set_state(UserState.EXPIRED) # your specified state
from aiogram.fsm.storage.base import StorageKey
from aiogram.fsm.state import State, StatesGroup
from aiogram.fsm.context import FSMContext
class UserState(StatesGroup):
# any other states
EXPIRED = State()
bot = Bot("your_token", parse_mode=ParseMode.HTML)
storage = MemoryStorage() # for clarity it is created explicitly
dp = Dispatcher(storage=storage)
# here goes the desired userId and chatId (they are same in private chat)
new_user_storage_key = StorageKey(bot.id,user_id,user_id)
new_user_context = FSMContext(storage=storage,key=new_user_storage_key)
await new_user_context.set_state(UserState.EXPIRED) # your specified state
字符串用户自己的存储空间(MemoryRecord)现在是通过 * ESTKey * 访问的,而不仅仅是通过user_id访问的,这就是为什么需要先创建它,然后用值(ID)填充它,然后将它传递给FSM上下文。上下文创建后,调用set_state(...)和set_data(...)将专门为所需的user_id设置state/data。实际上,不需要像前面的代码片段中所示的那样显式地创建存储,因为它将自动创建,所以您可以通过dispatcher.storage属性访问它,如下所示:
# the same imports # the same UserState class bot = Bot("your_token", parse_mode=ParseMode.HTML) dp = Dispatcher() # we don't create storage manually this time # here goes the desired userId and chatId (they are same in private chat) new_user_storage_key = StorageKey(bot.id,user_id,user_id) new_user_context = FSMContext(storage=dp.storage,key=new_user_storage_key) await new_user_context.set_state(UserState.EXPIRED) # your specified state
# the same imports
# the same UserState class
dp = Dispatcher() # we don't create storage manually this time
new_user_context = FSMContext(storage=dp.storage,key=new_user_storage_key)
型希望有帮助!:)
qybjjes14#
dp -Dispatcher类的一个对象chat_id - chat id,如果这是与用户的通信,则必须等于用户id User.accepted -我们希望将用户带到的状态
4条答案
按热度按时间sz81bmfz1#
我有同样的问题
在基类State中找到方法set():
字符串
所以我从State创建了一个新的类,并以这种方式覆盖了方法:
型
使用方法:
型
如果你想要一些邮件的东西,收集用户ID和使用提示。
camsedfj2#
字符串
然后
set_state()
和set_data()
在new_state
上。storage
是FSM存储。3pvhb19x3#
对于新的Aiogram 3.1.0
由于Aiogram 3.1.0访问存储和上下文的方法略有不同,我想分享我的想法:
字符串
用户自己的存储空间(MemoryRecord)现在是通过 * ESTKey * 访问的,而不仅仅是通过user_id访问的,这就是为什么需要先创建它,然后用值(ID)填充它,然后将它传递给FSM上下文。上下文创建后,调用set_state(...)和set_data(...)将专门为所需的user_id设置state/data。
实际上,不需要像前面的代码片段中所示的那样显式地创建存储,因为它将自动创建,所以您可以通过dispatcher.storage属性访问它,如下所示:
型
希望有帮助!:)
qybjjes14#
state = dp.current_state(chat=chat_id,user=user_id)await state.set_state(User.accepted)
dp -Dispatcher类的一个对象chat_id - chat id,如果这是与用户的通信,则必须等于用户id User.accepted -我们希望将用户带到的状态