-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendmsg.sh
executable file
·116 lines (109 loc) · 2.89 KB
/
sendmsg.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
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
#!/bin/bash
# Send a message to firebase using cloud messaging.
# Usage:
# Send a message with default title and body to Firebase:
# ./sendmsg.sh
# Send a message with the specified title to Firebase.
# sendmsg.sh [title]
# Set the default body
body=$(echo "You have been notified"|base64)
body_overridden=0
message_token=""
server_key=""
# Override the body depending on the passed flags.
while getopts :m:s:b:ht opt; do
case $opt in
m)
message_token="${OPTARG}"
echo "${OPTARG}" > ~/.sendmsg-message-token
;;
s)
server_key="${OPTARG}"
echo "${OPTARG}" > ~/.sendmsg-server-key
;;
b)
if ((body_overridden)); then
echo "body was already overridden, skipping -b flag"
continue
fi
echo "overriding notification body"
body=$(echo "${OPTARG}"|base64)
body_overridden=1
;;
h)
if ((body_overridden)); then
echo "body was already overridden, skipping -h flag"
continue
fi
if [[ -t 0 ]]; then
echo "option -h is not valid in an interactive shell"
exit 1
fi
echo "reading body from stdin"
body=$(head -c 2560|base64)
body_overridden=1
;;
t)
if ((body_overridden)); then
echo "body was already overridden, skipping -t flag"
continue
fi
if [[ -t 0 ]]; then
echo "option -t is not valid in an interactive shell"
exit 1
fi
echo "reading body from stdin"
body=$(tail -c 2560|base64)
body_overridden=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Get rid of the processed options and arguments.
shift $((OPTIND - 1))
# Set the title.
title="Boop"
if [[ -n "$*" ]]; then
title="$*"
fi
# If we didn't set the server key and message token with arguments, read them
# from the config files.
if [[ -z "${server_key}" ]]; then
if [[ ! -f ~/.sendmsg-server-key ]]; then
echo "No server key - configure one with the -s argument"
exit 1
fi
server_key=$(cat ~/.sendmsg-server-key)
fi
if [[ -z "${message_token}" ]]; then
if [[ ! -f ~/.sendmsg-message-token ]]; then
echo "No message token - configure one with the -m argument"
exit 1
fi
message_token=$(cat ~/.sendmsg-message-token)
fi
# Generate a message id using uuidgen or fallback to the time
id=$(uuidgen || date +%s.%N)
timestamp=$(date +%s)
resp=$(curl -s -X POST \
-H "Authorization: key=${server_key}" \
-H "Content-Type: application/json" \
-d "{
\"data\": {
\"title\": \"${title}\",
\"body\": \"${body}\",
\"id\": \"${id}\",
\"timestamp\": \"${timestamp}\",
\"time_to_live\": \"86400\",
},
\"to\": \"${message_token}\"
}" \
https://fcm.googleapis.com/fcm/send)
echo "${resp}"