Skip to content

Instantly share code, notes, and snippets.

@serverwentdown
Last active August 29, 2015 14:27
Show Gist options
  • Save serverwentdown/6c27b68580a428466dad to your computer and use it in GitHub Desktop.
Save serverwentdown/6c27b68580a428466dad to your computer and use it in GitHub Desktop.
Sends messages as a slackbot
#!/bin/zsh
function json_escape(){
echo -n "$1" | python -c 'import json,sys; print json.dumps(sys.stdin.read())'
}
function json_escape_file() {
cat $1 | python -c 'import json,sys; print json.dumps(sys.stdin.read())'
}
SLACK_URL="https://hooks.slack.com/services/bla/bla/bla"
if [[ -t 0 ]]; then
while True; do
read -r MESSAGE &&
echo -n "sending..." &&
PAYLOAD="payload={\"text\": $(json_escape $MESSAGE), \"channel\": \"#$2\", \"username\": \"$1\", \"icon_emoji\": \"$3\"}" &&
curl -X POST --data-urlencode "$PAYLOAD" $SLACK_URL &&
echo
done
else
echo -n "sending..." &&
PAYLOAD="payload={\"text\": $(json_escape_file /dev/stdin), \"channel\": \"#$2\", \"username\": \"$1\", \"icon_emoji\": \"$3\"}" &&
curl -X POST --data-urlencode "$PAYLOAD" $SLACK_URL &&
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment