Last active
June 8, 2021 02:28
-
-
Save iganari/8a636b25055dd088111ef461688beb07 to your computer and use it in GitHub Desktop.
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/AAA/BBB" | |
### slack 送信チャンネル | |
CHANNEL=${CHANNEL:-"#notify-me"} | |
### slack 送信名 | |
BOTNAME=${BOTNAME:-"Claudia M"} | |
# BOTNAME=${BOTNAME:-"${HOSTNAME}"} | |
### slack アイコン | |
ICON='icon_url' # 'icon_url'か'icon_emoji'を選択可能 | |
# FACEICON=${FACEICON:-":ghost:"} # slack上のアイコンから選択可能 | |
FACEICON="https://pbs.twimg.com/profile_images/474832226773770240/MMctMu9Y.png" #urlで設置可能 | |
### 見出しとなるようなメッセージ | |
MESSAGE=${MESSAGE:-""} | |
#################### 設定 #################### | |
#メッセージを保存する一時ファイル | |
MESSAGEFILE=$(mktemp -t webhooks.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 | |
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={\"channel\": \"${CHANNEL}\", \"username\": \"${BOTNAME}\", \"${ICON}\": \"${FACEICON}\", \"text\": \"${MESSAGE}${WEBMESSAGE}\" }" ${WEBHOOKURL} >/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~/.bashrc
の配置例