Created
February 27, 2017 01:18
-
-
Save IkezoeMakoto/4d4f4561b442a74a1a9ea67972225f97 to your computer and use it in GitHub Desktop.
slack.sh
This file contains 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/sh | |
set -eu | |
#Incoming WebHooksのURL(※よしなにかえてね) | |
WEBHOOKURL="https://hooks.slack.com/services/XXXXXx" | |
#メッセージを保存する一時ファイル | |
MESSAGEFILE=$(mktemp -t webhook.XXXXXX) | |
trap " | |
rm ${MESSAGEFILE} | |
" 0 | |
usage_exit() { | |
echo "Usage: $0 [-m message] [-c channel] [-i icon] [-n botname]" 1>&2 | |
exit 0 | |
} | |
while getopts c:i:n:m: opts | |
do | |
case $opts in | |
c) | |
CHANNEL=$OPTARG | |
;; | |
i) | |
FACEICON=$OPTARG | |
;; | |
n) | |
BOTNAME=$OPTARG | |
;; | |
m) | |
MESSAGE=$OPTARG"\n" | |
;; | |
\?) | |
usage_exit | |
;; | |
esac | |
done | |
#slack 送信チャンネル(※よしなにかえてね) | |
CHANNEL=${CHANNEL:-"#channel"} | |
#slack 送信名(※よしなにかえてね) | |
BOTNAME=${BOTNAME:-"deploy-bot"} | |
#slack アイコン(※よしなにかえてね) | |
FACEICON=${FACEICON:-":gear: "} | |
#見出しとなるようなメッセージ | |
MESSAGE=${MESSAGE:-""} | |
if [ -p /dev/stdin ] ; then | |
#改行コードをslack用に変換 | |
cat - | tr '\n' '\\' | sed 's/\\/\\n/g' > ${MESSAGEFILE} | |
else | |
echo "nothing stdin" | |
exit 1 | |
fi | |
WEBMESSAGE=`cat ${MESSAGEFILE}` | |
#Incoming WebHooks送信 | |
curl -s -S -X POST --data-urlencode "payload={\"link_names\": 1,\"channel\": \"${CHANNEL}\", \"username\": \"${BOTNAME}\", \"icon_emoji\": \"${FACEICON}\", \"text\": \"${MESSAGE}${WEBMESSAGE}\" }" ${WEBHOOKURL} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment