[bin] Add hroot notif errors to ntfy as well
This commit is contained in:
@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user