Skip to content

Instantly share code, notes, and snippets.

@tamuhey
Last active October 10, 2019 10:36
Show Gist options
  • Save tamuhey/05dccb202e0065ca8f03c4d4e369a84f to your computer and use it in GitHub Desktop.
Save tamuhey/05dccb202e0065ca8f03c4d4e369a84f to your computer and use it in GitHub Desktop.
notify-slack
#!/bin/bash
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
SLACK_MESSAGE="$1"
SLACK_WEBHOOK_SERVICE=$(head -1 ~/.notify-slack-cfg)
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment