Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper function for using Slack WebHooks in Linux
#!/bin/bash
#
# Slack Helper Functions
#
#The MIT License (MIT)
#
#Copyright (c) 2015 "whiteboardcoder" Patrick Bailey
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#
########################
#The Slack URL for your webhook
SLACK_URL_HOOK="https://hooks.slack.com/services/REPLACE_WITH_YOUR_URL!!!"
#SLACK_URL_HOOK CHECK
if [[ $SLACK_URL_HOOK == *"REPLACE_WITH_YOUR_URL!!!"* ]]
then
echo "Update your SLACK_URL_HOOK Variable!"
exit 1
fi
set_variables() {
USERNAME=$1
EMOJI=$2
ROOM=$3
TITLE_1="$4"
MSG_1="$5"
TITLE_2="$6"
MSG_2="$7"
}
post() {
if [ -n "$TITLE_2" ] && [ -n "$MSG_2" ]
then
post_double_msg
elif [ -n "$TITLE_1" ] && [ -n "$MSG_1" ]
then
post_single_msg
else
echo "No Message provided"
return 1
fi
}
post_single_msg() {
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel" : "#'"$ROOM"'",
"username" : "'"$USERNAME"'",
"icon_emoji" : ":'"$EMOJI"':",
"attachments" : [
{
"fallback" : "'"$TITLE_1"'",
"color" : "'"$COLOR"'",
"fields" : [
{
"title" : "'"$TITLE_1"'",
"value" : "'"$MSG_1"'"
}
]
}
]
}' $SLACK_URL_HOOK
}
post_double_msg() {
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel" : "#'"$ROOM"'",
"username" : "'"$USERNAME"'",
"icon_emoji" : ":'"$EMOJI"':",
"attachments" : [
{
"fallback" : "'"$TITLE_1"'",
"color" : "'"$COLOR"'",
"fields" : [
{
"title" : "'"$TITLE_1"'",
"value" : "'"$MSG_1"'",
"short" : true
},
{
"title" : "'"$TITLE_2"'",
"value" : "'"$MSG_2"'",
"short" : true
}
]
}
]
}' $SLACK_URL_HOOK
}
info() {
COLOR="#d3d3d3"
set_variables "$1" "$2" "$3" "$4" "$5" "$6" "$7"
post
}
good() {
COLOR="good"
set_variables "$1" "$2" "$3" "$4" "$5" "$6" "$7"
post
}
warning() {
COLOR="warning"
set_variables "$1" "$2" "$3" "$4" "$5" "$6" "$7"
post
}
bad () {
COLOR="danger"
set_variables "$1" "$2" "$3" "$4" "$5" "$6" "$7"
post
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment