Skip to content

Instantly share code, notes, and snippets.

@elnygren
Last active March 28, 2022 00:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save elnygren/eb19e50cc067174c8689d2cd0f45d058 to your computer and use it in GitHub Desktop.
Save elnygren/eb19e50cc067174c8689d2cd0f45d058 to your computer and use it in GitHub Desktop.
Post to Slack webhook with curl & jq
#!/usr/bin/env bash
# CONFIG
URL="https://hooks.slack.com/services/..."
PAYLOAD='{
"channel": "#test",
"username": "Ghost",
"text": "no-message",
"icon_emoji": ":ghost:"
}'
# PARAMS
if [[ $# -eq 0 ]] ; then
echo 'Please give a message as first arg'
exit 0
fi
# CHECK jq installed
exit_with_err() {
echo >&2 "I require jq but it's not installed. Aborting."; exit 1;
}
command -v jq >/dev/null 2>&1 || { exit_with_err; }
type jq >/dev/null 2>&1 || { exit_with_err; }
hash jq 2>/dev/null || { exit_with_err; }
#SLACK
body=$(echo $PAYLOAD | jq -c --arg msg "$1" '.text = $msg')
curl -X POST -H "Content-Type: application/json" -d "$body" $URL
@elnygren
Copy link
Author

elnygren commented May 3, 2018

Example usage:

./slackpost.sh foo

# with whitespace
./slackpost.sh "foo bar"

@elnygren
Copy link
Author

elnygren commented May 3, 2019

You can also use the following payload, if you setup channel, username, icon in Slack for the hook:

PAYLOAD='{"text": "no-message"}'

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