Last active
August 29, 2015 14:27
-
-
Save serverwentdown/6c27b68580a428466dad to your computer and use it in GitHub Desktop.
Sends messages as a slackbot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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