Storages¶
Storages out of the box¶
MemoryStorage¶
RedisStorage¶
- class aiogram.fsm.storage.redis.RedisStorage(redis: ~redis.asyncio.client.Redis, key_builder: ~aiogram.fsm.storage.base.KeyBuilder | None = None, state_ttl: int | ~datetime.timedelta | None = None, data_ttl: int | ~datetime.timedelta | None = None, json_loads: ~collections.abc.Callable[[...], ~typing.Any] = <function loads>, json_dumps: ~collections.abc.Callable[[...], str] = <function dumps>)[source]¶
Redis storage requires the
redispackage (pip install redis)- __init__(redis: ~redis.asyncio.client.Redis, key_builder: ~aiogram.fsm.storage.base.KeyBuilder | None = None, state_ttl: int | ~datetime.timedelta | None = None, data_ttl: int | ~datetime.timedelta | None = None, json_loads: ~collections.abc.Callable[[...], ~typing.Any] = <function loads>, json_dumps: ~collections.abc.Callable[[...], str] = <function dumps>) None[source]¶
- Parameters:
redis – instance of Redis connection
key_builder – builder that helps to convert contextual key to string
state_ttl – TTL for state records
data_ttl – TTL for data records
- classmethod from_url(url: str, connection_kwargs: dict[str, Any] | None = None, **kwargs: Any) RedisStorage[source]¶
Create an instance of
RedisStoragewith the specified connection url- Parameters:
url – the connection url (i.e.
redis://user:password@host:port/db)connection_kwargs – see
redisdocskwargs – arguments passed to
RedisStorage
- Returns:
an instance of
RedisStorage
MongoStorage¶
- class aiogram.fsm.storage.pymongo.PyMongoStorage(client: AsyncMongoClient[Any], key_builder: KeyBuilder | None = None, db_name: str = 'aiogram_fsm', collection_name: str = 'states_and_data')[source]¶
MongoDB storage requires the
pymongopackage (pip install pymongo).- __init__(client: AsyncMongoClient[Any], key_builder: KeyBuilder | None = None, db_name: str = 'aiogram_fsm', collection_name: str = 'states_and_data') None[source]¶
- Parameters:
client – instance of AsyncMongoClient
key_builder – builder that helps to convert contextual key to string
db_name – name of the MongoDB database for FSM
collection_name – name of the collection for storing FSM states and data
- classmethod from_url(url: str, connection_kwargs: dict[str, Any] | None = None, **kwargs: Any) PyMongoStorage[source]¶
Create an instance of
PyMongoStoragewith the specified connection url- Parameters:
url – the connection url (i.e.
mongodb://user:password@host:port)connection_kwargs – see
pymongodocskwargs – arguments passed to
PyMongoStorage
- Returns:
an instance of
PyMongoStorage
- class aiogram.fsm.storage.mongo.MongoStorage(client: AsyncIOMotorClient, key_builder: KeyBuilder | None = None, db_name: str = 'aiogram_fsm', collection_name: str = 'states_and_data')[source]¶
Warning
DEPRECATED: Use
PyMongoStorageinstead. This class will be removed in future versions.MongoDB storage required
motorpackage installed (pip install motor)- __init__(client: AsyncIOMotorClient, key_builder: KeyBuilder | None = None, db_name: str = 'aiogram_fsm', collection_name: str = 'states_and_data') None[source]¶
- Parameters:
client – Instance of AsyncIOMotorClient
key_builder – builder that helps to convert contextual key to string
db_name – name of the MongoDB database for FSM
collection_name – name of the collection for storing FSM states and data
- classmethod from_url(url: str, connection_kwargs: dict[str, Any] | None = None, **kwargs: Any) MongoStorage[source]¶
Create an instance of
MongoStoragewith specifying the connection string- Parameters:
url – for example
mongodb://user:password@host:portconnection_kwargs – see
motordocskwargs – arguments to be passed to
MongoStorage
- Returns:
an instance of
MongoStorage
KeyBuilder¶
Keys inside Redis and Mongo storages can be customized via key builders:
- class aiogram.fsm.storage.base.DefaultKeyBuilder(*, prefix: str = 'fsm', separator: str = ':', with_bot_id: bool = False, with_business_connection_id: bool = False, with_destiny: bool = False)[source]¶
Simple key builder with default prefix.
Generates a colon-joined string with prefix, chat_id, user_id, optional bot_id, business_connection_id, destiny and field.
- Format:
<prefix>:<bot_id?>:<business_connection_id?>:<chat_id>:<user_id>:<destiny?>:<field?>
Writing own storages¶
- class aiogram.fsm.storage.base.BaseStorage[source]¶
Base class for all FSM storages
- abstract async set_state(key: StorageKey, state: str | State | None = None) None[source]¶
Set state for specified key
- Parameters:
key – storage key
state – new state
- abstract async get_state(key: StorageKey) str | None[source]¶
Get key state
- Parameters:
key – storage key
- Returns:
current state
- abstract async set_data(key: StorageKey, data: Mapping[str, Any]) None[source]¶
Write data (replace)
- Parameters:
key – storage key
data – new data
- abstract async get_data(key: StorageKey) dict[str, Any][source]¶
Get current data for key
- Parameters:
key – storage key
- Returns:
current data