Skip to content

Instantly share code, notes, and snippets.

@itsmenaga
Forked from elnygren/slackpost.sh
Created May 29, 2021 15:39
Show Gist options
  • Save itsmenaga/06869210a8fafea49a9e4222904ca64c to your computer and use it in GitHub Desktop.
Save itsmenaga/06869210a8fafea49a9e4222904ca64c 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment