[Up]常用資訊

[重點文章] 重點文章 [重點文章] 重點文章

2020年4月4日 星期六

[SHELL] 簡化 bash shell 發送訊息到 Slack

[SHELL] 簡化 bash shell 發送訊息到 Slack 簡化 bash shell 發送訊息到 Slack
#!/bin/bash
#
set -eu
WEBHOOKURL="Incoming Webhooks Integration" #你的 incoming webhook url
#
MESSAGEFILE=$(mktemp -t slackweb.XXXXXXX)
trap "
rm ${MESSAGEFILE}
" 0

usage_exit() {
    #echo "Usage: $0 [-m message] [-c channel] [-i icon] [-n botname]" 1>&2
    echo "Usage: $0 [-m message] [-c channel] [-i icon] [-n botname] [-T title]" 1>&2
    exit 0
}
#

CHANNEL=${CHANNEL:-"#lab-01"} # 
#
FACEICON=${FACEICON:-":ghost:"}
#
MESSAGE=${MESSAGE:-""}
#
character=$1
case "$character" in
  "t1") SITENAME=${SITENAME:-":whale:"} ;;
  "t2") SITENAME=${SITENAME:-":construction_worker:"} ;;
  "t3") SITENAME=${SITENAME:-":rocket:"} ;;
esac
#
character=$1
case "$character" in
  "t1") SITEBOT=${SITEBOT:-"report-bott1"} ;;
  "t2") SITEBOT=${SITEBOT:-"report-bott2"} ;;
  "t3") SITEBOT=${SITEBOT:-"report-bott3"} ;;
esac
#
#echo "var 'file' == $SITENAME"

if [ -p /dev/stdin ] ; then
    #
    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\": \"${SITEBOT}\", \"icon_emoji\": \"${SITENAME}\", \"text\": \"${MESSAGE}${WEBMESSAGE}\" }" ${WEBHOOKURL} >/dev/null


沒有留言:

張貼留言