Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
slack.sh
#!/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
You can’t perform that action at this time.