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)
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
# 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
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 -我们希望将用户带到的状态