-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathntfy.sh
90 lines (79 loc) · 2.24 KB
/
ntfy.sh
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
#!/usr/bin/env bash
# load env file from $NTFY_ENV or script dir
SCRIPTPATH=${NTFY_ENV:-$(dirname "$0")/.env}
[ -f ${SCRIPTPATH} ] && . "${SCRIPTPATH}" || echo "ENV missing: ${SCRIPTPATH}"
help()
{
echo "Options:"
echo "-t Your topic. (Optional, hostname will be used if omitted.)"
echo "-m Your message."
echo "-p Notification priority, 1-5, 5 is the highest. (Optional)"
echo "-e Choose emoji. (https://ntfy.sh/docs/emojis/?h=emo)"
echo "-i Icon URL"
echo "-h Print this help."
echo
echo "If you want to show if the last command was successful or not, you can do something like this:"
echo "yourcommand ; export le=$? ; /path/to/ntfy.sh"
echo
}
while getopts "t:m:p:e:i:h" option; do
case $option in
t) ntfy_topic=${OPTARG};;
m) ntfy_message=${OPTARG};;
p) ntfy_prio=${OPTARG};;
e) ntfy_emoji=${OPTARG};;
i) ntfy_icon=${OPTARG};;
h) help
exit;;
\?)
echo "Error: Invalid option"
exit;;
esac
done
if [ -z "$ntfy_message" ]; then
ntfy_message="Done"
fi
if [ "$ntfy_prio" == "1" ]; then
ntfy_prio="min"
ntfy_tag="white_small_square"
elif [ "$ntfy_prio" == "2" ]; then
ntfy_prio="low"
ntfy_tag="computer"
elif [ "$ntfy_prio" == "3" ]; then
ntfy_prio="default"
ntfy_tag="computer"
elif [ "$ntfy_prio" == "4" ]; then
ntfy_prio="high"
ntfy_tag="warning"
elif [ "$ntfy_prio" == "5" ]; then
ntfy_prio="max"
ntfy_tag="rotating_light"
else
ntfy_prio="default"
ntfy_tag="computer"
fi
if [ -n "$ntfy_emoji" ]; then
ntfy_tag="$ntfy_emoji"
fi
if [ -n "$le" ]; then
if [ "$le" == "0" ]; then
ntfy_tag="heavy_check_mark"
else
ntfy_tag="x"
fi
fi
if [ -z "$ntfy_topic" ]; then
ntfy_topic="$HOSTNAME"
fi
if [[ -n $ntfy_password && -n $ntfy_token ]]; then
echo "Use ntfy_username and ntfy_password OR ntfy_token"
exit 1
elif [ -n "$ntfy_password" ]; then
ntfy_base64=$( echo -n "$ntfy_username:$ntfy_password" | base64 )
ntfy_auth="Authorization: Basic $ntfy_base64"
elif [ -n "$ntfy_token" ]; then
ntfy_auth="Authorization: Bearer $ntfy_token"
else
ntfy_auth=""
fi
curl -s -H "$ntfy_auth" -H "tags:"$ntfy_tag -H "icon:"$ntfy_icon -H "prio:"$ntfy_prio -H "X-Title: $ntfy_topic" -d "$ntfy_message" "$ntfy_url/$ntfy_ntfy_topic" > /dev/null