lily_website

📜 Base Manager

⬅️ Back 🏠 Docs Root

This module defines the BaseStateManager class, which serves as a foundational component for managing FSM (Finite State Machine) states within the Telegram bot. It provides a structured way to store and retrieve feature-specific data within the Aiogram FSM context, ensuring data isolation and simplifying state management logic.

BaseStateManager Class

The BaseStateManager is an abstract manager designed to encapsulate the common operations for handling temporary data (drafts) associated with a particular feature.

Initialization (__init__)

def __init__(self, state: FSMContext, feature_key: str):

Initializes the BaseStateManager.

Key Action:

get_payload Method

async def get_payload(self) -> dict[str, Any]:

Asynchronously retrieves all data associated with the feature’s draft.

Returns: dict[str, Any]: A dictionary containing all the draft data for the feature. Returns an empty dictionary if no data is found for the storage_key.

update Method

async def update(self, **kwargs: Any) -> dict[str, Any]:

Asynchronously updates the feature’s draft with the provided key-value pairs. This method performs a partial update, merging new data with existing data.

Process:

  1. Retrieves the current draft data using get_payload().
  2. Updates the current data with the new kwargs.
  3. Saves the updated data back into the FSM under self.storage_key.

Returns: dict[str, Any]: The updated dictionary of the feature’s draft data.

clear Method

async def clear(self) -> None:

Asynchronously clears all draft data associated with the feature.

Process:

set_value Method

async def set_value(self, key: str, value: Any) -> None:

Asynchronously sets a specific key-value pair within the feature’s draft.

Process:

get_value Method

async def get_value(self, key: str, default: Any = None) -> Any:

Asynchronously retrieves a specific value from the feature’s draft.

Process:

  1. Retrieves the entire draft payload using get_payload().
  2. Returns the value associated with the key from the payload, or the default value if the key is not present.