Skip to content

Instantly share code, notes, and snippets.

@Mearman
Created April 18, 2018 09:15
Show Gist options
  • Select an option

  • Save Mearman/7015020d44a99d458b64ebc419781b8c to your computer and use it in GitHub Desktop.

Select an option

Save Mearman/7015020d44a99d458b64ebc419781b8c to your computer and use it in GitHub Desktop.
#!/bin/bash
#source: https://gist.github.com/dopiaza/6449505#gistcomment-1627214
# Usage: slackpost "<webhook_url>" "<channel>" "<username>" "<message>"
# ------------
webhook_url=$1
if [[ $webhook_url == "" ]]
then
echo "No webhook_url specified"
exit 1
fi
# ------------
shift
channel=$1
if [[ $channel == "" ]]
then
echo "No channel specified"
exit 1
fi
# ------------
shift
username=$1
if [[ $username == "" ]]
then
echo "No username specified"
exit 1
fi
# ------------
shift
text=$*
if [[ $text == "" ]]
then
echo "No text specified"
exit 1
fi
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"danger\" , \"text\": \"$escapedText\"}]}"
curl -s -d "payload=$json" "$webhook_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment