Created
September 10, 2018 17:32
-
-
Save abachman/ca5b2147b404119d5fe0c573a7c36a3f to your computer and use it in GitHub Desktop.
slack-post shell script
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
#!/usr/bin/env bash | |
############################################################################### | |
# | |
# ./slack-post | |
# | |
# A script for sending a system message to a channel. | |
# | |
# Docs: | |
# | |
# Usage: | |
# cat message.txt | ./slack-post -f "System" | |
# echo -e "New\nline" | ./slack-post -f "System" | |
# | |
############################################################################### | |
# exit on failure | |
set -e | |
# http://apple.stackexchange.com/questions/68684/why-sh-c-echo-n-1-is-different-from-bash-c-echo-n-1 | |
shopt -u xpg_echo | |
usage() { | |
cat << EOF | |
Usage: $0 [-f <from name> -r <color>] | |
This script will read from stdin and send the contents to the given channel. | |
OPTIONS: | |
-h Show this message | |
-f <from name> Bot name | |
EOF | |
} | |
# Include hipchat defaults if available | |
test -f /etc/slackrc && . /etc/slackrc | |
if [ -f ~/.slackrc ]; then | |
echo 'found .slackrc config' | |
source ~/.slackrc | |
fi | |
FROM=${SLACK_FROM:-} | |
WEBHOOK_URL=${SLACK_WEBHOOK_URL:-} | |
if [ -z "$WEBHOOK_URL" ]; then | |
cat << EOF | |
Configure slack-post by creating a config file at ~/.slackrc or /etc/slackrc | |
and adding your Incoming Webhook app's URL: | |
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/SOMETHING/LIKE/THISSECRETURL" | |
EOF | |
usage | |
exit 1 | |
fi | |
while getopts "hf:c:" OPTION; do | |
case $OPTION in | |
h) usage; exit 1;; | |
f) FROM=$OPTARG;; | |
[?]) usage; exit;; | |
esac | |
done | |
if [ -z "$INPUT" ]; then | |
# read stdin | |
INPUT=$(cat) | |
fi | |
PAYLOAD="payload={\"text\": \"$INPUT\"" | |
if [ -n "$FROM" ]; then | |
PAYLOAD="$PAYLOAD,\"username\":\"$FROM\"" | |
fi | |
PAYLOAD="$PAYLOAD}" | |
curl -X POST --data-urlencode "$PAYLOAD" $WEBHOOK_URL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment