Netdata.cloud bot for Zulip
1"""Message formatting for Zulip notifications.""" 2 3from typing import Union 4 5from .models import AlertNotification, ReachabilityNotification, TestNotification 6 7 8class ZulipMessageFormatter: 9 """Format Netdata notifications for Zulip messages.""" 10 11 def format_alert(self, alert: AlertNotification) -> tuple[str, str]: 12 """Format alert notification for Zulip. 13 14 Returns: 15 Tuple of (topic, message_content) 16 """ 17 topic = alert.severity.value 18 19 # Severity emoji mapping 20 severity_emoji = { 21 "critical": "🔴", 22 "warning": "⚠️", 23 "clear": "" 24 } 25 26 emoji = severity_emoji.get(alert.severity.value, "📊") 27 28 message = f"""{emoji} **{alert.alert}** 29 30**Space:** {alert.space} 31**Chart:** {alert.chart} 32**Context:** {alert.context} 33**Severity:** {alert.severity.value.title()} 34**Time:** {alert.date.strftime('%Y-%m-%d %H:%M:%S UTC')} 35 36**Details:** {alert.info} 37 38**Summary:** {alert.message} 39 40[View Alert]({alert.alert_url})""" 41 42 return topic, message 43 44 def format_reachability(self, notification: ReachabilityNotification) -> tuple[str, str]: 45 """Format reachability notification for Zulip. 46 47 Returns: 48 Tuple of (topic, message_content) 49 """ 50 topic = "reachability" 51 52 status_emoji = "" if notification.status.reachable else "" 53 severity_emoji = "🔴" if notification.severity.value == "critical" else "ℹ️" 54 55 message = f"""{severity_emoji} **Host {notification.status.text.title()}** 56 57**Host:** {notification.host} 58**Status:** {status_emoji} {notification.status.text.title()} 59**Severity:** {notification.severity.value.title()} 60 61**Summary:** {notification.message} 62 63[View Host]({notification.url})""" 64 65 return topic, message 66 67 def format_test(self, notification: TestNotification) -> tuple[str, str]: 68 """Format test notification for Zulip. 69 70 Returns: 71 Tuple of (topic, message_content) 72 """ 73 topic = "test" 74 75 message = f"""🧪 **Netdata Webhook Test** 76 77{notification.message} 78 79Your webhook integration is working correctly! ✅""" 80 81 return topic, message 82 83 def format_notification(self, notification: Union[AlertNotification, ReachabilityNotification, TestNotification]) -> tuple[str, str]: 84 """Format any notification type for Zulip. 85 86 Returns: 87 Tuple of (topic, message_content) 88 """ 89 if isinstance(notification, AlertNotification): 90 return self.format_alert(notification) 91 elif isinstance(notification, ReachabilityNotification): 92 return self.format_reachability(notification) 93 elif isinstance(notification, TestNotification): 94 return self.format_test(notification) 95 else: 96 raise ValueError(f"Unknown notification type: {type(notification)}")