from __future__ import annotations import asyncio from typing import Any import httpx from . import db async def send_webhook(event: str, payload: dict[str, Any]) -> None: """Existing webhook notifier — unchanged behavior (ops/ntfy is a separate module).""" cfg = db.get_settings_map() url = str(cfg.get("notify_webhook_url") or "").strip() if not url: return if event == "live" and not cfg.get("notify_on_live", True): return if event == "offline" and not cfg.get("notify_on_offline", False): return if event == "risk" and not cfg.get("notify_on_risk", True): return if event == "shorts_fail" and not cfg.get("notify_on_shorts_fail", True): return if event == "shorts_ok" and not cfg.get("notify_on_shorts_ok", False): return if event == "shorts_quota" and not cfg.get("notify_on_shorts_quota", True): return if event == "tiktok_fail" and not cfg.get("notify_on_tiktok_fail", True): return if event == "tiktok_ok" and not cfg.get("notify_on_tiktok_ok", False): return if event == "tiktok_limit" and not cfg.get("notify_on_tiktok_limit", True): return if event == "watch_remind" and not cfg.get("notify_on_watch_remind", True): return # allow ops/test always when URL configured body = { "event": event, "text": payload.get("message") or event, "payload": payload, } async def _post() -> None: try: async with httpx.AsyncClient(timeout=8.0, trust_env=False) as client: await client.post(url, json=body) except Exception: pass try: asyncio.get_running_loop() asyncio.create_task(_post()) except RuntimeError: await _post()