FsmContextMiddleware
extends BaseMiddleware
in package
Dispatcher-side middleware that resolves the FSM context for each incoming update and injects `state`, `raw_state`, and `fsm_storage` into the kwarg bag so that handlers and filters can access FSM state via reflection binding.
Mirrors aiogram.fsm.middleware.FSMContextMiddleware
(aiogram/fsm/middleware.py).
Upstream bugfix (https://github.com/aiogram/aiogram/issues/1317): the raw state is loaded after the event-isolation lock is acquired, not before. This prevents two concurrent fibers from reading a stale state value before the other fiber has finished updating it.
Key conventions (snake_case matching upstream wire keys):
STATE_KEY='state'RAW_STATE_KEY='raw_state'FSM_STORAGE_KEY='fsm_storage'
Table of Contents
Constants
- FSM_STORAGE_KEY : string = 'fsm_storage'
- RAW_STATE_KEY : string = 'raw_state'
- STATE_KEY : string = 'state'
Properties
Methods
- __construct() : mixed
- __invoke() : mixed
- close() : void
- Release all resources held by both the storage and the event-isolation strategy.
- getContext() : FsmContext
- Construct a fresh `FsmContext` bound to an explicit `StorageKey`.
- resolveContext() : FsmContext|null
- Build an `FsmContext` from raw ids, applying the configured strategy.
- resolveEventContext() : FsmContext|null
- Derive the FsmContext for the current update by reading the EventContext that `UserContextMiddleware` has already placed in `$data`.
Constants
FSM_STORAGE_KEY
public
string
FSM_STORAGE_KEY
= 'fsm_storage'
RAW_STATE_KEY
public
string
RAW_STATE_KEY
= 'raw_state'
STATE_KEY
public
string
STATE_KEY
= 'state'
Properties
$storage read-only
public
BaseStorage
$storage
$eventsIsolation read-only
private
BaseEventIsolation
$eventsIsolation
$strategy read-only
private
FsmStrategy
$strategy
= FsmStrategy::UserInChat
Methods
__construct()
public
__construct(BaseStorage $storage, BaseEventIsolation $eventsIsolation[, FsmStrategy $strategy = FsmStrategy::UserInChat ]) : mixed
Parameters
- $storage : BaseStorage
- $eventsIsolation : BaseEventIsolation
- $strategy : FsmStrategy = FsmStrategy::UserInChat
__invoke()
public
__invoke(callable(object, array<string, mixed>): mixed $handler, object $event, array<string, mixed> $data) : mixed
Parameters
- $handler : callable(object, array<string, mixed>): mixed
- $event : object
- $data : array<string, mixed>
close()
Release all resources held by both the storage and the event-isolation strategy.
public
close() : void
Mirrors FSMContextMiddleware.close (middleware.py:115-117).
getContext()
Construct a fresh `FsmContext` bound to an explicit `StorageKey`.
public
getContext(Bot $bot, int $chatId, int $userId[, int|null $threadId = null ][, string|null $businessConnectionId = null ][, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext
Mirrors FSMContextMiddleware.get_context (middleware.py:94-113).
Parameters
- $bot : Bot
- $chatId : int
- $userId : int
- $threadId : int|null = null
- $businessConnectionId : string|null = null
- $destiny : string = StorageKey::DEFAULT_DESTINY
-
Destiny tag; defaults to
StorageKey::DEFAULT_DESTINY.
Return values
FsmContextresolveContext()
Build an `FsmContext` from raw ids, applying the configured strategy.
public
resolveContext(Bot $bot, int|null $chatId, int|null $userId[, int|null $threadId = null ][, string|null $businessConnectionId = null ][, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext|null
Mirrors FSMContextMiddleware.resolve_context (middleware.py:62-92).
Fallback rules (upstream parity):
- If
$chatIdis null, use$userIdas a substitute. - If
$userIdis null and the strategy isChatorChatTopic, fall back to$chatIdfor the user slot. - If both are null after fallbacks, return
null.
Parameters
- $bot : Bot
- $chatId : int|null
- $userId : int|null
- $threadId : int|null = null
- $businessConnectionId : string|null = null
- $destiny : string = StorageKey::DEFAULT_DESTINY
-
Destiny tag; defaults to
StorageKey::DEFAULT_DESTINY.
Return values
FsmContext|nullresolveEventContext()
Derive the FsmContext for the current update by reading the EventContext that `UserContextMiddleware` has already placed in `$data`.
public
resolveEventContext(Bot $bot, array<string, mixed> $data[, string $destiny = StorageKey::DEFAULT_DESTINY ]) : FsmContext|null
Falls through to resolveContext with the appropriate ids.
Parameters
- $bot : Bot
- $data : array<string, mixed>
- $destiny : string = StorageKey::DEFAULT_DESTINY