docs: add documentation for changing state of another user in FSM middleware (#1676)

This commit is contained in:
Alex Root Junior 2025-04-26 15:56:19 +03:00 committed by GitHub
parent eb84458ff5
commit 7a9ecdfbd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

1
CHANGES/1633.doc.rst Normal file
View file

@ -0,0 +1 @@
Added documentation for changing state of another user in FSM

View file

@ -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
=========