Custom Backends

Implement your own FSM storage backend by subclassing BaseStorage.

BaseStorage Interface

from pyrogram_patch.fsm.storages.base_storage import BaseStorage

class PostgresStorage(BaseStorage):
    async def get_state(self, key):
        """Return the current state string or None."""
        ...

    async def set_state(self, key, state):
        """Set the state for a key."""
        ...

    async def get_data(self, key):
        """Return the stored data dict."""
        ...

    async def set_data(self, key, data):
        """Set the data dict for a key."""
        ...

    async def finish(self, key):
        """Clear state and data for a key."""
        ...

    async def close(self):
        """Cleanup connections on shutdown."""
        ...

Key Format

The key parameter is a tuple of (chat_id: int, user_id: int). Your storage must handle this key format consistently.

Registering

storage = PostgresStorage(dsn="postgresql://...")
manager.set_storage(storage)