Skip to content

Instantly share code, notes, and snippets.

@MaximilianoFelice
Forked from arjones/notify-slack.sh
Last active February 21, 2017 16:05
Show Gist options
  • Save MaximilianoFelice/3126e9b86ae60ecff30efd98033986d5 to your computer and use it in GitHub Desktop.
Save MaximilianoFelice/3126e9b86ae60ecff30efd98033986d5 to your computer and use it in GitHub Desktop.
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
#
# Original script from:
# Copyright (C) 2016 Gustavo Arjones (@arjones)
#
# Some configuration and notification changes were added by:
# 2017 Maximiliano Felice
#
# 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 slack-conf() {
cat ~/.notify-slack-cfg | grep -v "^#" | grep . | sed "${1}q;d"
}
function get-notifications() {
echo " and they told me to notify: ${SLACK_NOTIFICATIONS}"
}
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=$(slack-conf 1)
local SLACK_URL=https://hooks.slack.com/services/${SLACK_WEBHOOK_SERVICE}
local SLACK_NOTIFICATIONS=$(slack-conf 2)
case "${SLACK_MESSAGE}" in
INFO*)
SLACK_ICON=':information_source: '
;;
WARN*)
SLACK_ICON=':warning: '
;;
ERROR*)
SLACK_ICON=':bangbang: '
local NOTIFIED_INFO=$(get-notifications)
;;
*)
SLACK_ICON=''
;;
esac
local HOSTNAME_INFO="\nSource: $(hostname)"
local PAYLOAD="payload={\"text\": \"${SLACK_ICON}${SLACK_MESSAGE}${NOTIFIED_INFO}${HOSTNAME_INFO}.\", \"icon_emoji\": \"${SLACK_NOTIFY_ICON}\", \"username\": \"Bash Notifier\"}"
echo "DEBUG: PAYLOAD: ${PAYLOAD}"
echo "DEBUG: SLACK_URL: ${SLACK_URL}"
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