diff options
Diffstat (limited to 'claude-templates/bin')
| -rwxr-xr-x | claude-templates/bin/page-signal | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/claude-templates/bin/page-signal b/claude-templates/bin/page-signal new file mode 100755 index 0000000..5a87c67 --- /dev/null +++ b/claude-templates/bin/page-signal @@ -0,0 +1,126 @@ +#!/usr/bin/env bash +# +# page-signal — wrap signal-cli send for paging Craig via Signal. +# +# Defaults to --note-to-self for safety. Outbound (to another contact) +# requires an explicit --to <+number> on every call. No defaults, no batch. +# +# Usage: +# page-signal "message" # note-to-self +# page-signal --file path "message" # with attachment +# page-signal --to +15551234567 "msg" # outbound (explicit recipient) +# page-signal --quiet "message" # suppress success output +# page-signal --json "message" # structured output (timestamp + status) +# echo "msg" | page-signal # message from stdin (no positional) +# +# Requires signal-cli installed and registered with an account. +# +# Use this when desktop notifications won't reach the user — page-signal +# fires cross-device. For routine completions and periodic status pings, +# stick with `notify`; Signal is for things that need attention while away +# from the desk. + +set -euo pipefail + +quiet=0 +json=0 +file= +recipient_args=(--note-to-self) +message= + +usage() { + sed -n '3,21p' "$0" | sed 's/^# \{0,1\}//' +} + +while [ "$#" -gt 0 ]; do + case "$1" in + -h|--help) + usage + exit 0 + ;; + --quiet) + quiet=1 + shift + ;; + --json) + json=1 + shift + ;; + --file) + [ "$#" -ge 2 ] || { echo "page-signal: --file needs a path" >&2; exit 2; } + file="$2" + [ -f "$file" ] || { echo "page-signal: file not found: $file" >&2; exit 2; } + shift 2 + ;; + --to) + [ "$#" -ge 2 ] || { echo "page-signal: --to needs a +E.164 number" >&2; exit 2; } + case "$2" in + +[0-9]*) ;; + *) echo "page-signal: --to expects +E.164 (e.g. +15551234567)" >&2; exit 2 ;; + esac + recipient_args=("$2") + shift 2 + ;; + --) + shift + break + ;; + -*) + echo "page-signal: unknown flag: $1 (use --help)" >&2 + exit 2 + ;; + *) + message="$1" + shift + ;; + esac +done + +if ! command -v signal-cli >/dev/null 2>&1; then + echo "page-signal: signal-cli not found in PATH" >&2 + exit 3 +fi + +# Message: from positional arg, or from stdin if none given. +if [ -z "$message" ]; then + if [ -t 0 ]; then + echo "page-signal: no message (pass as arg or pipe via stdin)" >&2 + exit 2 + fi + message="$(cat)" +fi + +# Build the send command. +cmd=(signal-cli send -m "$message") +if [ -n "$file" ]; then + cmd+=(-a "$file") +fi +cmd+=("${recipient_args[@]}") + +# Send. Capture stderr so we can surface clean failure messages. +timestamp="$(date '+%Y-%m-%dT%H:%M:%S%z')" +if err="$("${cmd[@]}" 2>&1 >/dev/null)"; then + status=ok +else + status=fail +fi + +if [ "$json" -eq 1 ]; then + printf '{"status":"%s","timestamp":"%s","recipient":"%s"' \ + "$status" "$timestamp" "${recipient_args[0]}" + if [ -n "$file" ]; then + printf ',"attachment":"%s"' "$file" + fi + if [ "$status" = "fail" ]; then + printf ',"error":"%s"' "$(echo "$err" | head -1 | sed 's/"/\\"/g')" + fi + printf '}\n' +elif [ "$quiet" -eq 0 ]; then + if [ "$status" = "ok" ]; then + echo "page-signal: sent to ${recipient_args[*]} at $timestamp" + else + echo "page-signal: FAIL — $err" >&2 + fi +fi + +[ "$status" = "ok" ] |
