Skip to content

Instantly share code, notes, and snippets.

@chusiang
Forked from dopiaza/slackpost
Last active March 28, 2018 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chusiang/df6610ada89def355bfd8a5534416446 to your computer and use it in GitHub Desktop.
Save chusiang/df6610ada89def355bfd8a5534416446 to your computer and use it in GitHub Desktop.
Post a message to a Slack channel
#!/bin/bash
# ============================================================
# Author: Chu-Siang Lai / chusiang (at) drx.tw
# Blog: http://note.drx.tw
# Filename: slackpost.sh
# Modified: 2018-03-28 15:58
# Description: Post a message to a Slack channel.
# Reference:
#
# - https://gist.github.com/dopiaza/6449505#gistcomment-1627214
#
# ===========================================================
# Help.
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo 'Usage: slackpost.sh "<webhook_url>" "<channel>" "<username>" "<color>" "<message>"'
exit 0
fi
# webhook & token.
WEBHOOK_URL=$1
if [[ $WEBHOOK_URL == "" ]]
then
echo "No webhook_url specified"
exit 1
fi
shift
# channel.
CHANNEL=$1
if [[ $CHANNEL == "" ]]
then
echo "No channel specified"
exit 1
fi
shift
# username.
USERNAME=$1
if [[ $USERNAME == "" ]]
then
echo "No username specified"
exit 1
fi
shift
# color.
COLOR=$1
if [[ $COLOR == "" ]]
then
echo "No status specified"
exit 1
fi
shift
# text.
TEXT=$*
if [[ $TEXT == "" ]]
then
echo "No text specified"
exit 1
fi
# convert formating.
MESSAGE=$( echo $TEXT | sed 's/"/\"/g' | sed "s/'/\'/g" )
JSON="{\"channel\": \"$CHANNEL\", \"username\":\"$USERNAME\", \"attachments\":[{\"color\":\"$COLOR\" , \"text\": \"$MESSAGE\"}]}"
# post to slack.
curl -s -d "payload=$JSON" "$WEBHOOK_URL" > /dev/null
@chusiang
Copy link
Author

It's can work on Rocket.Chat. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment