#!/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 </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