Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Created December 8, 2015 17:36
Show Gist options
  • Save Geekfish/dd373914ebbb3ed46430 to your computer and use it in GitHub Desktop.
Save Geekfish/dd373914ebbb3ed46430 to your computer and use it in GitHub Desktop.
Hipchat - Send message to room
#!/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&notify=$NOTIFY" \
https://$HOST/v1/rooms/message
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment