mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-05 23:34:42 +00:00
docs: add documentation for changing state of another user in FSM middleware (#1676)
This commit is contained in:
parent
eb84458ff5
commit
7a9ecdfbd9
2 changed files with 32 additions and 0 deletions
1
CHANGES/1633.doc.rst
Normal file
1
CHANGES/1633.doc.rst
Normal file
|
|
@ -0,0 +1 @@
|
|||
Added documentation for changing state of another user in FSM
|
||||
|
|
@ -89,6 +89,37 @@ Complete example
|
|||
:linenos:
|
||||
|
||||
|
||||
Changing state for another user
|
||||
-------------------------------
|
||||
|
||||
In some cases, you might need to change the state for a user other than the one who triggered the current handler.
|
||||
For example, you might want to change the state of a user based on an admin's command.
|
||||
|
||||
To do this, you can use the ``get_context`` method of the FSM middleware through the dispatcher:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@example_router.message(Command("example"))
|
||||
async def command_example(message: Message, dispatcher: Dispatcher, bot: Bot):
|
||||
user_id = ... # Get the user ID in the way that you need
|
||||
state = await dispatcher.fsm.get_context(
|
||||
bot=bot,
|
||||
chat_id=user_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# Now you can use the state context to change the state for the specified user
|
||||
await state.set_state(YourState.some_state)
|
||||
|
||||
# Or store data in the state
|
||||
await state.update_data(some_key="some_value")
|
||||
|
||||
# Or clear the state
|
||||
await state.clear()
|
||||
|
||||
This allows you to manage the state of any user in your bot, not just the one who triggered the current handler.
|
||||
|
||||
|
||||
Read more
|
||||
=========
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue