Skip to content

Instantly share code, notes, and snippets.

@GaryRogers
Created March 18, 2015 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GaryRogers/7f7035edf40a679ff9c4 to your computer and use it in GitHub Desktop.
Save GaryRogers/7f7035edf40a679ff9c4 to your computer and use it in GitHub Desktop.
# ==[ 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