mirror of
https://github.com/catspace-dev/unicheckbot.git
synced 2024-11-14 18:26:34 +03:00
writing /web checking, added nodes file
This commit is contained in:
parent
3f6c966b5f
commit
94487be5e4
|
@ -8,6 +8,7 @@ authors = ["kiriharu <kiriharu@yandex.ru>"]
|
|||
python = "^3.8"
|
||||
aiogram = "^2.11.2"
|
||||
core = {path = "../core"}
|
||||
httpx = "^0.16.1"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
from aiogram import Dispatcher
|
||||
|
||||
from .start import start_cmd
|
||||
from .web import web_cmd
|
||||
|
||||
|
||||
def setup(dp: Dispatcher):
|
||||
dp.register_message_handler(start_cmd, commands=['start'])
|
||||
dp.register_message_handler(web_cmd, commands=['web'])
|
||||
|
|
|
@ -2,4 +2,4 @@ from aiogram.types import Message
|
|||
|
||||
|
||||
async def start_cmd(msg: Message):
|
||||
await msg.answer("Basic reply")
|
||||
await msg.answer("Тут нужно будет хелп мессадж написать")
|
51
apps/tgbot/tgbot/handlers/default/web.py
Normal file
51
apps/tgbot/tgbot/handlers/default/web.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from aiogram.types import Message
|
||||
from typing import Optional
|
||||
from tgbot.handlers.helpers import check_int
|
||||
from tgbot.nodes import nodes
|
||||
import httpx
|
||||
|
||||
help_message = """
|
||||
Использование:
|
||||
/web <hostname> <port>
|
||||
/web <hostname>
|
||||
|
||||
Производит проверку хоста по протоколу HTTP.
|
||||
"""
|
||||
|
||||
invalid_port = """Неправильный порт!"""
|
||||
|
||||
|
||||
async def check_web(message: Message, host: str, port: Optional[int]):
|
||||
if port is None:
|
||||
port = 80
|
||||
responses = []
|
||||
for node in nodes:
|
||||
async with httpx.AsyncClient() as client:
|
||||
result = await client.get(
|
||||
f"{node.address}/http", params=dict(
|
||||
target=host,
|
||||
port=port,
|
||||
token=node.token
|
||||
)
|
||||
)
|
||||
await message.answer(result.json())
|
||||
responses.append(result)
|
||||
|
||||
await message.answer(f"{host}:{port}")
|
||||
|
||||
|
||||
async def web_cmd(msg: Message):
|
||||
|
||||
port = None
|
||||
# TODO: Maybe check it in separated function?
|
||||
args = msg.text.split(" ")
|
||||
if len(args) < 2:
|
||||
return await msg.answer(help_message)
|
||||
if len(args) == 3:
|
||||
port = args[2]
|
||||
if not check_int(port):
|
||||
return await msg.answer(invalid_port)
|
||||
host = args[1]
|
||||
|
||||
await check_web(msg, host, port)
|
||||
|
7
apps/tgbot/tgbot/handlers/helpers.py
Normal file
7
apps/tgbot/tgbot/handlers/helpers.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
def check_int(value) -> bool:
|
||||
try:
|
||||
int(value)
|
||||
except ValueError:
|
||||
return False
|
||||
else:
|
||||
return True
|
6
apps/tgbot/tgbot/nodes.py
Normal file
6
apps/tgbot/tgbot/nodes.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from core.coretypes import APINode
|
||||
from typing import List
|
||||
|
||||
nodes: List[APINode] = [
|
||||
APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),
|
||||
]
|
Loading…
Reference in New Issue
Block a user