Last active
March 28, 2022 00:25
-
-
Save elnygren/eb19e50cc067174c8689d2cd0f45d058 to your computer and use it in GitHub Desktop.
Post to Slack webhook with curl & jq
This file contains 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 | |
# 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
You can also use the following payload, if you setup channel, username, icon in Slack for the hook:
PAYLOAD='{"text": "no-message"}'