-
-
Save darioguarascio/4e859d1ed04a120ddce9532066105a37 to your computer and use it in GitHub Desktop.
Shell/Bash script for sending slack messages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#################################################################################### | |
# Slack Bash console script for sending messages. | |
#################################################################################### | |
# Installation | |
# $ curl -s https://gist.githubusercontent.com/disfasia/4e859d1ed04a120ddce9532066105a37/raw --output /usr/bin/slack | |
# $ chmod +x /usr/bin/slack | |
#################################################################################### | |
# USAGE | |
# Send message to slack channel/user | |
# Send a message to the channel #ch-01 | |
# $ slack '#ch-01' 'Some message here.' | |
# | |
# Send a message to the channel #ch-01 and user @me. | |
# $ slack '#ch-01,@me' MESSAGE | |
# | |
# Send a message to the default channel (it must be declared in APP_SLACK_CHANNEL). | |
# $ slack MESSAGE | |
# | |
# VARIABLES | |
# | |
# Please declare environment variables: | |
# - APP_SLACK_WEBHOOK | |
# - APP_SLACK_CHANNEL (optional) | |
# - APP_SLACK_USERNAME (optional) | |
# - APP_SLACK_ICON_EMOJI (optional) | |
# | |
# To add an attachment: | |
# - APP_SLACK_ATTACHMENT (optional) | |
# - APP_SLACK_ATTACHMENT_COLOR (optional) | |
# - APP_SLACK_ATTACHMENT_TITLE (optional) | |
# | |
# You may also declare them in ~/.slackrc file. | |
#################################################################################### | |
set -o pipefail | |
set -o errexit | |
set -o nounset | |
#set -o xtrace | |
init_params() { | |
# you may declare ENV vars in /etc/profile.d/slack.sh | |
if [ -z "${APP_SLACK_WEBHOOK:-}" ]; then | |
echo 'error: Please configure Slack environment variable: ' > /dev/stderr | |
echo ' APP_SLACK_WEBHOOK' > /dev/stderr | |
exit 2 | |
fi | |
APP_SLACK_USERNAME=${APP_SLACK_USERNAME:-$(hostname | cut -d '.' -f 1)} | |
APP_SLACK_ICON_EMOJI=${APP_SLACK_ICON_EMOJI:-:slack:} | |
if [ -z "${1:-}" ]; then | |
echo 'error: Missed required arguments.' > /dev/stderr | |
echo 'note: Please follow this example:' > /dev/stderr | |
echo ' $ slack.sh "#CHANNEL1,CHANNEL2" Some message here. ' > /dev/stderr | |
exit 3 | |
fi | |
slack_channels=(${APP_SLACK_CHANNEL:-}) | |
if [ "${1::1}" == '#' ] || [ "${1::1}" == '@' ]; then | |
# explode by comma | |
IFS=',' read -r -a slack_channels <<< "${1}" | |
shift | |
fi | |
slack_message=${@} | |
if [ ! -z "${APP_SLACK_ATTACHMENT:-}" ]; then | |
APP_SLACK_ATTACHMENT="$(printf '{ "fallback": "%s", "color": "%s", "fields": [ { "title": "%s", "value": "%s", "short": false } ] }' \ | |
"${slack_message}" \ | |
"${APP_SLACK_ATTACHMENT_COLOR:-good}" \ | |
"${APP_SLACK_ATTACHMENT_TITLE:-Attachment}" \ | |
"${APP_SLACK_ATTACHMENT}" \ | |
)" | |
else | |
APP_SLACK_ATTACHMENT=''; | |
fi | |
} | |
send_message() { | |
local channel=${1} | |
echo 'Sending to '${channel}'...' | |
curl --silent --data-urlencode \ | |
"$(printf 'payload={"text": "%s", "channel": "%s", "username": "%s", "as_user": "true", "link_names": "true", "icon_emoji": "%s", "attachments": [ %s ] }' \ | |
"${slack_message}" \ | |
"${channel}" \ | |
"${APP_SLACK_USERNAME}" \ | |
"${APP_SLACK_ICON_EMOJI}" \ | |
"${APP_SLACK_ATTACHMENT}" \ | |
)" \ | |
${APP_SLACK_WEBHOOK} || true | |
echo | |
} | |
send_message_to_channels() { | |
for channel in "${slack_channels[@]:-}"; do | |
send_message "${channel}" | |
done | |
} | |
slack() { | |
# Set magic variables for current file & dir | |
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" | |
readonly __dir __file | |
cd ${__dir} | |
if [ -f $(cd; pwd)/.slackrc ]; then | |
. $(cd; pwd)/.slackrc | |
fi | |
declare -a slack_channels | |
init_params ${@} | |
send_message_to_channels | |
} | |
if [ "${BASH_SOURCE[0]:-}" != "${0}" ]; then | |
export -f slack | |
else | |
slack ${@} | |
exit $? | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment