Storages#

Storages out of the box#

MemoryStorage#

class aiogram.fsm.storage.memory.MemoryStorage[source]#

Default FSM storage, stores all data in dict and loss everything on shutdown

Warning

Is not recommended using in production in due to you will lose all data when your bot restarts

__init__() None[source]#

RedisStorage#

class aiogram.fsm.storage.redis.RedisStorage(redis: redis.asyncio.client.Redis, key_builder: Optional[aiogram.fsm.storage.redis.KeyBuilder] = None, state_ttl: Optional[Union[float, datetime.timedelta]] = None, data_ttl: Optional[Union[float, datetime.timedelta]] = None)[source]#

Redis storage required aioredis package installed (pip install aioredis)

__init__(redis: redis.asyncio.client.Redis, key_builder: Optional[aiogram.fsm.storage.redis.KeyBuilder] = None, state_ttl: Optional[Union[float, datetime.timedelta]] = None, data_ttl: Optional[Union[float, datetime.timedelta]] = None) 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

  • lock_kwargs – Custom arguments for Redis lock

classmethod from_url(url: str, connection_kwargs: Optional[Dict[str, Any]] = None, **kwargs: Any) aiogram.fsm.storage.redis.RedisStorage[source]#

Create an instance of RedisStorage with specifying the connection string

Parameters
  • url – for example redis://user:password@host:port/db

  • connection_kwargs – see aioredis docs

  • kwargs – arguments to be passed to RedisStorage

Returns

an instance of RedisStorage

Keys inside storage can be customized via key builders:

class aiogram.fsm.storage.redis.KeyBuilder[source]#

Base class for Redis key builder

abstract build(key: aiogram.fsm.storage.base.StorageKey, part: Literal['data', 'state', 'lock']) str[source]#

This method should be implemented in subclasses

Parameters
  • key – contextual key

  • part – part of the record

Returns

key to be used in Redis queries

class aiogram.fsm.storage.redis.DefaultKeyBuilder(*, prefix: str = 'fsm', separator: str = ':', with_bot_id: bool = False, with_destiny: bool = False)[source]#

Simple Redis key builder with default prefix.

Generates a colon-joined string with prefix, chat_id, user_id, optional bot_id and optional destiny.

build(key: aiogram.fsm.storage.base.StorageKey, part: Literal['data', 'state', 'lock']) str[source]#

This method should be implemented in subclasses

Parameters
  • key – contextual key

  • part – part of the record

Returns

key to be used in Redis queries

Writing own storages#

class aiogram.fsm.storage.base.BaseStorage[source]#

Base class for all FSM storages

abstract async set_state(bot: aiogram.client.bot.Bot, key: aiogram.fsm.storage.base.StorageKey, state: Optional[Union[str, aiogram.fsm.state.State]] = None) None[source]#

Set state for specified key

Parameters
  • bot – instance of the current bot

  • key – storage key

  • state – new state

abstract async get_state(bot: aiogram.client.bot.Bot, key: aiogram.fsm.storage.base.StorageKey) Optional[str][source]#

Get key state

Parameters
  • bot – instance of the current bot

  • key – storage key

Returns

current state

abstract async set_data(bot: aiogram.client.bot.Bot, key: aiogram.fsm.storage.base.StorageKey, data: Dict[str, Any]) None[source]#

Write data (replace)

Parameters
  • bot – instance of the current bot

  • key – storage key

  • data – new data

abstract async get_data(bot: aiogram.client.bot.Bot, key: aiogram.fsm.storage.base.StorageKey) Dict[str, Any][source]#

Get current data for key

Parameters
  • bot – instance of the current bot

  • key – storage key

Returns

current data

async update_data(bot: aiogram.client.bot.Bot, key: aiogram.fsm.storage.base.StorageKey, data: Dict[str, Any]) Dict[str, Any][source]#

Update date in the storage for key (like dict.update)

Parameters
  • bot – instance of the current bot

  • key – storage key

  • data – partial data

Returns

new data

abstract async close() None[source]#

Close storage (database connection, file or etc.)