feat(notifications): add Telegram test button

New "Test senden" button next to Speichern posts current form
credentials (not DB) to /actions/notifications/test, which fires a
test message and redirects back with a flash chip showing the outcome
(including the Telegram API's error description on failure).

telegram_send is now public and returns (ok, detail) so the UI can
surface real error messages ("chat not found", "Unauthorized", etc.).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
EiSiMo 2026-04-23 09:35:02 +02:00
parent d06dfdaca1
commit 64439fd42e
3 changed files with 63 additions and 8 deletions

View file

@ -18,7 +18,9 @@ logger = logging.getLogger("web.notifications")
EventType = str # 'match' | 'apply_ok' | 'apply_fail'
def _telegram_send(token: str, chat_id: str, text: str) -> bool:
def telegram_send(token: str, chat_id: str, text: str) -> tuple[bool, str]:
"""Send a Telegram message. Returns (ok, detail) — detail is the Telegram
API's `description` on failure, or a short error string, or "ok"."""
try:
r = requests.post(
f"https://api.telegram.org/bot{token}/sendMessage",
@ -26,13 +28,17 @@ def _telegram_send(token: str, chat_id: str, text: str) -> bool:
"disable_web_page_preview": True},
timeout=10,
)
if not r.ok:
logger.warning("telegram send failed: %s %s", r.status_code, r.text[:200])
return False
return True
if r.ok:
return True, "ok"
try:
detail = r.json().get("description") or f"HTTP {r.status_code}"
except ValueError:
detail = f"HTTP {r.status_code}"
logger.warning("telegram send failed: %s %s", r.status_code, r.text[:200])
return False, detail
except requests.RequestException as e:
logger.warning("telegram unreachable: %s", e)
return False
return False, str(e)
def _should_notify(notif, event: EventType) -> bool:
@ -57,7 +63,7 @@ def notify_user(user_id: int, event: EventType, *,
token = notif["telegram_bot_token"]
chat = notif["telegram_chat_id"]
if token and chat:
_telegram_send(token, chat, body_markdown or body_plain)
telegram_send(token, chat, body_markdown or body_plain)
except Exception:
logger.exception("notify_user failed for user=%s event=%s", user_id, event)