-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_notify.sh
104 lines (85 loc) · 2.38 KB
/
push_notify.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# send_key=${send_key:-'set your sendkey at conf'}
send_log=${send_log:-'send_hist.log'}
## --------- direct send
## msg is markdown, new-line need ' \n' OR '\n\n'
do_push_notify(){
local tit="$1"
local msg="$2"
local backend
for backend in $push_backend; do
case "$backend" in
telegram) do_push_$backend "$tit" "$msg";;
feishu) do_push_$backend "$tit" "$msg";;
wework) do_push_$backend "$tit" "$msg";;
dingtalk) do_push_$backend "$tit" "$msg";;
pushplus) do_push_$backend "$tit" "$msg";;
wxpusher) do_push_$backend "$tit" "$msg";;
serverchan) do_push_$backend "$tit" "$msg";;
*) echo 'choose a push backend by set push_backend in conf';;
esac
done
# echo "==`date -Is`: $tit: $msg" >&2
echo -e "\e[31m==`date -Is`: $tit: $msg\e[0m" | tee /dev/tty >> $send_log
}
load_push_impl(){
local backend
for backend in $push_backend; do
case "$backend" in
telegram) source $dir/push_impl.$backend.sh;;
feishu) source $dir/push_impl.$backend.sh;;
wework) source $dir/push_impl.$backend.sh;;
dingtalk) source $dir/push_impl.$backend.sh;;
pushplus) source $dir/push_impl.$backend.sh;;
wxpusher) source $dir/push_impl.$backend.sh;;
serverchan) source $dir/push_impl.$backend.sh;;
*) echo 'choose push backend by set push_backend in conf';;
esac
done
}
push_old=0 # last push time
push_cnt=0 # push count (last hour)
push_delay=30 # push interval (will adjusted dynamically)
push_notify(){
local tit="$1"
local msg="$2"
push_now=`date +%s`
if [ $((push_now-push_old)) -le $push_delay ]; then
echo -e "\e[31m push_msg too fast ($push_cnt recent), wait $push_delay sec!\e[0m"
return
fi
push_cnt=$((push_cnt+1)) #push_cnt
if [ $((push_now-push_old)) -ge 3600 ]; then
push_cnt=0 #1hour clear
fi
if [ $push_cnt -ge 2 ]; then #push_delay
push_delay=600 #10min
else
push_delay=30 #.5min
fi
push_old=$push_now
do_push_notify "$tit" "$msg"
echo
}
# push_notify hello "hello, world
# bbb"
## --------- append send
send_body=''
send_titl=''
push_notify_append(){
local tit="$1"
local msg="$2"
send_titl="$tit"
send_body="$send_body$msg
"
}
push_notify_final(){
[ -n "$send_body" ] && {
send_body="${send_body%%$'\n'}"
push_notify "$send_titl" "$send_body"
}
send_titl=''
send_body=''
}
# push_notify_append hello1 body1
# push_notify_append hello2 body2
# push_notify_final