-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdadapush.sh
executable file
·87 lines (71 loc) · 1.86 KB
/
dadapush.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
#!/usr/bin/env bash
# Default config vars
CURL="$(which curl)"
DADAPUSH_URL="https://www.dadapush.com/api/v1/message"
TOKEN=""
CURL_OPTS=""
# Functions used elsewhere in this script
usage() {
echo "${0} -T <TOKEN> -M <msg_file>"
exit 1
}
send_message() {
curl_cmd="\"${CURL}\" --silent --write-out "HTTPSTATUS:%{http_code}" -X POST \"${DADAPUSH_URL}\" \
${CURL_OPTS} \
-H 'content-type: application/json' \
-H 'x-channel-token: ${TOKEN}' \
-d @\"${message}\"
"
# echo $curl_cmd
# execute and return exit code from curl command
HTTP_RESPONSE="$(eval "${curl_cmd}")"
# extract the body
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
# extract the status
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
# print the body
echo "$HTTP_BODY"
# example using the status
if [ ! $HTTP_STATUS -eq 200 ]; then
echo "Error [HTTP status: $HTTP_STATUS]"
exit 1
fi
r="${?}"
if [ "${r}" -ne 0 ]; then
echo "${0}: Failed to send message" >&2
fi
return "${r}"
}
# Option parsing
optstring="T:m:"
# Process the remaining options
OPTIND=1
while getopts ${optstring} c; do
case ${c} in
T) TOKEN="${OPTARG}" ;;
m) msg_file="${OPTARG}" ;;
[h\?]) usage ;;
esac
done
shift $((OPTIND-1))
# Is there anything left?
if [ "$#" -lt 1 -a "$msg_file" = "" ]; then
usage
fi
message="$*"
# load the rest of the message from the file
if [ "$msg_file" != "" ] ; then
if [ ! -f "$msg_file" ] ; then
echo "failed to read message file: $msg_file"
exit 1
fi
message="$msg_file"
fi
# Check for required config variables
if [ ! -x "${CURL}" ]; then
echo "CURL is unset, empty, or does not point to curl executable. This script requires curl!" >&2
exit 1
fi
send_message
r=${?}
exit "${r}"