-
Notifications
You must be signed in to change notification settings - Fork 153
/
is-alive-bot.sh
124 lines (104 loc) · 3.09 KB
/
is-alive-bot.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh
# Yona status telegram bot shell
#
# Check Yona server status and send me/group a message with telegram
#
# Configurations start..
# ~~~~~~~~~~~~~~~~~~~~~~
# API_TOKEN=123456789:AbCdEfgijk1LmPQRSTu234v5Wx-yZA67BCD
API_TOKEN=""
# USER_CHAT_ID=123456789
USER_CHAT_ID=""
# TARGET_SERVER="http://my-yona-server.com"
TARGET_SERVER="http://127.0.0.1:9000"
CHECK_URL="$TARGET_SERVER/-_-api/v1/hello"
EXPECTED_STATUS_CODE=200
# Polling Time (sec)
POLLING_TIME=60
# Messages
messageOnDown="Yona is unavaliable!"
messageOnRevive="Yona comeback!"
# Configurations end here...
#######################################
if [ $# -eq 0 ]
then
if [ -z $API_TOKEN ]
then
echo "Telegram Bot api token is reqruied!"
echo ""
echo "ex>"
echo "sh is-alive-bot.sh 328394984:AAFhL69afasfqjtUtIeRSzIagVYw7H3zF4"
echo ""
echo "See: https://core.telegram.org/bots#3-how-do-i-create-a-bot"
exit 1
fi
else
API_TOKEN=$1
fi
if [ -z $USER_CHAT_ID ]
then
if [ -z $2 ]
then
echo "---------------------------------------------------"
echo "Find chat id of user or group with your own eyes.. "
echo ""
echo "..chat\":{ \"id\": 621884 ... <= user or group id!"
echo "---------------------------------------------------"
echo ""
curl -sL https://api.telegram.org/bot$API_TOKEN/getUpdates
echo ""
echo "---------------------------------------------------"
echo ""
echo "and then retry..."
echo "sh is-alive-bot.sh API_TOKEN USER_CHAT_ID"
echo ""
echo "ex>"
echo "sh is-alive-bot.sh 328394984:AAFhL69afasfqjtUtIeRSzIagVYw7H3zF4 2156789"
exit 1;
else
USER_CHAT_ID=$2
fi
fi
### preparing for message and server status check
sendMessageToTelegram() {
curl -s \
-X POST \
https://api.telegram.org/bot$API_TOKEN/sendMessage \
-d text="$1" \
-d chat_id=$USER_CHAT_ID > /dev/null
}
# it is used for one time message sending
isDown=false
echo ""
echo "Monitoring started... $(date +%Y-%m-%d" "%H:%M:%S)"
echo ""
sendMessageToTelegram "Monitoring started... - $TARGET_SERVER"
while true
do
NOW=$(date +%Y-%m-%d" "%H:%M:%S)
response=$(curl -I $CHECK_URL 2> /dev/null | head -n 1 | cut -d$' ' -f2)
if [ -z $response ] || [ $response != $EXPECTED_STATUS_CODE ]
then
if [ $isDown == false ]
then
echo "Sending message - $messageOnDown - $NOW"
isDown=true
sendMessageToTelegram "$messageOnDown $response - $TARGET_SERVER"
fi
else
if [ $isDown == true ]
then
echo "Sending message - $messageOnRevive - $NOW"
isDown=false
sendMessageToTelegram "$messageOnRevive - $TARGET_SERVER"
fi
fi
sleep $POLLING_TIME
done;
#
# referred pages
#
# https://core.telegram.org/bots#3-how-do-i-create-a-bot
# https://core.telegram.org/bots/api
# https://community.onion.io/topic/499/sending-telegram-messages-via-bots
#