forked from hipchat/hipchat-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhipchat_room_message
More file actions
executable file
·132 lines (117 loc) · 3.68 KB
/
hipchat_room_message
File metadata and controls
executable file
·132 lines (117 loc) · 3.68 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
###############################################################################
#
# ./hipchat_room_message
#
# A script for sending a system message to a room.
#
# Docs: http://github.com/hipchat/hipchat-cli
#
# Usage:
# cat message.txt | ./hipchat_room_message -t <token> -r 1234 -f "System"
# echo -e "New\nline" | ./hipchat_room_message -t <token> -r 1234 -f "System"
#
###############################################################################
# exit on failure
set -e
usage() {
cat << EOF
Usage: $0 -t <token> -r <room id> -f <from name>
This script will read from stdin and send the contents to the given room as
a system message.
OPTIONS:
-h Show this message
-t <token> API token
-r <room id> Room ID
-f <from name> From name
-c <color> Message color (yellow, red, green, purple or random - default: yellow)
-m <format> Message format (html or text - default: html)
-i <input> Optional: Input to send to room (default: stdin)
-l <level> Nagios message level (critical, warning, unknown, ok, down, up). Will override color.
-n Trigger notification for people in the room
-o API host (api.hipchat.com)
-v <version> API version (default: v1)
EOF
}
# Include hipchat defaults if available
test -f /etc/hipchat && . /etc/hipchat
TOKEN=${HIPCHAT_TOKEN:-}
ROOM_ID=${HIPCHAT_ROOM_ID:-}
FROM=${HIPCHAT_FROM:-}
COLOR=${HIPCHAT_COLOR:-yellow}
FORMAT=${HIPCHAT_FORMAT:-html}
MESSAGE=${HIPCHAT_MESSAGE:-html}
NOTIFY=${HIPCHAT_NOTIFY:-0}
HOST=${HIPCHAT_HOST:-api.hipchat.com}
LEVEL=${HIPCHAT_LEVEL:-}
API=${HIPCHAT_API:-v1}
while getopts “ht:r:f:c:m:o:i:l:v:n” OPTION; do
case $OPTION in
h) usage; exit 1;;
t) TOKEN=$OPTARG;;
r) ROOM_ID=$OPTARG;;
f) FROM=$OPTARG;;
c) COLOR=$OPTARG;;
m) FORMAT=$OPTARG;;
n) NOTIFY=1;;
i) INPUT=$OPTARG;;
l) LEVEL=$OPTARG;;
o) HOST=$OPTARG;;
v) API=$OPTARG;;
[?]) usage; exit;;
esac
done
# check for required args
if [[ -z $TOKEN ]] || [[ -z $ROOM_ID ]] || [[ -z $FROM ]]; then
usage
exit 1
fi
# nagios levels
if [ ! -z "$LEVEL" ]; then
if [[ $LEVEL == 'CRITICAL' ]] || [[ $LEVEL == 'critical' ]]; then
COLOR="red";
elif [[ $LEVEL == 'WARNING' ]] || [[ $LEVEL == 'warning' ]]; then
COLOR="yellow";
elif [[ $LEVEL == 'UNKNOWN' ]] || [[ $LEVEL == 'unknown' ]]; then
COLOR="gray";
elif [[ $LEVEL == 'OK' ]] || [[ $LEVEL == 'ok' ]]; then
COLOR="green";
elif [[ $LEVEL == 'DOWN' ]] || [[ $LEVEL == 'down' ]]; then
COLOR="red";
elif [[ $LEVEL == 'UP' ]] || [[ $LEVEL == 'up' ]]; then
COLOR="green";
fi
fi
if [ -z "$INPUT" ]; then
# read stdin
INPUT=$(cat)
fi
# replace newlines with XHTML <br>
if [ $FORMAT == 'html' ]; then
INPUT=$(echo -n "${INPUT}" | sed "s/$/\<br\>/")
fi
# replace bare URLs with real hyperlinks
INPUT=$(echo -n "${INPUT}" | perl -p -e "s/(?<!href=\"|href=')((?:https?|ftp|mailto)\:\/\/[^ \n]*)/\<a href=\"\1\"\>\1\<\/a>/g")
# urlencode with perl
if [ $API == 'v1' ]; then
INPUT=$(echo -n "${INPUT}" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg')
fi
# replace notification boolean from 1/0 to 'true'/'false' for API v2
if [ $API == 'v2' ]; then
if [ $NOTIFY -eq 0 ]; then
NOTIFY='false'
else
NOTIFY='true'
fi
fi
# do the curl
if [ $API == 'v2' ]; then
curl -sS \
-H 'Content-type: application/json' \
-d "{\"color\":\"$COLOR\", \"message\":\"$INPUT\", \"message_format\":\"$FORMAT\", \"notify\":$NOTIFY}" \
https://$HOST/v2/room/$ROOM_ID/notification?auth_token=$TOKEN
else
curl -sS \
-d "auth_token=$TOKEN&room_id=$ROOM_ID&from=$FROM&color=$COLOR&message_format=$FORMAT&message=$INPUT¬ify=$NOTIFY" \
https://$HOST/v1/rooms/message
fi