From 8989adc9aecd409309c6f472b1022d1eada9d58d Mon Sep 17 00:00:00 2001 From: Re-bin Date: Tue, 3 Feb 2026 06:36:58 +0000 Subject: [PATCH] refactor: use explicit dependency injection for groq_api_key --- nanobot/channels/manager.py | 8 +++----- nanobot/channels/telegram.py | 10 +++------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/nanobot/channels/manager.py b/nanobot/channels/manager.py index c32aa3d..73c3334 100644 --- a/nanobot/channels/manager.py +++ b/nanobot/channels/manager.py @@ -36,10 +36,10 @@ class ChannelManager: if self.config.channels.telegram.enabled: try: from nanobot.channels.telegram import TelegramChannel - # Inject parent config for access to providers - self.config.channels.telegram.parent = self.config self.channels["telegram"] = TelegramChannel( - self.config.channels.telegram, self.bus + self.config.channels.telegram, + self.bus, + groq_api_key=self.config.providers.groq.api_key, ) logger.info("Telegram channel enabled") except ImportError as e: @@ -49,8 +49,6 @@ class ChannelManager: if self.config.channels.whatsapp.enabled: try: from nanobot.channels.whatsapp import WhatsAppChannel - # Inject parent config for access to providers - self.config.channels.whatsapp.parent = self.config self.channels["whatsapp"] = WhatsAppChannel( self.config.channels.whatsapp, self.bus ) diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 75b9299..23e1de0 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -85,9 +85,10 @@ class TelegramChannel(BaseChannel): name = "telegram" - def __init__(self, config: TelegramConfig, bus: MessageBus): + def __init__(self, config: TelegramConfig, bus: MessageBus, groq_api_key: str = ""): super().__init__(config, bus) self.config: TelegramConfig = config + self.groq_api_key = groq_api_key self._app: Application | None = None self._chat_ids: dict[str, int] = {} # Map sender_id to chat_id for replies @@ -253,12 +254,7 @@ class TelegramChannel(BaseChannel): # Handle voice transcription if media_type == "voice" or media_type == "audio": from nanobot.providers.transcription import GroqTranscriptionProvider - # Try to get Groq API key from config - groq_key = None - if hasattr(self.config, 'parent') and hasattr(self.config.parent, 'providers'): - groq_key = self.config.parent.providers.groq.api_key - - transcriber = GroqTranscriptionProvider(api_key=groq_key) + transcriber = GroqTranscriptionProvider(api_key=self.groq_api_key) transcription = await transcriber.transcribe(file_path) if transcription: logger.info(f"Transcribed {media_type}: {transcription[:50]}...")