From b5430e93bb530add86c3fe54bb79be53d4bd3586 Mon Sep 17 00:00:00 2001 From: wsw70 <1345886+wsw70@users.noreply.github.com> Date: Thu, 25 Apr 2024 15:51:08 +0200 Subject: [PATCH 1/4] Added ntfy icon prefix Added the ability to use an icon as the prefix for the subject Updated documentation --- docs/alerters/ntfy.rst | 26 ++++++++++++++++++++++++++ simplemonitor/Alerters/ntfy.py | 23 ++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/alerters/ntfy.rst b/docs/alerters/ntfy.rst index ae6481db..bd9f07eb 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: bool + :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: bool + :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..8e663cf1 100644 --- a/simplemonitor/Alerters/ntfy.py +++ b/simplemonitor/Alerters/ntfy.py @@ -49,14 +49,35 @@ 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 = cast( + str, + self.get_config_option("icon_failed", required_type="str", default="274C"), + ) + self.ntfy_icon_succeeded = cast( + str, + self.get_config_option( + "icon_succeeded", required_type="str", default="2705" + ), + ) def send_ntfy_notification(self, subject: str, body: str) -> None: """Send a push notification.""" + # prefix icon to subject when relevant + if self.ntfy_icon_prefix and subject.endswith("failed"): + subject = f"{chr(int(self.ntfy_icon_failed, 16))} {subject}" + if self.ntfy_icon_prefix and subject.endswith("succeeded"): + subject = f"{chr(int(self.ntfy_icon_succeeded, 16))} {subject}" + # send the notification requests.post( 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 {}), **( From 084589e388023dd634740624211dda67cd63e7b6 Mon Sep 17 00:00:00 2001 From: wsw70 <1345886+wsw70@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:27:09 +0200 Subject: [PATCH 2/4] moved icon logic closer to the message construction --- simplemonitor/Alerters/ntfy.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/simplemonitor/Alerters/ntfy.py b/simplemonitor/Alerters/ntfy.py index 8e663cf1..5fa7807f 100644 --- a/simplemonitor/Alerters/ntfy.py +++ b/simplemonitor/Alerters/ntfy.py @@ -67,12 +67,6 @@ def __init__(self, config_options: dict) -> None: def send_ntfy_notification(self, subject: str, body: str) -> None: """Send a push notification.""" - # prefix icon to subject when relevant - if self.ntfy_icon_prefix and subject.endswith("failed"): - subject = f"{chr(int(self.ntfy_icon_failed, 16))} {subject}" - if self.ntfy_icon_prefix and subject.endswith("succeeded"): - subject = f"{chr(int(self.ntfy_icon_succeeded, 16))} {subject}" - # send the notification requests.post( f"{self.ntfy_server}/{self.ntfy_topic}", data=body, @@ -99,6 +93,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"{chr(int(self.ntfy_icon_failed, 16))} {subject}" + if self.ntfy_icon_prefix and subject.endswith("succeeded"): + subject = f"{chr(int(self.ntfy_icon_succeeded, 16))} {subject}" + if not self._dry_run: try: self.send_ntfy_notification(subject, body) From 8bcace3d2ad26566afe33db7a52bd8681a96d33f Mon Sep 17 00:00:00 2001 From: wsw70 <1345886+wsw70@users.noreply.github.com> Date: Thu, 25 Apr 2024 16:54:57 +0200 Subject: [PATCH 3/4] corrected ntfy docs (icon prefix) --- docs/alerters/ntfy.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/alerters/ntfy.rst b/docs/alerters/ntfy.rst index bd9f07eb..d571e57c 100644 --- a/docs/alerters/ntfy.rst +++ b/docs/alerters/ntfy.rst @@ -62,7 +62,7 @@ Send alerts using the ntfy_ service. .. confval:: icon_failed - :type: bool + :type: str :required: false :default: ``274C`` @@ -70,7 +70,7 @@ Send alerts using the ntfy_ service. .. confval:: icon_succeeded - :type: bool + :type: str :required: false :default: ``2705`` From 40ecb721b95b685346e90df73517ac8399dc7663 Mon Sep 17 00:00:00 2001 From: wsw70 <1345886+wsw70@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:02:35 +0200 Subject: [PATCH 4/4] reorganized icon prefixing logic --- simplemonitor/Alerters/ntfy.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/simplemonitor/Alerters/ntfy.py b/simplemonitor/Alerters/ntfy.py index 5fa7807f..c7df43b7 100644 --- a/simplemonitor/Alerters/ntfy.py +++ b/simplemonitor/Alerters/ntfy.py @@ -54,16 +54,8 @@ def __init__(self, config_options: dict) -> None: str, self.get_config_option("icon_prefix", required_type="bool", default=False), ) - self.ntfy_icon_failed = cast( - str, - self.get_config_option("icon_failed", required_type="str", default="274C"), - ) - self.ntfy_icon_succeeded = cast( - str, - self.get_config_option( - "icon_succeeded", required_type="str", default="2705" - ), - ) + 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.""" @@ -95,9 +87,9 @@ def send_alert(self, name: str, monitor: Monitor) -> None: # prefix icon to subject when relevant if self.ntfy_icon_prefix and subject.endswith("failed"): - subject = f"{chr(int(self.ntfy_icon_failed, 16))} {subject}" + subject = f"{self.ntfy_icon_failed} {subject}" if self.ntfy_icon_prefix and subject.endswith("succeeded"): - subject = f"{chr(int(self.ntfy_icon_succeeded, 16))} {subject}" + subject = f"{self.ntfy_icon_succeeded} {subject}" if not self._dry_run: try: