-
-
Save chusiang/df6610ada89def355bfd8a5534416446 to your computer and use it in GitHub Desktop.
Post a message to a Slack channel
This file contains hidden or 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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's can work on Rocket.Chat. 🚀