Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 130 lines (115 sloc) 2.8 KB
#!/bin/bash
#+---------------------------------------+
#| hipchat |
#| author: Donald Merand |
#+---------------------------------------+
#| post a message to a HipChat room |
#| not dissimilar to |
#| https://github.com/hipchat/hipchat-cli|
#+---------------------------------------+
HOST='api.hipchat.com'
#get your own auth token from https://www.hipchat.com/admin/api
AUTH_TOKEN="YOUR_AUTH_KEY" #hard-coded Proof API key for now
ROOM_ID="DEFAULT_ROOM_ID"
FROM="CommandLine"
NOTIFY=1 #boolean
COLOR="red" #can be red, yellow, green, purple, or random
MESSAGE_FORMAT="text" #can be text or html
show_help() {
cat <<HELP
HIPCHAT MESSAGE SENDER
Usage - \`hipchat <options> "message"\`
Options:
-h - Show this help screen
-l - List HipChat rooms
-u - List HipChat users
-f - Specify sender (up to 15 characters)
-r - Specify HipChat room ID
-a - Specify HipChat API token
-n - Trigger notifications in this message (values should be 0 or 1)
-o - Message format (text or html, default is html)
-c - Specify color (yellow, red, green, purple, random)
HELP
}
#test for existence of required programs
test_existence() {
program=$1
which ${program} &> /dev/null;
if [[ "$?" != "0" ]];
then
echo "'$program' appears to not be installed, check your PATH variable"
exit 1
fi
}
test_existence curl
#list available hipchat rooms
list_rooms() {
curl -sS "https://api.hipchat.com/v1/rooms/list?auth_token=$AUTH_TOKEN"
}
#list available hipchat users
list_users() {
curl -sS "https://api.hipchat.com/v1/users/list?auth_token=$AUTH_TOKEN"
}
while getopts ":hluf:r:a:n:c:o:" OPTNAME
do
case "$OPTNAME" in
"h")
show_help
exit
;;
"l")
list_rooms
exit
;;
"u")
list_users
exit
;;
"f")
FROM=$OPTARG
;;
"r")
ROOM_ID=$OPTARG
;;
"a")
AUTH_TOKEN=$OPTARG
;;
"n")
NOTIFY=$OPTARG
;;
"c")
COLOR=$OPTARG
;;
"o")
MESSAGE_FORMAT=$OPTARG
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
done
#now get the non-options, in this case `MESSAGE`
shift $(($OPTIND - 1))
MESSAGE="$@"
#test params to make sure they're not empty
if [ -z "$MESSAGE" ]
then
MESSAGE="$(cat)"
fi
#now actually send the message
curl -sS \
--data-urlencode "auth_token=$AUTH_TOKEN" \
--data-urlencode "room_id=$ROOM_ID" \
--data-urlencode "from=$FROM" \
--data-urlencode "color=$COLOR" \
--data-urlencode "notify=$NOTIFY" \
--data-urlencode "message_format=$MESSAGE_FORMAT" \
--data-urlencode "message=$MESSAGE" \
https://$HOST/v1/rooms/message