Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Notify Slack - Send notifications to a Slack Channel - http://arjon.es/2016/09/15/be-notified-on-slack-when-a-long-process-finishes/
# Notify Slack - Send notifications to a Slack Channel
# Copyright (C) 2016 Gustavo Arjones (@arjones)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# Full blog post here: http://arjon.es/2016/09/15/be-notified-on-slack-when-a-long-process-finishes/
#
function notify-slack () {
# Change the Icon when a notification is sent
#
local SLACK_NOTIFY_ICON=":ghost:"
if [ ! -f ~/.notify-slack-cfg ]; then
echo "ERROR: A Webhook URL is required. Create yours here: https://my.slack.com/services/new/incoming-webhook/"
echo "Once you have your KEY, please create a file at ${HOME}/.notify-slack-cfg containng only the KEY. Eg: T02TKKSAX/B246MJ6HX/WXt2BWPfNhSKxdoFNFblczW9"
return
fi
local SLACK_MESSAGE="$1"
local SLACK_WEBHOOK_SERVICE=$(head -1 ~/.notify-slack-cfg)
local SLACK_URL=https://hooks.slack.com/services/${SLACK_WEBHOOK_SERVICE}
case "${SLACK_MESSAGE}" in
INFO*)
SLACK_ICON=':information_source: '
;;
WARN*)
SLACK_ICON=':warning: '
;;
ERROR*)
SLACK_ICON=':bangbang: '
;;
*)
SLACK_ICON=''
;;
esac
local PAYLOAD="payload={\"text\": \"${SLACK_ICON}${SLACK_MESSAGE} from $(hostname)\", \"icon_emoji\": \"${SLACK_NOTIFY_ICON}\", \"username\": \"Bash Notifier\"}"
curl -XPOST --data-urlencode "${PAYLOAD}" ${SLACK_URL} #&>/dev/null
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment