Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Created November 2, 2018 23:09
Show Gist options
  • Save dishbreak/7d861d8b19804e4d93a6af3fff7e07d9 to your computer and use it in GitHub Desktop.
Save dishbreak/7d861d8b19804e4d93a6af3fff7e07d9 to your computer and use it in GitHub Desktop.
Send a message to a Slack Webhook
#!/bin/bash
set -e
## Take in the Webhook URL from Slack and the actual message to send.
if [[ $# -ne 2 ]]; then
( >&2 echo "Usage $(basename "$0") WEBHOOK_URL MESSAGE" )
exit 1
fi
WEBHOOK_URL=$1
MESSAGE=$2
## Using a heredoc means you can type unescaped quotes, use interpolation and even whitespace to indent!
PAYLOAD=$(cat <<EOF
{"text": "$MESSAGE"}
EOF
)
# Flags used:
## --silent - no annoying progress bar.
## --request - the HTTP verb.
## --header - Never send JSON without saying it's JSON!
## --data - the body.
curl --silent \
--request POST \
--header "Content-Type: application/json" \
--data "$PAYLOAD" \
"$WEBHOOK_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment