Created
March 18, 2015 13:53
-
-
Save GaryRogers/7f7035edf40a679ff9c4 to your computer and use it in GitHub Desktop.
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
# ==[ printSlack ]============================================================= | |
# Function to send output from the commandline to Slack. | |
# (wants SLACK_TOKEN to be defined in .bashrc or other ENV method, or you can set it here.) | |
# | |
# @parameter string $LEVEL INFO/ERROR/WARNING message. Changes emoji | |
# @parameter string $MESSAGE Message to send to slack. | |
printSlack() | |
{ | |
SLACK_HOSTNAME=${SLACK_HOSTNAME-'mycompany.slack.com'}; | |
SLACK_TOKEN=${SLACK_TOKEN-'oops'}; | |
SLACK_CHANNEL=${SLACK_CHANNEL-'#devops'}; | |
SLACK_BOTNAME=${SLACK_BOTNAME-$(hostname -s)}; | |
SLACK_BOTEMOJI=${SLACK_BOTEMOJI-':computer:'} | |
SEVERITY=${1-'INFO'}; | |
ICON=':slack:'; | |
case "$SEVERITY" in | |
INFO) | |
ICON=':page_with_curl:'; | |
shift; | |
;; | |
WARN|WARNING) | |
ICON=':warning:'; | |
shift; | |
;; | |
ERROR|ERR) | |
ICON=':bangbang:'; | |
shift; | |
;; | |
*) | |
ICON=':slack:'; | |
;; | |
esac | |
MESSAGE=$@; | |
PAYLOAD="payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"text\": \"${ICON} ${MESSAGE}\", \"icon_emoji\": \"${SLACK_BOTEMOJI}\"}"; | |
CURL_RESULT=$(curl -s -S -X POST --data-urlencode "$PAYLOAD" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}); | |
if [ -z "$CURL_RESULT" ]; then | |
return 0; | |
else | |
return 1; | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment