#!/usr/bin/env bash set -euo pipefail # Usage: ./notify.sh "" [""] MESSAGE="${1:-Task complete}" AGENT_LABEL="${2:-$(whoami)}" # ------------------------------------------------------------------ # Build the notification title. # In a git repo: user@host repo (branch) [session] # Not in a repo: user@host [session] # Override (PI_NOTIFY_TITLE): use exactly that string. # ------------------------------------------------------------------ build_title() { # Full manual override — use exactly what the user set if [[ -n "${PI_NOTIFY_TITLE:-}" ]]; then echo "${PI_NOTIFY_TITLE}" return fi local label="$AGENT_LABEL" # ----- hostname ----- local hostname hostname="$(hostname -s 2>/dev/null || hostname 2>/dev/null || echo "unknown")" # ----- git repo context: only show when in a git repo ----- local location="" local repo_root repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)" if [[ -n "$repo_root" ]]; then local repo_name branch repo_name="$(basename "$repo_root")" branch="$(git branch --show-current 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")" if [[ -n "$branch" && "$branch" != "HEAD" ]]; then location="${repo_name} (${branch})" else location="${repo_name}" fi fi # ----- optional session label (multiple concurrent agents) ----- local session="${PI_NOTIFY_SESSION_NAME:-}" # ----- assemble ----- local title="${label}@${hostname}" if [[ -n "$location" ]]; then title="${title} ${location}" fi if [[ -n "$session" ]]; then title="${title} [${session}]" fi echo "${title}" } TITLE="$(build_title)" # ------------------------------------------------------------------ # Notification backend # ------------------------------------------------------------------ send_ntfy_notification() { if [[ -z "${PI_NOTIFY_NTFY_SERVER:-}" || -z "${PI_NOTIFY_NTFY_TOPIC:-}" || -z "${PI_NOTIFY_NTFY_TOKEN:-}" ]]; then echo "Warning: ntfy notification is not configured" >&2 return 0 fi if ! command -v curl >/dev/null 2>&1; then echo "Warning: curl is required for ntfy notifications" >&2 return 0 fi local server="${PI_NOTIFY_NTFY_SERVER%/}" curl -fsS \ -H "Authorization: Bearer ${PI_NOTIFY_NTFY_TOKEN}" \ -H "Title: ${TITLE}" \ -H "Priority: default" \ --data-binary "${MESSAGE}" \ "${server}/${PI_NOTIFY_NTFY_TOPIC}" >/dev/null || true } # ------------------------------------------------------------------ # Dispatch # ------------------------------------------------------------------ send_ntfy_notification