Skip to content

Instantly share code, notes, and snippets.

@arjones
Last active September 9, 2023 22:20
Show Gist options
  • Save arjones/9750b39a26c63b9d3775b5334cadf11b to your computer and use it in GitHub Desktop.
Save arjones/9750b39a26c63b9d3775b5334cadf11b 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
# 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