Skip to content

Instantly share code, notes, and snippets.

@calind
Last active February 13, 2017 16:12
Show Gist options
  • Save calind/554219f46b9dd62c2821bd7c5b90602f to your computer and use it in GitHub Desktop.
Save calind/554219f46b9dd62c2821bd7c5b90602f to your computer and use it in GitHub Desktop.
Send slack messages
#!/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