Skip to content

Instantly share code, notes, and snippets.

@halcyonardency
Created July 2, 2015 22:02
Show Gist options
  • Save halcyonardency/deeb67dca39820d6f573 to your computer and use it in GitHub Desktop.
Save halcyonardency/deeb67dca39820d6f573 to your computer and use it in GitHub Desktop.
Slack notify script
#!/bin/sh
# must set:
# - slacktoken (token part of slack hook url)
# - slackchannel (channel to send to)
# - slackbotname (bot name)
# - slicon (default bot icon)
# - slacknotice (true/false, false - supress sending slack notices)
#
if [ -z "${slacktoken}" ]; then
slacktoken='l0lw4t'
fi
if [ -z "${slackchannel}" ]; then
slackchannel='test'
fi
if [ -z "${slackbotname}" ]; then
slackbotname=autobot
fi
if [ -z "${slicon}" ]; then
slicon=gotime
fi
function msgslack {
# usage: msgslack <token> <channel> <message>
if [ -z "${slacknotice}" ] && [ "${slacknotice}" != "true" ]; then
echo "### Supressing Slack notice"
return 0
fi
token=$1
if [[ $token == "" ]]; then
echo "## No token specified for Slack API"
return 1
fi
shift
channel=$1
if [[ $channel == "" ]]; then
echo "## No channel specified for Slack"
return 1
fi
shift
color=$1
if [[ $color == "" ]]; then
echo "## No text color specified for Slack"
return 1
fi
shift
text="$*"
if [[ $text == "" ]]; then
echo "## No text specified for Slack"
return 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{ \"channel\": \"#${channel}\", \"username\": \"${slackbotname}\", \"icon_emoji\": \":${slicon}:\", \"attachments\": [{ \"text\": \"${escapedText}\", \"unfurl_links\": \"false\", \"mrkdwn_in\": [ \"text\" ], \"color\": \"#${color}\" }] }"
echo "### Sending Slack notification to channel ${channel}"
curl --insecure -s --data-urlencode "payload=${json}" "https://hooks.slack.com/services/${token}"
}
## example using jenkins vars
msgslack ${slacktoken} ${slackchannel} 666666 "*Starting* buildm\n> <${BUILD_URL}|${BUILD_TAG}>"
. ./do-things.sh
buildcode=$?
if [ ${buildcode} -ne 0 ]; then
finalcolor=FF0000
finalicon=":thumbsdown:"
else
finalcolor=00FF00
finalicon=":thumbsup:"
fi
msgslack ${slacktoken} ${slackchannel} ${finalcolor} "*Completed* build\n> <${BUILD_URL}|${BUILD_TAG}> ${finalicon}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment