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