Sends alerts to a configurable Ntfy topic on: new user registration, account lockout after 5 failed login attempts, and login attempts on an already-locked account. Fire-and-forget — never raises if Ntfy is down. Configure via NTFY_URL and NTFY_TOKEN in .env. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
26 lines
872 B
Python
26 lines
872 B
Python
import logging
|
|
import httpx
|
|
from app.config import get_settings
|
|
|
|
logger = logging.getLogger("homeschool.ntfy")
|
|
|
|
|
|
async def notify(title: str, message: str, priority: str = "default", tags: list[str] | None = None) -> None:
|
|
"""Fire-and-forget notification to Ntfy. Silently logs errors — never raises."""
|
|
settings = get_settings()
|
|
if not settings.ntfy_url:
|
|
return
|
|
|
|
headers = {"Title": title, "Priority": priority}
|
|
if tags:
|
|
headers["Tags"] = ",".join(tags)
|
|
if settings.ntfy_token:
|
|
headers["Authorization"] = f"Bearer {settings.ntfy_token}"
|
|
|
|
try:
|
|
async with httpx.AsyncClient(timeout=5) as client:
|
|
resp = await client.post(settings.ntfy_url, content=message, headers=headers)
|
|
resp.raise_for_status()
|
|
except Exception as exc:
|
|
logger.warning("Ntfy notification failed: %s", exc)
|