From 29fa11cce102749f425b73c2ee1a38253225b363 Mon Sep 17 00:00:00 2001 From: Prime Backup Bot Date: Wed, 11 Mar 2026 20:27:20 +0000 Subject: [PATCH] feat(notify): add telegram alerts for blocked/failed queue tasks --- docs/PRIME_BOOTSTRAP.md | 9 +++++++++ prime_core/notify.py | 24 ++++++++++++++++++++++++ prime_core/worker.py | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 prime_core/notify.py diff --git a/docs/PRIME_BOOTSTRAP.md b/docs/PRIME_BOOTSTRAP.md index 20dc491..da025c5 100644 --- a/docs/PRIME_BOOTSTRAP.md +++ b/docs/PRIME_BOOTSTRAP.md @@ -35,3 +35,12 @@ Worker service: ```bash sudo systemctl status prime-worker.service ``` + +## 6) Telegram notifications +Set in local runtime secrets (`.secrets/runtime.env`): +- `TELEGRAM_BOT_TOKEN` +- `TELEGRAM_CHAT_ID` + +Worker sends alerts for: +- blocked approval tasks +- failed tasks diff --git a/prime_core/notify.py b/prime_core/notify.py new file mode 100644 index 0000000..dc27a94 --- /dev/null +++ b/prime_core/notify.py @@ -0,0 +1,24 @@ +from __future__ import annotations +import os +import urllib.parse +import urllib.request + + +def send_telegram(text: str) -> bool: + token = os.getenv('TELEGRAM_BOT_TOKEN', '').strip() + chat_id = os.getenv('TELEGRAM_CHAT_ID', '').strip() + if not token or not chat_id: + return False + + url = f"https://api.telegram.org/bot{token}/sendMessage" + data = urllib.parse.urlencode({ + 'chat_id': chat_id, + 'text': text, + 'disable_web_page_preview': 'true' + }).encode() + req = urllib.request.Request(url, data=data, method='POST') + try: + with urllib.request.urlopen(req, timeout=12) as resp: + return 200 <= resp.status < 300 + except Exception: + return False diff --git a/prime_core/worker.py b/prime_core/worker.py index 8907744..c719570 100644 --- a/prime_core/worker.py +++ b/prime_core/worker.py @@ -5,6 +5,7 @@ import time from .router import dispatch from .queue import next_item, update from .policy import needs_approval +from .notify import send_telegram def run_once() -> dict: @@ -14,6 +15,7 @@ def run_once() -> dict: if needs_approval(it['task']): update(it['id'], status='blocked_approval', blocked_reason='sensitive_task') + send_telegram(f"🚧 Prime blocked task {it['id']} (approval required): {it['task']}") return {'id': it['id'], 'status': 'blocked_approval'} out = dispatch(it['task_type'], it['task'])