nomadro/deploy/observability/notify-ntfy.sh
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

75 lines
1.8 KiB
Bash

#!/bin/bash
# Publish a rich notification to ntfy (https://ntfy.nomadro.cn).
# Usage: notify-ntfy.sh "title" "message" [priority] [tags] [topic] [click]
set -euo pipefail
CONF="${NOMADRO_NTFY_CONF:-/etc/nomadro/ntfy.env}"
# shellcheck disable=SC1090
[[ -f "$CONF" ]] && source "$CONF"
TITLE="${1:-Alert}"
MESSAGE="${2:-}"
PRIORITY="${3:-default}"
TAGS="${4:-warning}"
TOPIC="${5:-${NTFY_TOPIC:-nomadro}}"
CLICK="${6:-}"
BASE_URL="${NTFY_BASE_URL:-https://ntfy.nomadro.cn}"
TOKEN="${NTFY_TOKEN:-}"
ICON="${NTFY_ICON_URL:-https://nomadro.com/icon-192.png}"
if [[ -z "$TOKEN" ]]; then
echo "notify-ntfy: NTFY_TOKEN missing in $CONF" >&2
exit 1
fi
# Map named priorities to ntfy ints
case "$PRIORITY" in
min) P=1 ;;
low) P=2 ;;
default) P=3 ;;
high) P=4 ;;
urgent|max) P=5 ;;
*) P="$PRIORITY" ;;
esac
NOW="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S')"
RICH=$(cat <<EOF
### ${TITLE}
${MESSAGE}
- **时间** \`${NOW}\`
- **主机** \`$(hostname 2>/dev/null || echo unknown)\`
EOF
)
# Build JSON without requiring jq
python3 - "$BASE_URL" "$TOKEN" "$TOPIC" "$TITLE" "$RICH" "$P" "$TAGS" "$ICON" "$CLICK" <<'PY'
import json, sys, urllib.request
base, token, topic, title, message, priority, tags, icon, click = sys.argv[1:10]
payload = {
"topic": topic.strip("/"),
"title": title[:200],
"message": message[:3900],
"priority": int(priority),
"markdown": True,
"tags": [t.strip() for t in tags.split(",") if t.strip()],
"icon": icon,
}
if click:
payload["click"] = click
payload["actions"] = [{"action": "view", "label": "打开", "url": click, "clear": True}]
req = urllib.request.Request(
base.rstrip("/") + "/",
data=json.dumps(payload).encode("utf-8"),
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
},
method="POST",
)
with urllib.request.urlopen(req, timeout=12) as resp:
resp.read()
print("ok")
PY