mirror of
https://github.com//cppla/ServerStatus
synced 2025-07-08 02:15:59 +08:00
telegram for serverstatus
This commit is contained in:
parent
0834ff138d
commit
f68d3a8963
34
docker-compose-telegram.yml
Normal file
34
docker-compose-telegram.yml
Normal file
@ -0,0 +1,34 @@
|
||||
version: "3"
|
||||
services:
|
||||
serverstatus:
|
||||
image: cppla/serverstatus:latest
|
||||
container_name: serverstatus
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
serverstatus-network:
|
||||
ipv4_address: 172.23.0.2
|
||||
volumes:
|
||||
- ./server/config.json:/ServerStatus/server/config.json
|
||||
- ./web/json:/usr/share/nginx/html/json
|
||||
ports:
|
||||
- 35601:35601
|
||||
- 8080:80
|
||||
bot:
|
||||
build:
|
||||
context: ./plugin
|
||||
dockerfile: Dockerfile-telegram
|
||||
container_name: bot4sss
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
serverstatus-network:
|
||||
ipv4_address: 172.23.0.3
|
||||
environment:
|
||||
- TG_CHAT_ID=tg_chat_id
|
||||
- TG_BOT_TOKEN=tg_bot_token
|
||||
|
||||
networks:
|
||||
serverstatus-network:
|
||||
name: serverstatus-network
|
||||
ipam:
|
||||
config:
|
||||
- subnet: 172.23.0.0/24
|
22
docker-compose.yml
Normal file
22
docker-compose.yml
Normal file
@ -0,0 +1,22 @@
|
||||
version: "3"
|
||||
services:
|
||||
serverstatus:
|
||||
image: cppla/serverstatus:latest
|
||||
container_name: serverstatus
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
serverstatus-network:
|
||||
ipv4_address: 172.23.0.2
|
||||
volumes:
|
||||
- ./server/config.json:/ServerStatus/server/config.json
|
||||
- ./web/json:/usr/share/nginx/html/json
|
||||
ports:
|
||||
- 35601:35601
|
||||
- 8080:80
|
||||
|
||||
networks:
|
||||
serverstatus-network:
|
||||
name: serverstatus-network
|
||||
ipam:
|
||||
config:
|
||||
- subnet: 172.23.0.0/24
|
9
plugin/Dockerfile-telegram
Normal file
9
plugin/Dockerfile-telegram
Normal file
@ -0,0 +1,9 @@
|
||||
FROM nyurik/alpine-python3-requests
|
||||
|
||||
LABEL maintainer="lidalao"
|
||||
LABEL version="0.0.1"
|
||||
LABEL description="Telegram Bot for ServerStatus"
|
||||
|
||||
WORKDIR /app
|
||||
COPY ./bot-telegram.py .
|
||||
CMD [ "python", "./bot-telegram.py" ]
|
73
plugin/bot-telegram.py
Normal file
73
plugin/bot-telegram.py
Normal file
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# coding: utf-8
|
||||
# Create by : https://github.com/lidalao/ServerStatus
|
||||
# 版本:0.0.1, 支持Python版本:2.7 to 3.9
|
||||
# 支持操作系统: Linux, OSX, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures
|
||||
|
||||
import os
|
||||
import sys
|
||||
import requests
|
||||
import time
|
||||
import traceback
|
||||
|
||||
NODE_STATUS_URL = 'http://sss/json/stats.json'
|
||||
|
||||
offs = []
|
||||
counterOff = {}
|
||||
counterOn = {}
|
||||
|
||||
def _send(text):
|
||||
chat_id = os.getenv('TG_CHAT_ID')
|
||||
bot_token = os.environ.get('TG_BOT_TOKEN')
|
||||
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?parse_mode=HTML&disable_web_page_preview=true&chat_id=" + chat_id + "&text=" + text
|
||||
try:
|
||||
requests.get(url)
|
||||
except Exception as e:
|
||||
print("catch exception: ", traceback.format_exc())
|
||||
|
||||
def send2tg(srv, flag):
|
||||
if srv not in counterOff:
|
||||
counterOff[srv] = 0
|
||||
if srv not in counterOn:
|
||||
counterOn[srv] = 0
|
||||
|
||||
if flag == 1 : # online
|
||||
if srv in offs:
|
||||
if counterOn[srv] < 10:
|
||||
counterOn[srv] += 1
|
||||
return
|
||||
#1. Remove srv from offs; 2. Send to tg: I am online
|
||||
offs.remove(srv)
|
||||
counterOn[srv] = 0
|
||||
text = '<b>Server Status</b>' + '\n主机上线: ' + srv
|
||||
_send(text)
|
||||
else: #offline
|
||||
if srv not in offs:
|
||||
if counterOff[srv] < 10:
|
||||
counterOff[srv] += 1
|
||||
return
|
||||
#1. Append srv to offs; 2. Send to tg: I am offline
|
||||
offs.append(srv)
|
||||
counterOff[srv] = 0
|
||||
text = '<b>Server Status</b>' + '\n主机下线: ' + srv
|
||||
_send(text)
|
||||
|
||||
def sscmd(address):
|
||||
while True:
|
||||
r = requests.get(url=address, headers={"User-Agent": "ServerStatus/20211116"})
|
||||
try:
|
||||
jsonR = r.json()
|
||||
except Exception as e:
|
||||
print('未发现任何节点')
|
||||
continue
|
||||
for i in jsonR["servers"]:
|
||||
if i["online4"] is False and i["online6"] is False:
|
||||
send2tg(i["name"], 0)
|
||||
else:
|
||||
send2tg(i["name"], 1)
|
||||
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sscmd(NODE_STATUS_URL)
|
Loading…
x
Reference in New Issue
Block a user