From dd6f7e6dec5ad0d89299ddd8bb85b175ce0970a2 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 6 Feb 2026 10:53:02 -0500 Subject: [PATCH] [bin] Add hroot notif errors to ntfy as well --- bin/.bin/hungryroot-notifications | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bin/.bin/hungryroot-notifications b/bin/.bin/hungryroot-notifications index 29ca8ea..6beacf1 100755 --- a/bin/.bin/hungryroot-notifications +++ b/bin/.bin/hungryroot-notifications @@ -37,6 +37,7 @@ IMAP_PASS = os.environ.get("IMAP_PASS", "") NTFY_BASE = os.environ.get("NTFY_BASE", "https://ntfy.unbl.ink") NTFY_TOPIC = os.environ.get("NTFY_TOPIC", "hroot-notifications") +NTFY_ERROR_TOPIC = os.environ.get("NTFY_ERROR_TOPIC", "errors") POLL_SECONDS = int(os.environ.get("POLL_SECONDS", "20")) # Optional: if your ntfy needs auth @@ -171,6 +172,28 @@ def notify_ntfy(title: str, message: str, url: str | None = None): ) r.raise_for_status() +def notify_error(err: str): + endpoint = f"{NTFY_BASE.rstrip('/')}/{NTFY_ERROR_TOPIC}" + headers = { + "Title": "hungryroot-notifications error", + "Tags": "warning", + "Priority": "5", + } + + auth = None + if NTFY_TOKEN: + headers["Authorization"] = f"Bearer {NTFY_TOKEN}" + elif NTFY_USER and NTFY_PASS: + auth = (NTFY_USER, NTFY_PASS) + + # Keep it short so it doesn't spam + body = err.strip() + if len(body) > 1500: + body = body[:1500] + "…" + + r = requests.post(endpoint, data=body.encode("utf-8"), headers=headers, auth=auth, timeout=10) + r.raise_for_status() + def fetch_unseen(): context = ssl.create_default_context() @@ -217,7 +240,12 @@ def main(): msg = f"{snippet}\n\nFrom: {from_}" notify_ntfy(title=title, message=msg, url=github_url) except Exception as e: - print(f"[hungryroot-notifications] error: {e}") + msg = f"[hungryroot-notifications] {type(e).__name__}: {e}" + print(msg) + try: + notify_error(msg) + except Exception as e2: + print(f"[hungryroot-notifications] failed to notify error: {e2}") time.sleep(POLL_SECONDS)