Skip to content

Instantly share code, notes, and snippets.

@dopiaza
Created September 5, 2013 12:33
Show Gist options
  • Save dopiaza/6449505 to your computer and use it in GitHub Desktop.
Save dopiaza/6449505 to your computer and use it in GitHub Desktop.
Post a message to a Slack channel
#!/bin/bash
# Usage: slackpost <token> <channel> <message>
# Enter the name of your slack host here - the thing that appears in your URL:
# https://slackhost.slack.com/
slackhost=PUT_YOUR_HOST_HERE
token=$1
if [[ $token == "" ]]
then
echo "No token specified"
exit 1
fi
shift
channel=$1
if [[ $channel == "" ]]
then
echo "No channel 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\", \"text\": \"$escapedText\"}"
curl -s -d "payload=$json" "https://$slackhost.slack.com/services/hooks/incoming-webhook?token=$token"
@romaopedro
Copy link

romaopedro commented Sep 25, 2019

Don't add the payload key before json variable.
And add the token in the headers.

curl -v \
	-H 'Content-Type:application/json; charset=utf-8' \
	-H 'Authorization: Bearer xoxp-XXXXXX-XXXX-XXXXXXXX \
	--data "$json" \
	"$slackwebhook"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment