diff --git a/docker-compose-telegram.yml b/docker-compose-telegram.yml new file mode 100644 index 0000000..8d22cef --- /dev/null +++ b/docker-compose-telegram.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..39443c5 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/plugin/Dockerfile-telegram b/plugin/Dockerfile-telegram new file mode 100644 index 0000000..e42e0ee --- /dev/null +++ b/plugin/Dockerfile-telegram @@ -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" ] diff --git a/plugin/bot-telegram.py b/plugin/bot-telegram.py new file mode 100644 index 0000000..bb76efb --- /dev/null +++ b/plugin/bot-telegram.py @@ -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 = 'Server Status' + '\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 = 'Server Status' + '\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)