"""Zulip client for sending notifications.""" import structlog import zulip from .models import ZulipConfig logger = structlog.get_logger() class ZulipNotifier: """Handles sending notifications to Zulip.""" def __init__(self, config: ZulipConfig): """Initialize Zulip client with configuration.""" self.config = config self.client = zulip.Client( site=config.site, email=config.email, api_key=config.api_key, ) # Test connection try: result = self.client.get_profile() if result['result'] != 'success': raise ConnectionError(f"Failed to connect to Zulip: {result}") logger.info("Connected to Zulip", user=result['email']) except Exception as e: logger.error("Failed to initialize Zulip client", error=str(e)) raise def send_message(self, topic: str, content: str) -> bool: """Send a message to the configured Zulip stream. Args: topic: The topic within the stream content: The message content (markdown formatted) Returns: True if successful, False otherwise """ try: result = self.client.send_message({ "type": "stream", "to": self.config.stream, "topic": topic, "content": content, }) if result['result'] == 'success': logger.info( "Message sent to Zulip", stream=self.config.stream, topic=topic, message_id=result.get('id') ) return True else: logger.error( "Failed to send message to Zulip", stream=self.config.stream, topic=topic, error=result.get('msg', 'Unknown error') ) return False except Exception as e: logger.error( "Exception sending message to Zulip", stream=self.config.stream, topic=topic, error=str(e) ) return False