Last active
February 13, 2017 16:12
-
-
Save calind/554219f46b9dd62c2821bd7c5b90602f to your computer and use it in GitHub Desktop.
Send slack messages
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
#!/bin/bash | |
if [ -z "$SLACK_WEBHOOK" ] ; then | |
echo "No Slack webhook specified. export SLACK_WEBHOOK=..." | |
exit 1 | |
fi | |
username="" | |
while getopts ":u:" opt; do | |
case $opt in | |
u) | |
username="$OPTARG" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
channel="$1" | |
body="$(cat)" | |
jq -n --arg username "$username" \ | |
--arg body "$body" \ | |
--arg channel "$channel" \ | |
'{ | |
"username": $username, | |
"channel": $channel, | |
"text": $body | |
}' | jq 'map_values(if . == "" then null else . end)' | \ | |
curl -X POST "$SLACK_WEBHOOK" \ | |
-H 'Content-type: application/json' \ | |
-d @- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment