"""Outbound notifications for the dashboard server.""" from __future__ import annotations import json import sys from typing import Any, Dict, List class AppriseNotifier: def __init__(self, config: Dict[str, Any]) -> None: notifications = config.get("notifications", {}) if isinstance(config.get("notifications", {}), dict) else {} self.enabled = bool(notifications.get("enabled", False)) self.targets: List[str] = [str(item) for item in notifications.get("targets", [])] self.available = False self._apprise = None if not self.enabled: return if not self.targets: sys.stderr.write("dashboard notify: enabled but no Apprise targets configured; notifications disabled\n") self.enabled = False return try: import apprise # type: ignore except ImportError: sys.stderr.write("dashboard notify: Apprise is not installed; run pip install -r tools/dashboard/requirements.txt\n") self.enabled = False return app = apprise.Apprise() for target in self.targets: app.add(target) self._apprise = app self.available = True def send(self, event: Dict[str, Any]) -> None: if not self.enabled or not self.available or self._apprise is None: return title = "NT8 {0}".format(event.get("type", "event")) body = json.dumps(event, sort_keys=True) try: self._apprise.notify(title=title, body=body) except Exception as ex: # best-effort notifier boundary sys.stderr.write("dashboard notify: Apprise send failed; continuing: {0}\n".format(ex)) def status(self) -> Dict[str, Any]: return { "enabled": self.enabled, "available": self.available, "target_count": len(self.targets), }