diff --git a/docs/alerters/ntfy.rst b/docs/alerters/ntfy.rst index ae6481db..d571e57c 100644 --- a/docs/alerters/ntfy.rst +++ b/docs/alerters/ntfy.rst @@ -51,3 +51,29 @@ Send alerts using the ntfy_ service. :default: ``5`` Timeout for HTTP request + +.. confval:: icon_prefix + + :type: bool + :required: false + :default: ``false`` + + Prefix the subject line with an icon dependent on the result (failed/succeeded) + +.. confval:: icon_failed + + :type: str + :required: false + :default: ``274C`` + + Unicode code for the "failed" icon. The code is often provided as "U+" (e.g. ``U+274C``). The default icon for the failed status is ❌. + +.. confval:: icon_succeeded + + :type: str + :required: false + :default: ``2705`` + + Unicode code for the "succeeded" icon. The code is often provided as "U+" (e.g. ``U+2705``). The default icon for the failed status is ✅. + + diff --git a/simplemonitor/Alerters/ntfy.py b/simplemonitor/Alerters/ntfy.py index 809c4ffa..c7df43b7 100644 --- a/simplemonitor/Alerters/ntfy.py +++ b/simplemonitor/Alerters/ntfy.py @@ -49,6 +49,13 @@ def __init__(self, config_options: dict) -> None: int, self.get_config_option("timeout", required_type="int", default=5) ) self.support_catchup = True + # prefix icon to subject + self.ntfy_icon_prefix = cast( + str, + self.get_config_option("icon_prefix", required_type="bool", default=False), + ) + self.ntfy_icon_failed = chr(int(cast(str,self.get_config_option("icon_failed", required_type="str", default="274C")), 16)) + self.ntfy_icon_succeeded = chr(int(cast(str, self.get_config_option("icon_succeeded", required_type="str", default="2705")), 16)) def send_ntfy_notification(self, subject: str, body: str) -> None: """Send a push notification.""" @@ -56,7 +63,7 @@ def send_ntfy_notification(self, subject: str, body: str) -> None: f"{self.ntfy_server}/{self.ntfy_topic}", data=body, headers={ - "Title": subject, + "Title": subject.encode("UTF-8"), "Priority": self.ntfy_priority, **({"Tags": self.ntfy_tags} if self.ntfy_tags else {}), **( @@ -78,6 +85,12 @@ def send_alert(self, name: str, monitor: Monitor) -> None: subject = self.build_message(AlertLength.NOTIFICATION, alert_type, monitor) body = self.build_message(AlertLength.FULL, alert_type, monitor) + # prefix icon to subject when relevant + if self.ntfy_icon_prefix and subject.endswith("failed"): + subject = f"{self.ntfy_icon_failed} {subject}" + if self.ntfy_icon_prefix and subject.endswith("succeeded"): + subject = f"{self.ntfy_icon_succeeded} {subject}" + if not self._dry_run: try: self.send_ntfy_notification(subject, body)