0 Star 3 Fork 0

evine / myscripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
notify.sh 3.50 KB
一键复制 编辑 原始数据 按行查看 历史
anonymous 提交于 2021-07-14 17:41 . iyuu是http
## 钉钉通知,参考https://github.com/hzgjq/DingTalkRobot,$1:消息内容
notify_dingding_bot () {
local message="{\"msgtype\": \"text\",\"text\": {\"content\": \"$1\"}}"
local dd_api_url_base=https://oapi.dingtalk.com/robot/send
local timestamp sign dd_api_url send_result err_code err_message
timestamp=$(date -u "+%s%3N")
sign=$(echo -ne "$timestamp\n$DD_BOT_SECRET" | openssl dgst -sha256 -hmac "$DD_BOT_SECRET" -binary | openssl base64)
dd_api_url="$dd_api_url_base?access_token=${DD_BOT_TOKEN}&timestamp=${timestamp}&sign=${sign}"
send_result=$(curl -Ssk -H "Content-Type:application/json" -X POST -d "$message" "$dd_api_url")
err_code=$(echo "$send_result" | jq .errcode)
err_message=$(echo "$send_result" | jq -r .errmsg)
if [[ $err_code -eq 0 ]]; then
echo -e "发送钉钉BOT通知成功\n"
else
echo -e "发送钉钉BOT通知失败,错误代码:$err_code,错误消息:$err_message\n"
fi
}
## 发送Telegram通知,$1:消息内容
notify_telegram () {
local message="$(echo -e $1)"
local tg_api_url="https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage"
local send_result err_code err_message
send_result=$(curl -Ssk -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "chat_id=${TG_USER_ID}&text=${message}&disable_web_page_preview=true" "$tg_api_url")
send_result=$(echo "$send_result" | perl -pe "{s|\n|\\\n|g; s|\\\n$||}")
err_code=$(echo "$send_result" | jq .ok)
if [[ $err_code == true ]]; then
echo -e "发送Telegram通知成功\n"
else
err_code=$(echo "$send_result" | jq .err_code)
[[ err_code == 400 ]] && err_message="请主动给bot发送一条消息并检查接收用户ID是否正确。"
[[ err_code == 401 ]] && err_message="Telegram Bot Token 填写错误。"
echo -e "发送Telegram通知失败,错误代码:$err_code,错误消息:$err_message\n"
fi
}
## 爱语飞飞通知,$1:标题;$2:内容
notify_iyuu () {
local title="$1"
local desp="$(echo -e $2)"
local iyuu_url=http://iyuu.cn/${IYUU_TOKEN}.send
local send_result err_code err_message
send_result=$(curl -Ss -X POST -d "text=$title&desp=$desp" $iyuu_url)
err_code=$(echo "$send_result" | jq .errcode)
err_message=$(echo "$send_result" | jq -r .errmsg)
if [[ $err_code -eq 0 ]]; then
echo -e "发送爱语飞飞通知成功\n"
else
echo -e "发送爱语飞飞通知失败,错误代码:$err_code,错误消息:$err_message\n"
fi
}
## Server酱通知,$1:标题;$2:内容
notify_serverchan () {
local title="$1"
local desp="$(echo -e $2)"
local serverchan_url end_result
[[ $SCKEY == SCT* ]] && serverchan_url=https://sctapi.ftqq.com/${SCKEY}.send || serverchan_url=https://fcqq.com/${SCKEY}.send
send_result=$(curl -Ssk -X POST -d "text=$title&desp=$desp" $serverchan_url)
if [[ $(echo "$send_result" | jq .data.errno) -eq 0 || $(echo "$send_result" | jq .errno) -eq 0 ]]; then
echo -e "发送Server酱通知成功...\n"
else
echo -e "发送Server酱通知失败,错误消息:$send_result\n"
fi
}
## 发送通知,$1:标题;$2:内容
notify () {
local title="$1"
local desp="$2"
[[ $DD_BOT_TOKEN ]] && [[ $DD_BOT_SECRET ]] && notify_dingding_bot "$title\n\n$desp"
[[ $TG_BOT_TOKEN ]] && [[ $TG_USER_ID ]] && notify_telegram "$title\n\n$desp"
[[ $IYUU_TOKEN ]] && notify_iyuu "$title" "$desp"
[[ $SCKEY ]] && notify_serverchan "$title" "$desp"
}
1
https://gitee.com/evine/myscripts.git
git@gitee.com:evine/myscripts.git
evine
myscripts
myscripts
master

搜索帮助