forked from MrCapsLock/watchtower
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (69 loc) · 2.24 KB
/
main.py
File metadata and controls
84 lines (69 loc) · 2.24 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
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
import os
import json
import time
import uvloop
import asyncio
import subprocess
from logger import logger
from tasks import ping, wget
from utiles import Validator
from pyrogram import Client, filters
from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()
app = Client(
"Watchtower",
bot_token=os.environ["BOT_TOKEN"],
api_id=int(os.environ["API_ID"]),
api_hash=os.environ["API_HASH"],
)
loop = asyncio.get_event_loop()
app.loop = loop
validator = Validator()
def jsonify(message):
return json.loads(str(message))
async def add_usage(message):
await message.reply_text(
text=f"Wrong command!\n\nUsage:\n/add `<name>` `<ip>` `<port:int>` `<ping=default/wget>` `<interval:int>`"
)
return
def init():
# TODO: add procedure to retrieve scheduled tasks from redis
pass
@app.on_message(filters.command("start") & filters.private)
async def start(client, message):
await client.send_message(
chat_id=message.chat.id,
text=f"Hello, Welcome to watchtower bot\n\n/add `<name>` `<ip>` `<port:int>` `<ping=default/wget>` `<interval:int>`\n/list - show servers list and state",
)
@app.on_message(filters.command("add") & filters.private)
async def add_server(client, message):
args = message.text.split()
if len(args) != 6 or not validator.validate_input(args):
await add_usage(message)
return
name, ip, port, method, interval = args[1:]
fn = wget if method == "wget" else ping
scheduler.add_job(
fn,
"interval",
seconds=int(interval),
kwargs={
"message": message,
"name": name,
"ip": ip,
"port": port,
},
)
# TODO: create function to save redis database on disk
# database.save()
logger.info(f"Scheduled for {name} with address: {ip}:{port} / method: {method}")
await message.reply_text(text=f"{name} scheduled successfully")
@app.on_message(filters.command("list") & filters.private)
async def server_list(client, message):
await message.reply(
text="You didn't add any server before.\nUse /add to add new server",
)
if __name__ == "__main__":
scheduler.start()
init()
app.run()