-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerStatusAlert.py
More file actions
55 lines (48 loc) · 2.02 KB
/
ServerStatusAlert.py
File metadata and controls
55 lines (48 loc) · 2.02 KB
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
from mcstatus import JavaServer
from datetime import datetime
import requests
import time
server_address = 'YOUR_SERVER_ADDRESS'
online_check_interval = 60 * 5
offline_check_interval = 60 * 1
bot_token = 'YOUR_BOT_TOKEN'
chat_id = 'YOUR_CHAT_ID'
message_offline = 'я упал'
message_online = 'я встал'
def get_server_status(ip):
try:
server = JavaServer.lookup(ip)
status = server.status()
return {
'online': True,
'players': status.players.online,
'max_players': status.players.max
}
except:
return {
'online': False
}
def send_telegram_message(token, chat, message):
url = f'https://api.telegram.org/bot{token}/sendMessage'
params = {'chat_id': chat, 'text': message}
response = requests.get(url, params=params)
if response.status_code == 200:
print(f'[{datetime.today().strftime("%d.%m.%Y %H:%M:%S")}] Сообщение "{message}" отправлено в Telegram')
else:
print(f'[{datetime.today().strftime("%d.%m.%Y %H:%M:%S")}] Cообщение "{message}" не удалось отправить '
f'в Telegram ({response.status_code} {response.json()["description"]})')
was_online = True
while True:
result = get_server_status(server_address)
if result['online']:
print(f'[{datetime.today().strftime("%d.%m.%Y %H:%M:%S")}] Сервер {server_address} работает: '
f'{result["players"]}/{result["max_players"]} игроков')
if not was_online:
send_telegram_message(bot_token, chat_id, message_online)
was_online = True
else:
print(f'[{datetime.today().strftime("%d.%m.%Y %H:%M:%S")}] Сервер {server_address} не работает')
if was_online:
send_telegram_message(bot_token, chat_id, message_offline)
was_online = False
time.sleep(online_check_interval if result['online'] else offline_check_interval)