-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 267edf0
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
|
||
############################################################################### | ||
# | ||
# ./hipchat_cli | ||
# | ||
# A script for sending a system message to a room. | ||
# | ||
# 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) | ||
-n Trigger notification for people in the room | ||
-o API host (api.hipchat.com) | ||
EOF | ||
} | ||
|
||
TOKEN= | ||
ROOM_ID= | ||
FROM= | ||
COLOR= | ||
MESSAGE= | ||
NOTIFY=0 | ||
HOST='api.hipchat.com' | ||
while getopts “ht:r:f:c:o:n” OPTION; do | ||
case $OPTION in | ||
h) usage; exit 1;; | ||
t) TOKEN=$OPTARG;; | ||
r) ROOM_ID=$OPTARG;; | ||
f) FROM=$OPTARG;; | ||
c) COLOR=$OPTARG;; | ||
n) NOTIFY=1;; | ||
o) HOST=$OPTARG;; | ||
[?]) usage; exit;; | ||
esac | ||
done | ||
|
||
# Check for required args | ||
if [[ -z $TOKEN ]] || [[ -z $ROOM_ID ]] || [[ -z $FROM ]]; then | ||
usage | ||
exit 1 | ||
fi | ||
|
||
# Read stdin | ||
INPUT=$(cat) | ||
|
||
# Replace newlines with XHTML <br> | ||
INPUT=$(echo -n "${INPUT}" | sed "s/$/\<br\>/") | ||
|
||
# Replace bare URLs with real hyperlinks | ||
INPUT=$(echo -n "${INPUT}" | perl -p -e "s/(?<!href=\")((?:https?|ftp|mailto)\:\/\/[^ \n]*)/\<a href=\"\1\"\>\1\<\/a>/g") | ||
|
||
# Urlencode with perl | ||
INPUT=$(echo -n "${INPUT}" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg') | ||
|
||
# Curl | ||
curl -sS \ | ||
-d "auth_token=$TOKEN&room_id=$ROOM_ID&from=$FROM&color=$COLOR&message=$INPUT¬ify=$NOTIFY" \ | ||
https://$HOST/v1/rooms/message |