# Copyright (c) 2025 Patrick Motsch # All rights reserved. """ Twilio SMS Connector Handles SMS sending via Twilio """ import logging from typing import Optional from modules.shared.configuration import APP_CONFIG logger = logging.getLogger(__name__) class ConnectorMessagingSms: """ Twilio SMS connector. Handles SMS sending. """ def __init__(self): """ Initialize Twilio SMS client using APP_CONFIG. """ try: accountSid = APP_CONFIG.get("MESSAGING_TWILIO_ACCOUNT_SID") authToken = APP_CONFIG.get("MESSAGING_TWILIO_AUTH_TOKEN") fromNumber = APP_CONFIG.get("MESSAGING_TWILIO_FROM_NUMBER") if not accountSid or not authToken: logger.warning("Twilio credentials not configured for SMS") self._client = None self._fromNumber = None return try: from twilio.rest import Client self._client = Client(accountSid, authToken) self._fromNumber = fromNumber logger.info("Twilio SMS client initialized successfully") except ImportError: logger.error("Twilio library not installed. Please install with: pip install twilio") self._client = None self._fromNumber = None except Exception as e: logger.error(f"Failed to initialize Twilio SMS client: {e}") self._client = None self._fromNumber = None def send(self, recipient: str, subject: str, message: str) -> bool: """ Send an SMS via Twilio. Args: recipient: Recipient phone number (with country code, e.g., '+41791234567') subject: Ignored (SMS has no subject) message: SMS message content Returns: bool: True if successful, False otherwise """ if not self._client or not self._fromNumber: logger.error("Twilio SMS client not initialized") return False try: messageObj = self._client.messages.create( body=message, from_=self._fromNumber, to=recipient ) logger.info(f"SMS sent successfully to {recipient}. SID: {messageObj.sid}") return True except Exception as e: logger.error(f"Failed to send SMS to {recipient}: {e}") return False