Skip to content

Instantly share code, notes, and snippets.

@hmoenck
Last active January 8, 2019 21:28
Show Gist options
  • Save hmoenck/53e695d5dba085c83d556847605fab37 to your computer and use it in GitHub Desktop.
Save hmoenck/53e695d5dba085c83d556847605fab37 to your computer and use it in GitHub Desktop.
Slackpost
#!/bin/bash
# Usage: slackpost <channel> <message>
# Setup:
# 1. Create an incoming webhook. Link can be found in https://api.slack.com/custom-integrations
# 2. Retrieve link like https://hooks.slack.com/services/T0XXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX
# 3. Add link to this script (webhook)
# 4. Add your slackhost
# 5. Profit
#This is a slightly adjusted version from: https://gist.github.com/dopiaza/6449505
# Enter the name of your slack host here - the thing that appears in your URL:
# eg "fubioroboticslab"
slackhost="fubioroboticslab"
webhook="https://hooks.slack.com/services/T0XXXXXXX/BXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
echo $1
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 -X POST -H 'Content-type: application/json' --data "$json" $webhook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment