| ⬅️ Back | 🏠Docs Root |
This module defines the FeatureDiscoveryService class, which is responsible for dynamically discovering and registering various configurations and components of features within the Telegram bot application. It supports both UI-oriented (Telegram) and background (Redis) features, promoting a modular and extensible architecture.
FeatureDiscoveryService ClassThe FeatureDiscoveryService automates the process of integrating new features into the bot by scanning predefined paths for feature_setting.py files and extracting relevant configurations, such as menu buttons, FSM states for garbage collection, and Redis Stream handlers.
__init__)def __init__(self) -> None:
Initializes the FeatureDiscoveryService.
Key Action:
self._loaded_features as an empty set, which can be used to track successfully loaded features (though not explicitly used for that purpose in the current methods).discover_all Methoddef discover_all(self) -> None:
Executes a full discovery cycle, scanning all features listed in INSTALLED_FEATURES and INSTALLED_REDIS_FEATURES for their respective configurations.
Process:
INSTALLED_FEATURES:
_discover_menu() to find and process menu configurations._discover_garbage_states() to register FSM states for garbage collection.INSTALLED_REDIS_FEATURES:
_discover_redis_handlers() to include Redis Stream routers._discover_garbage_states() to register FSM states for garbage collection.create_feature_orchestrators Methoddef create_feature_orchestrators(self, container: "BotContainer") -> dict[str, Any]:
Creates and returns a dictionary of orchestrator instances for all discovered features. It handles naming conventions for Redis-based features.
container (BotContainer): The main Dependency Injection container, which is passed to the orchestrator factories.Process:
INSTALLED_FEATURES and INSTALLED_REDIS_FEATURES.feature_setting module using _load_feature_setting().create_orchestrator factory function is found in the module, it calls this factory, passing the container.notifications or redis_notifications) and stores the created instance in the orchestrators dictionary.Returns:
dict[str, Any]: A dictionary where keys are feature names (with redis_ prefix for Redis features) and values are the instantiated orchestrator objects.
get_menu_buttons Methoddef get_menu_buttons(self) -> dict[str, dict[str, Any]]:
Collects and returns a dictionary of menu button configurations from all UI-oriented features.
Process:
INSTALLED_FEATURES._discover_menu() to retrieve its menu configuration.buttons dictionary.Returns:
dict[str, dict[str, Any]]: A dictionary where keys are button keys and values are their configuration dictionaries.
_load_feature_setting Methoddef _load_feature_setting(self, feature_path: str) -> Any | None:
Attempts to dynamically import the feature_setting.py module for a given feature_path.
feature_path (str): The dot-separated path to the feature (e.g., features.telegram.commands).Process:
src.telegram_bot.{feature_path}.feature_setting or src.telegram_bot.{feature_path}.ImportError gracefully.Returns:
Any | None: The imported module object if successful, otherwise None.
_discover_menu Methoddef _discover_menu(self, feature_path: str) -> dict[str, Any] | None:
Discovers and returns the MENU_CONFIG dictionary from a feature’s menu.py module.
feature_path (str): The dot-separated path to the feature.Process:
src.telegram_bot.{feature_path}.menu.MENU_CONFIG dictionary, it is returned.Returns:
dict[str, Any] | None: The menu configuration dictionary if found, otherwise None.
_discover_garbage_states Methoddef _discover_garbage_states(self, feature_path: str) -> None:
Discovers and registers FSM states from a feature’s feature_setting.py module with the GarbageStateRegistry.
feature_path (str): The dot-separated path to the feature.Process:
feature_setting module.GARBAGE_STATES or GARBAGE_COLLECT flags.STATES if GARBAGE_COLLECT is True) with GarbageStateRegistry.register()._discover_redis_handlers Methoddef _discover_redis_handlers(self, feature_path: str) -> None:
Discovers and includes RedisRouter instances from a feature’s handlers.py module into the bot_redis_dispatcher.
feature_path (str): The dot-separated path to the feature.Process:
src.telegram_bot.{feature_path}.handlers.redis_router instance of type RedisRouter, it includes this router into the global bot_redis_dispatcher.