-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend-ntfy
More file actions
executable file
·118 lines (108 loc) · 2.7 KB
/
send-ntfy
File metadata and controls
executable file
·118 lines (108 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
set -e
# Help function
show_help() {
cat <<EOF
Usage: $0 [OPTIONS] MESSAGE
Send a notification via ntfy.sh
Options:
-c, --config FILE Configuration file (default: ~/.ntfy or \$CONFIG)
-s, --server URL Ntfy server URL (default: first line of config or \$SERVER)
-t, --topic TOPIC Topic to publish to (default: second line of config or \$TOPIC)
-k, --token TOKEN Access token (default: last line of config or \$TOKEN)
--tags TAGS Tags for notification (comma-separated, default: computer or \$TAGS)
--title TITLE Notification title (default: Alert or \$TITLE)
-p, --priority PRIO Priority (1-5, default: 3 or \$PRIORITY)
--delay DELAY Delay delivery until specific time (timestamp)
--actions ACTIONS Notification actions (JSON format)
-h, --help Show this help message
Environment variables can be used instead of flags and take precedence.
EOF
exit 0
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --config)
CONFIG="$2"
shift 2
;;
-s | --server)
SERVER="$2"
shift 2
;;
-t | --topic)
TOPIC="$2"
shift 2
;;
-k | --token)
TOKEN="$2"
shift 2
;;
--tags)
TAGS="$2"
shift 2
;;
--title)
TITLE="$2"
shift 2
;;
-p | --priority)
PRIORITY="$2"
shift 2
;;
--delay)
DELAY="$2"
shift 2
;;
--actions)
ACTIONS="$2"
shift 2
;;
-h | --help)
show_help
;;
*)
# First non-option argument is treated as the message
if [[ -z "$MESSAGE" ]]; then
MESSAGE="$1"
shift
else
echo "Error: Unknown option or too many arguments: $1" >&2
exit 1
fi
;;
esac
done
# Default values
CONFIG=${CONFIG-$HOME/.ntfy}
SERVER=${SERVER-$(head -n1 "$CONFIG" 2>/dev/null || true)}
TOPIC=${TOPIC-$(head -n2 "$CONFIG" 2>/dev/null | tail -n1 || true)}
TOKEN=${TOKEN-$(tail -n1 "$CONFIG" 2>/dev/null || true)}
TAGS=${TAGS-computer}
TITLE=${TITLE-Alert}
PRIORITY=${PRIORITY-3}
DELAY=${DELAY-}
ACTIONS=${ACTIONS-}
send_ntfy() {
local curl_args=(
"-H" "Authorization: Bearer $TOKEN"
"-H" "Tags: $TAGS"
"-H" "Title: $TITLE"
"-H" "X-Priority: $PRIORITY"
)
# Only add Delay header if DELAY is set
[[ -n "$DELAY" ]] && curl_args+=("-H" "Delay: $DELAY")
# Only add Actions header if ACTIONS is set
[[ -n "$ACTIONS" ]] && curl_args+=("-H" "Actions: $ACTIONS")
curl "${curl_args[@]}" -d "$@" "$SERVER/$TOPIC"
}
# If not sourced and no help requested, send notification
if [[ "${BASH_SOURCE[0]}" == "$0" ]] && [[ "$1" != "-h" && "$1" != "--help" ]]; then
if [[ -z "$MESSAGE" ]]; then
echo "Error: Message content is required" >&2
show_help
exit 1
fi
send_ntfy "$MESSAGE"
fi