peyk.fsm¶
Finite-state conversation support for Peyk.
- class peyk.fsm.BaseEventIsolation[source]¶
Bases:
objectSerialize handlers that share one
StorageKey.- lock(key: StorageKey) AsyncIterator[None][source]¶
Return an async context manager for the event lock.
- class peyk.fsm.BaseStorage(*, key_builder: KeyBuilder | None = None)[source]¶
Bases:
ABCAbstract asynchronous FSM storage keyed by
StorageKey.- abstractmethod async get_data(key: StorageKey | str) dict[str, object][source]¶
Retrieves data from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- abstractmethod async get_state(key: StorageKey | str) str | None[source]¶
Retrieves state from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- abstractmethod async set_data(key: StorageKey | str, data: Mapping[str, object]) None[source]¶
Updates data through the FSM API.
- Parameters:
key – Value used by this operation.
data – Value used by this operation.
- abstractmethod async set_state(key: StorageKey | str, state: str | None) None[source]¶
Updates state through the FSM API.
- Parameters:
key – Value used by this operation.
state – Value used by this operation.
- abstractmethod async update_data(key: StorageKey | str, data: Mapping[str, object] | None = None, **kwargs: object) dict[str, object][source]¶
Performs the update data operation for the FSM client.
- Parameters:
key – Value used by this operation.
data – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- peyk.fsm.build_storage_key(event: object, *, platform: str, bot_id: int | str, strategy: str | FSMStrategy = FSMStrategy.USER_IN_CHAT, destiny: str = 'default') StorageKey[source]¶
Build a platform/bot-aware key from a normalized event.
- peyk.fsm.conversation_key(event: object, *, platform: str | None = None, bot_id: int | str | None = None) StorageKey[source]¶
Return a compatibility
StorageKeyfor an event.The old form inferred the platform from
event.rawand had no bot ID. That form is deprecated; dispatcher integrations must pass both values.
- class peyk.fsm.DefaultKeyBuilder(prefix: str = 'peyk', separator: str = ':', with_bot_id: bool = True, with_destiny: bool = True)[source]¶
Bases:
objectBuild aiogram-shaped FSM keys with platform and bot namespaces.
The default is equivalent to aiogram’s
with_bot_id=Trueplus the additional platform segment required by Peyk’s multi-platform contract.- prefix: str¶
- separator: str¶
- with_bot_id: bool¶
- with_destiny: bool¶
- build(key: StorageKey, part: str | None = None) str[source]¶
Performs the build operation for the FSM client.
- Parameters:
key – Value used by this operation.
part – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.DisabledEventIsolation[source]¶
Bases:
BaseEventIsolationDo not serialize events.
- lock(key: StorageKey) AsyncIterator[None][source]¶
Performs the lock operation for the FSM client.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.FSMContext(storage: BaseStorage, key: StorageKey)[source]¶
- class peyk.fsm.FSMContext(event: object, storage: BaseStorage | LegacyStringStorage, *, platform: str | None = None, bot_id: int | str | None = None)
Bases:
objectRead and mutate state/data for one
StorageKey.- classmethod for_event(event: object, storage: BaseStorage, *, platform: str, bot_id: int | str, strategy: str | FSMStrategy = FSMStrategy.USER_IN_CHAT, destiny: str = 'default') FSMContext[source]¶
Create a context for a normalized event using explicit identity data.
- class peyk.fsm.FSMStrategy(*values)[source]¶
Bases:
str,EnumSelect which event identities share one FSM conversation.
- USER_IN_CHAT = 'USER_IN_CHAT'¶
- CHAT = 'CHAT'¶
- GLOBAL_USER = 'GLOBAL_USER'¶
- class peyk.fsm.KeyBuilder(*args, **kwargs)[source]¶
Bases:
ProtocolBuild backend keys from a
StorageKey.- build(key: StorageKey, part: str | None = None) str[source]¶
Performs the build operation for the FSM client.
- Parameters:
key – Value used by this operation.
part – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.LegacyStorageAdapter(storage: LegacyStringStorage, *, key_builder: KeyBuilder | None = None)[source]¶
Bases:
BaseStorageAdapt the pre-Phase-8 string-key storage API to
StorageKey.- async get_data(key: StorageKey | str) dict[str, object][source]¶
Retrieves data from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- async get_state(key: StorageKey | str) str | None[source]¶
Retrieves state from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- async set_data(key: StorageKey | str, data: Mapping[str, object]) None[source]¶
Updates data through the FSM API.
- Parameters:
key – Value used by this operation.
data – Value used by this operation.
- async set_state(key: StorageKey | str, state: str | None) None[source]¶
Updates state through the FSM API.
- Parameters:
key – Value used by this operation.
state – Value used by this operation.
- async update_data(key: StorageKey | str, data: Mapping[str, object] | None = None, **kwargs: object) dict[str, object][source]¶
Performs the update data operation for the FSM client.
- Parameters:
key – Value used by this operation.
data – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.LegacyStringStorage(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol for the pre-Phase-8 string-key storage API.
- async get_data(key: str) dict[str, object][source]¶
Retrieves data from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- async get_state(key: str) str | None[source]¶
Retrieves state from the FSM API.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- async set_data(key: str, data: Mapping[str, object]) None[source]¶
Updates data through the FSM API.
- Parameters:
key – Value used by this operation.
data – Value used by this operation.
- class peyk.fsm.MemoryStorage(*, key_builder: KeyBuilder | None = None)[source]¶
Bases:
BaseStorageStore FSM state and data in process memory.
- async get_data(key: StorageKey | str) dict[str, object][source]¶
Return a defensive copy of conversation data.
- async get_state(key: StorageKey | str) str | None[source]¶
Return the stored state for
key.
- async set_data(key: StorageKey | str, data: Mapping[str, object]) None[source]¶
Replace conversation data with a defensive copy.
- async set_state(key: StorageKey | str, state: str | None) None[source]¶
Set or clear the stored state.
- async update_data(key: StorageKey | str, data: Mapping[str, object] | None = None, **kwargs: object) dict[str, object][source]¶
Merge mapping and keyword values into conversation data.
- class peyk.fsm.RedisEventIsolation(redis: RedisLockClient, *, key_builder: KeyBuilder | None = None, lock_timeout: float | None = 30.0, blocking_timeout: float | None = None)[source]¶
Bases:
BaseEventIsolationSerialize events using Redis distributed locks.
- lock(key: StorageKey) AsyncIterator[None][source]¶
Performs the lock operation for the FSM client.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.RedisStorage(redis: AsyncRedisClient | None = None, *, state_ttl: int | float | None = None, data_ttl: int | float | None = None, key_builder: KeyBuilder | None = None, url: str | None = None, prefix: str | None = None)[source]¶
Bases:
BaseStoragePersist FSM state/data in Redis with independent optional TTLs.
- async get_data(key: StorageKey | str) dict[str, object][source]¶
Decode JSON conversation data from Redis.
- async get_state(key: StorageKey | str) str | None[source]¶
Return a stored state string, if present.
- async set_data(key: StorageKey | str, data: Mapping[str, object]) None[source]¶
Replace JSON conversation data and apply
data_ttl.
- async set_state(key: StorageKey | str, state: str | None) None[source]¶
Set or clear state, applying
state_ttlwhen configured.
- async update_data(key: StorageKey | str, data: Mapping[str, object] | None = None, **kwargs: object) dict[str, object][source]¶
Read, merge and persist conversation data.
- class peyk.fsm.SimpleEventIsolation[source]¶
Bases:
BaseEventIsolationSerialize by key and evict idle locks after release.
- lock(key: StorageKey) AsyncIterator[None][source]¶
Performs the lock operation for the FSM client.
- Parameters:
key – Value used by this operation.
- Returns:
Result produced by the FSM operation.
- class peyk.fsm.State(state: str | None = None, group_name: str | None = None)[source]¶
Bases:
objectDescribe one named FSM state, optionally with an explicit group name.
- property group: type[StatesGroup]¶
Return the owning state-group class.
- set_parent(group: type[StatesGroup]) None[source]¶
Bind this state to a
StatesGroupclass.
- property state: str | None¶
Return the fully-qualified state name.
- class peyk.fsm.StatesGroup[source]¶
Bases:
objectBase class for declarative FSM state groups.
- classmethod get_root() type[StatesGroup][source]¶
Return the root group for a nested group.
- class peyk.fsm.StorageKey(platform: str, bot_id: int | str, chat_id: int | str | None, user_id: int | str | None, destiny: str = 'default')[source]¶
Bases:
objectIdentify one FSM namespace without embedding a bot token.
platformis deliberately part of the identity so equal numeric IDs on Telegram and Bale cannot share state.bot_idisolates two bots on the same platform.- platform: str¶
- bot_id: int | str¶
- chat_id: int | str | None¶
- user_id: int | str | None¶
- destiny: str¶
Modules
FSM context and event-to-storage-key compatibility helpers. |
|
Dispatcher middleware that injects FSM context and event isolation. |
|
Declarative FSM states compatible with aiogram 3 semantics. |
|
FSM storage backends and event isolation. |