fsm — FSM Manager and Garbage Collector
BaseStateManager
BaseStateManager
Base FSM state manager for a feature (draft).
Isolates specific feature data under the draft:<feature_key> key
within the user's general FSM storage. This prevents collisions
between different features operating in the same FSM session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
FSMContext
|
FSM context of the current user. |
required |
feature_key
|
str
|
Unique feature key (e.g., |
required |
Example
manager = BaseStateManager(state, feature_key="booking")
await manager.update(date="2024-01-15", time="14:00")
payload = await manager.get_payload()
# {"date": "2024-01-15", "time": "14:00"}
await manager.clear()
Source code in src/codex_bot/fsm/state_manager.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
Functions
clear()
async
Completely removes the draft key from FSM storage.
Unlike resetting to {key: {}}, it completely removes the key,
leaving no empty "zombie dictionaries" in Redis.
Does not affect data of other features in the same FSM storage.
Source code in src/codex_bot/fsm/state_manager.py
73 74 75 76 77 78 79 80 | |
get_payload()
async
Returns all data of the feature draft.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with current data. Empty dictionary if no data exists. |
Source code in src/codex_bot/fsm/state_manager.py
43 44 45 46 47 48 49 50 51 | |
get_value(key, default=None)
async
Returns a specific value from the draft.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Field key. |
required |
default
|
Any
|
Default value if the key is not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Field value or default. |
Source code in src/codex_bot/fsm/state_manager.py
92 93 94 95 96 97 98 99 100 101 102 103 104 | |
set_value(key, value)
async
Sets one specific value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Field key. |
required |
value
|
Any
|
Value to save. |
required |
Source code in src/codex_bot/fsm/state_manager.py
82 83 84 85 86 87 88 89 90 | |
update(**kwargs)
async
Updates the draft with the passed fields (partial update).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Fields to update. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Updated dictionary with draft data. |
Example
await manager.update(name="Alice", age=30)
Source code in src/codex_bot/fsm/state_manager.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
GarbageStateRegistry
GarbageStateRegistry
Registry of FSM states where text messages are considered garbage.
Global registry: states are registered once at startup, the filter checks the user's current state with each message.
Supports registration of:
- a single State object
- an entire StatesGroup (all states in the group)
- a string (state name)
- a list / tuple / set of the above types
Example
GarbageStateRegistry.register(MyFeatureStates) # entire group
GarbageStateRegistry.register(MyFeatureStates.waiting) # single state
GarbageStateRegistry.register(["Feature1:main", "Feature2:step1"])
Source code in src/codex_bot/fsm/garbage_collector.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Functions
is_garbage(state_name)
classmethod
Checks if a state is registered as garbage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_name
|
str | None
|
String name of the current FSM state. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the state is registered as garbage. |
Source code in src/codex_bot/fsm/garbage_collector.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
register(state)
classmethod
Registers state(s) as garbage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State | StatesGroup | type[StatesGroup] | str | list[Any] | tuple[Any, ...] | set[Any]
|
State, StatesGroup, string, or an iterable of them. |
required |
Source code in src/codex_bot/fsm/garbage_collector.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
registered_states()
classmethod
Returns all registered garbage states (read-only).
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
Frozenset of string state names. |
Source code in src/codex_bot/fsm/garbage_collector.py
106 107 108 109 110 111 112 113 114 | |
IsGarbageStateFilter
IsGarbageStateFilter
Bases: Filter
Aiogram filter: True if the user's current FSM state is garbage.
Used in common_fsm_router for automatic deletion of unwanted text messages.
Example
@router.message(F.text, IsGarbageStateFilter())
async def delete_garbage(message: Message, state: FSMContext):
await message.delete()
Source code in src/codex_bot/fsm/garbage_collector.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
Functions
__call__(message, state)
async
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
Incoming message. |
required |
state
|
FSMContext
|
User's FSM context. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the current state is registered as garbage. |
Source code in src/codex_bot/fsm/garbage_collector.py
131 132 133 134 135 136 137 138 139 140 141 | |
common_fsm_router
A ready-to-use router with a garbage-collector handler. Connect it to the main dispatcher:
from codex_bot.fsm import common_fsm_router
dp.include_router(common_fsm_router)