nomadro/apps/api/services/community_svc/services/ntfy_push.py
Eric 3e60ba1a1b Ship social places/join UX, room-archive ops, and rich Markdown ntfy alerts.
Unify city+cafe into one places page, gate join with on-page modal, and enrich ntfy with icons, tables, and action buttons across portal/API/admin.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 12:21:01 +08:00

54 lines
1.3 KiB
Python

"""ntfy push notifications — Markdown + icon + actions."""
from __future__ import annotations
from typing import Any
import httpx
from community_svc.config import settings
DEFAULT_ICON = "https://nomadro.com/icon-192.png"
async def send_push(
topic: str,
title: str,
body: str,
*,
tags: list[str] | None = None,
click_url: str = "",
markdown: bool = True,
icon: str = DEFAULT_ICON,
actions: list[dict[str, Any]] | None = None,
attach: str = "",
) -> bool:
if not settings.ntfy_enabled:
return False
base = settings.ntfy_url.rstrip("/")
# Prefer JSON body so Markdown / Chinese titles work reliably.
payload: dict[str, Any] = {
"topic": topic.strip().strip("/"),
"title": (title or "Nomadro")[:200],
"message": (body or "")[:3900],
"markdown": bool(markdown),
"priority": 3,
}
if tags:
payload["tags"] = [t for t in tags if t][:12]
if click_url:
payload["click"] = click_url[:512]
if icon:
payload["icon"] = icon[:512]
if attach:
payload["attach"] = attach[:512]
if actions:
payload["actions"] = actions[:3]
headers: dict[str, str] = {"Content-Type": "application/json"}
try:
async with httpx.AsyncClient(timeout=8.0) as client:
r = await client.post(base, json=payload, headers=headers)
return r.status_code < 300
except Exception:
return False