Skip to content

Commit

Permalink
Merge pull request #1382 from wsw70/master
Browse files Browse the repository at this point in the history
Added ntfy icon prefix
  • Loading branch information
jamesoff authored Apr 25, 2024
2 parents c0cba71 + 40ecb72 commit 4bfc37c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/alerters/ntfy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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+<code>" (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+<code>" (e.g. ``U+2705``). The default icon for the failed status is ✅.


15 changes: 14 additions & 1 deletion simplemonitor/Alerters/ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ 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."""
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 {}),
**(
Expand All @@ -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)
Expand Down

0 comments on commit 4bfc37c

Please sign in to comment.