feat(notify): add telegram alerts for blocked/failed queue tasks
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user