From f81f1953d10a7cdb61b2b971b44c0161d2b281d3 Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sat, 20 Feb 2021 17:03:15 +0300 Subject: [PATCH 1/8] Remove redundant async/await when there is no IO --- apps/tgbot/tgbot/handlers/base.py | 8 ++++---- apps/tgbot/tgbot/handlers/default/icmp.py | 6 +++--- apps/tgbot/tgbot/handlers/default/ipcalc.py | 4 ++-- apps/tgbot/tgbot/handlers/default/minecraft.py | 2 +- apps/tgbot/tgbot/handlers/default/tcp.py | 2 +- apps/tgbot/tgbot/handlers/default/web.py | 2 +- apps/tgbot/tgbot/handlers/default/whois.py | 6 +++--- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index ee77eb8..63ee141 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -28,10 +28,10 @@ class SimpleCommandHandler: async def handler(self, message: Message): pass - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: raise NotImplemented - async def validate_target(self, target: str): + def validate_target(self, target: str): for validator in self.validators: validator.validate(target) @@ -81,7 +81,7 @@ class CheckerTargetPortHandler(CheckerBaseHandler): async def handler(self, message: Message): """This hanler can be used if you need target port args""" try: - args = await self.process_args(message.text) + args = self.process_args(message.text) except NotEnoughArgs: logger.info(f"User {message.from_user.id} got NotEnoughArgs error") return await message.answer(self.help_message, parse_mode="Markdown") @@ -89,7 +89,7 @@ class CheckerTargetPortHandler(CheckerBaseHandler): logger.info(f"User {message.from_user.id} got InvalidPort error") return await message.answer(self.invalid_port_message, parse_mode="Markdown") try: - await self.validate_target(args[0]) + self.validate_target(args[0]) except ValueError: # For ip check pass except LocalhostForbidden: diff --git a/apps/tgbot/tgbot/handlers/default/icmp.py b/apps/tgbot/tgbot/handlers/default/icmp.py index 4de783f..a79a3c4 100644 --- a/apps/tgbot/tgbot/handlers/default/icmp.py +++ b/apps/tgbot/tgbot/handlers/default/icmp.py @@ -23,7 +23,7 @@ class ICMPCheckerHandler(CheckerBaseHandler): @rate_limit async def handler(self, message: Message): try: - args = await self.process_args(message.text) + args = self.process_args(message.text) except NotEnoughArgs: return await message.answer(icmp_help_message, parse_mode="Markdown") except LocalhostForbidden: @@ -31,13 +31,13 @@ class ICMPCheckerHandler(CheckerBaseHandler): else: await self.check(message.chat.id, message.bot, dict(target=args[0], target_fq=args[0])) - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: args = text.split() if len(args) == 1: raise NotEnoughArgs() if len(args) >= 2: target = args[1] - await self.validate_target(target) + self.validate_target(target) return [target] async def prepare_message(self, res: Response): diff --git a/apps/tgbot/tgbot/handlers/default/ipcalc.py b/apps/tgbot/tgbot/handlers/default/ipcalc.py index eb15d69..83d18a4 100644 --- a/apps/tgbot/tgbot/handlers/default/ipcalc.py +++ b/apps/tgbot/tgbot/handlers/default/ipcalc.py @@ -24,7 +24,7 @@ class IPCalcCommandHandler(SimpleCommandHandler): @rate_limit async def handler(self, message: Message): try: - args = await self.process_args(message.text) + args = self.process_args(message.text) network = ipaddress.ip_network(args[1], False) except NotEnoughArgs: await message.answer(self.help_message, parse_mode='Markdown') @@ -34,7 +34,7 @@ class IPCalcCommandHandler(SimpleCommandHandler): msg = await self.prepare_message(network) await message.answer(msg) - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: args = text.split() if len(args) == 1: raise NotEnoughArgs diff --git a/apps/tgbot/tgbot/handlers/default/minecraft.py b/apps/tgbot/tgbot/handlers/default/minecraft.py index 4811c39..cdbf9c4 100644 --- a/apps/tgbot/tgbot/handlers/default/minecraft.py +++ b/apps/tgbot/tgbot/handlers/default/minecraft.py @@ -24,7 +24,7 @@ class MinecraftCheckerHandler(CheckerTargetPortHandler): def __init__(self): super().__init__() - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: return process_args_for_host_port(text, 25565) async def prepare_message(self, res: Response): diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index c3bf612..f0025a8 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -29,7 +29,7 @@ class TCPCheckerHandler(CheckerTargetPortHandler): async def handler(self, message: Message): await super(TCPCheckerHandler, self).handler(message) - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: args = text.split(' ', 1) if len(args) != 2: raise NotEnoughArgs() diff --git a/apps/tgbot/tgbot/handlers/default/web.py b/apps/tgbot/tgbot/handlers/default/web.py index b0ca9aa..7fce4a3 100644 --- a/apps/tgbot/tgbot/handlers/default/web.py +++ b/apps/tgbot/tgbot/handlers/default/web.py @@ -22,7 +22,7 @@ class WebCheckerHandler(CheckerTargetPortHandler): def __init__(self): super().__init__() - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: return process_args_for_host_port(text, 80) async def prepare_message(self, res: Response): diff --git a/apps/tgbot/tgbot/handlers/default/whois.py b/apps/tgbot/tgbot/handlers/default/whois.py index 4220cda..98188e8 100644 --- a/apps/tgbot/tgbot/handlers/default/whois.py +++ b/apps/tgbot/tgbot/handlers/default/whois.py @@ -116,7 +116,7 @@ class WhoisCommandHandler(SimpleCommandHandler): @rate_limit async def handler(self, message: Message): try: - args = await self.process_args(message.text) + args = self.process_args(message.text) except NotEnoughArgs: await message.answer(no_domain_text, parse_mode='Markdown') except LocalhostForbidden: @@ -124,13 +124,13 @@ class WhoisCommandHandler(SimpleCommandHandler): else: await message.answer(create_whois_message(args[0]), parse_mode='html') - async def process_args(self, text: str) -> list: + def process_args(self, text: str) -> list: args = text.split() if len(args) == 1: raise NotEnoughArgs if len(args) >= 2: host = args[1] - await self.validate_target(host) + self.validate_target(host) return [host] # only domain name async def prepare_message(self) -> str: From 82409c669bc45ce9d45ceb074c216ab6425b220d Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sat, 20 Feb 2021 18:24:39 +0300 Subject: [PATCH 2/8] nicer tests and host:port parsing --- apps/tgbot/pyproject.toml | 5 +- apps/tgbot/tgbot/handlers/base.py | 32 +++++++-- apps/tgbot/tgbot/handlers/default/tcp.py | 17 ++--- apps/tgbot/tgbot/test/__init__.py | 3 +- apps/tgbot/tgbot/test/port_parsers.py | 57 ---------------- apps/tgbot/tgbot/test/test_port_parsers.py | 75 ++++++++++++++++++++++ pyproject.toml | 2 + 7 files changed, 111 insertions(+), 80 deletions(-) delete mode 100644 apps/tgbot/tgbot/test/port_parsers.py create mode 100644 apps/tgbot/tgbot/test/test_port_parsers.py diff --git a/apps/tgbot/pyproject.toml b/apps/tgbot/pyproject.toml index e2893b7..fd54c57 100644 --- a/apps/tgbot/pyproject.toml +++ b/apps/tgbot/pyproject.toml @@ -17,10 +17,9 @@ tortoise-orm = "^0.16.20" aiomysql = "^0.0.21" [tool.poetry.dev-dependencies] +pytest = "^6.2.2" +flake8 = "^3.8.4" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" - -[tool.poetry.scripts] -test = 'tgbot.test:run_all' diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index 63ee141..c8b9538 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -102,14 +102,32 @@ class CheckerTargetPortHandler(CheckerBaseHandler): ) -def process_args_for_host_port(text: str, default_port: int) -> list: +def parse_host_port(text: str, default_port: int) -> Tuple[str, int]: + """Parse host:port + """ + text = text.strip() port = default_port + host = text + if ":" in text: + host, port = text.rsplit(":", 1) + elif " " in text: + host, port = text.rsplit(" ", 1) + + try: + port = int(port) + # !Important: Don't check range if port == default_port! + assert port == default_port or port in range(1, 65_536) + except (ValueError, AssertionError): + raise InvalidPort(port) + + return (host, port) + + +def process_args_for_host_port(text: str, default_port: int) -> Tuple[str, int]: + """Parse target from command + """ args = text.split(' ', 1) if len(args) != 2: raise NotEnoughArgs() - host = args[1] - if ":" in host: - host, port = host.rsplit(":", 1) - elif " " in host: - host, port = host.rsplit(" ", 1) - return [host, port] + target = args[1] + return parse_host_port(target, default_port) diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index f0025a8..29549dc 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -1,9 +1,9 @@ +from typing import Tuple from aiogram.types import Message from core.coretypes import ResponseStatus, ErrorPayload, PortResponse from httpx import Response -from tgbot.handlers.base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort -from tgbot.handlers.helpers import check_int +from tgbot.handlers.base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort, parse_host_port from tgbot.handlers.metrics import push_status_metric from tgbot.middlewares.throttling import rate_limit @@ -29,20 +29,15 @@ class TCPCheckerHandler(CheckerTargetPortHandler): async def handler(self, message: Message): await super(TCPCheckerHandler, self).handler(message) - def process_args(self, text: str) -> list: + def process_args(self, text: str) -> Tuple[str, int]: args = text.split(' ', 1) if len(args) != 2: raise NotEnoughArgs() host = args[1] - if ":" in host: - host, port = host.rsplit(":", 1) - elif " " in host: - host, port = host.split(maxsplit=1) - else: + host, port = parse_host_port(host, -1) + if port == -1: raise NotEnoughArgs() - if not check_int(port): - raise InvalidPort() - return [host, port] + return (host, port) async def prepare_message(self, res: Response): message, status = await self.message_std_vals(res) diff --git a/apps/tgbot/tgbot/test/__init__.py b/apps/tgbot/tgbot/test/__init__.py index 1ba86c5..aec7c3e 100644 --- a/apps/tgbot/tgbot/test/__init__.py +++ b/apps/tgbot/tgbot/test/__init__.py @@ -1,2 +1 @@ -def run_all(): - from . import port_parsers +from .test_port_parsers import * diff --git a/apps/tgbot/tgbot/test/port_parsers.py b/apps/tgbot/tgbot/test/port_parsers.py deleted file mode 100644 index 7f770f4..0000000 --- a/apps/tgbot/tgbot/test/port_parsers.py +++ /dev/null @@ -1,57 +0,0 @@ -import asyncio - -from ..handlers.default.tcp import TCPCheckerHandler -from ..handlers.base import process_args_for_host_port,\ - NotEnoughArgs, InvalidPort - - -try: - args = "/cmd" - process_args_for_host_port(args, 443) -except NotEnoughArgs: - pass -args = "/cmd example.com" -host, port = process_args_for_host_port(args, 443) -assert port == 443 - -args = "/cmd example.com 42" -host, port = process_args_for_host_port(args, 443) -assert port == "42" # TODO: FIX THIS SHIT - -args = "/cmd example.com:42" -host, port = process_args_for_host_port(args, 443) -assert port == "42" - -try: - args = "/cmd example.com fucktests" -except InvalidPort: - pass - -method = TCPCheckerHandler().process_args - - -async def test(): - try: - args = "/cmd" - await method(args) - args = "/cmd example.com" - await method(args) - except NotEnoughArgs: - pass - - args = "/cmd example.com 42" - host, port = await method(args) - assert port == "42" - - args = "/cmd example.com:42" - host, port = await method(args) - assert port == "42" - - try: - args = "/cmd example.com jdbnjsbndjsd" - await method(args) - except InvalidPort: - pass - - -asyncio.run(test()) diff --git a/apps/tgbot/tgbot/test/test_port_parsers.py b/apps/tgbot/tgbot/test/test_port_parsers.py new file mode 100644 index 0000000..5e3dd8c --- /dev/null +++ b/apps/tgbot/tgbot/test/test_port_parsers.py @@ -0,0 +1,75 @@ +from unittest import TestCase +import asyncio + +from ..handlers.default.tcp import TCPCheckerHandler +from ..handlers.base import process_args_for_host_port,\ + NotEnoughArgs, InvalidPort + + +class TestArgsProc(TestCase): + + def test_exceptions(self): + """Test exceptions being raised + on invalid commands + """ + cases = [ + ('/cmd', NotEnoughArgs), + ('/cmd example.com testsarenice', InvalidPort) + ] + for cmd, exc in cases: + with self.subTest(command=cmd): + self.assertRaises( + exc, + lambda: process_args_for_host_port(cmd, 443) + ) + + def test_host_port(self): + """Test that host and port are parsed correctly + """ + cases = [ + ('/cmd example.com', 'example.com', 443), + ('/cmd example.com 42', 'example.com', 42), + ('/cmd example.com:42', 'example.com', 42) + ] + + for cmd, host, port in cases: + with self.subTest(cmd=cmd, host=host, port=port): + test_host, test_port = process_args_for_host_port(cmd, 443) + self.assertEqual(test_host, host) + self.assertEqual(test_port, port) + + +class TestTCPCheckerHandler(TestCase): + def setUp(self) -> None: + self.method = TCPCheckerHandler().process_args + return super().setUp() + + def test_exceptions(self): + """Test all appropriate excpetions are raised. + """ + cases = [ + ('/cmd', NotEnoughArgs), + ('/cmd example.com', NotEnoughArgs), + ('/cmd example.com jdbnjsbndjsd', InvalidPort) + ] + + for cmd, exc in cases: + with self.subTest(cmd=cmd): + self.assertRaises( + exc, + lambda: self.method(cmd) + ) + + def test_host_port(self): + """Test that host and port are parsed correctly + """ + cases = [ + ('/cmd example.com 42', 'example.com', 42), + ('/cmd example.com:65', 'example.com', 65) + ] + + for cmd, host, port in cases: + with self.subTest(cmd=cmd, host=host, port=port): + test_host, test_port = self.method(cmd) + self.assertEqual(test_host, host) + self.assertEqual(test_port, port) diff --git a/pyproject.toml b/pyproject.toml index 2f65f47..bb7da24 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,6 +8,8 @@ authors = ["kiriharu "] python = "^3.8.2" [tool.poetry.dev-dependencies] +pytest = "^6.2.2" +flake8 = "^3.8.4" [build-system] requires = ["poetry-core>=1.0.0"] From 70eb2799827cc7f62fd3c1a066ecd6cea58a472f Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sat, 20 Feb 2021 18:26:51 +0300 Subject: [PATCH 3/8] remove not used asyncio import --- apps/tgbot/tgbot/test/test_port_parsers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/tgbot/tgbot/test/test_port_parsers.py b/apps/tgbot/tgbot/test/test_port_parsers.py index 5e3dd8c..66b63bc 100644 --- a/apps/tgbot/tgbot/test/test_port_parsers.py +++ b/apps/tgbot/tgbot/test/test_port_parsers.py @@ -1,5 +1,4 @@ from unittest import TestCase -import asyncio from ..handlers.default.tcp import TCPCheckerHandler from ..handlers.base import process_args_for_host_port,\ From 8a74756ab6275ad23c63c417e7bc2f0774820245 Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sat, 20 Feb 2021 18:42:47 +0300 Subject: [PATCH 4/8] remove check_int --- apps/tgbot/tgbot/handlers/helpers.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/apps/tgbot/tgbot/handlers/helpers.py b/apps/tgbot/tgbot/handlers/helpers.py index f6b8f68..f20db15 100644 --- a/apps/tgbot/tgbot/handlers/helpers.py +++ b/apps/tgbot/tgbot/handlers/helpers.py @@ -11,15 +11,6 @@ from traceback import format_exc import asyncio -def check_int(value) -> bool: - try: - int(value) - except ValueError: - return False - else: - return True - - async def send_api_request(client: AsyncClient, endpoint: str, data: dict, node: APINode): try: data['token'] = node.token From eda75f552cae05b5c0a0d3a94b429b5fd16c3255 Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sat, 20 Feb 2021 22:56:20 +0300 Subject: [PATCH 5/8] may as well not use lambdas here --- apps/tgbot/tgbot/test/test_port_parsers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/tgbot/tgbot/test/test_port_parsers.py b/apps/tgbot/tgbot/test/test_port_parsers.py index 66b63bc..926e674 100644 --- a/apps/tgbot/tgbot/test/test_port_parsers.py +++ b/apps/tgbot/tgbot/test/test_port_parsers.py @@ -19,7 +19,7 @@ class TestArgsProc(TestCase): with self.subTest(command=cmd): self.assertRaises( exc, - lambda: process_args_for_host_port(cmd, 443) + process_args_for_host_port, cmd, 443 ) def test_host_port(self): @@ -56,7 +56,7 @@ class TestTCPCheckerHandler(TestCase): with self.subTest(cmd=cmd): self.assertRaises( exc, - lambda: self.method(cmd) + self.method, cmd ) def test_host_port(self): From 723172387bbd42401015403a5eaa0080a712992b Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sun, 21 Feb 2021 00:43:41 +0300 Subject: [PATCH 6/8] this should probably make relative imports work --- apps/Dockerfile-tgbot | 2 +- apps/tgbot/{tgbot => }/bot.py | 4 ++-- apps/tgbot/test/__init__.py | 1 + apps/tgbot/{tgbot => }/test/test_port_parsers.py | 4 ++-- apps/tgbot/tgbot/handlers/base.py | 4 ++-- apps/tgbot/tgbot/handlers/default/icmp.py | 2 +- apps/tgbot/tgbot/handlers/default/ipcalc.py | 4 ++-- apps/tgbot/tgbot/handlers/default/minecraft.py | 4 ++-- apps/tgbot/tgbot/handlers/default/start.py | 6 +++--- apps/tgbot/tgbot/handlers/default/tcp.py | 6 +++--- apps/tgbot/tgbot/handlers/default/whois.py | 8 ++++---- apps/tgbot/tgbot/handlers/helpers.py | 4 ++-- apps/tgbot/tgbot/handlers/metrics.py | 2 +- apps/tgbot/tgbot/middlewares/__init__.py | 8 ++++---- apps/tgbot/tgbot/middlewares/userdata.py | 2 +- apps/tgbot/tgbot/middlewares/write_command_metric.py | 4 ++-- apps/tgbot/tgbot/test/__init__.py | 1 - 17 files changed, 33 insertions(+), 33 deletions(-) rename apps/tgbot/{tgbot => }/bot.py (95%) create mode 100644 apps/tgbot/test/__init__.py rename apps/tgbot/{tgbot => }/test/test_port_parsers.py (95%) delete mode 100644 apps/tgbot/tgbot/test/__init__.py diff --git a/apps/Dockerfile-tgbot b/apps/Dockerfile-tgbot index d6a583f..61dce7b 100644 --- a/apps/Dockerfile-tgbot +++ b/apps/Dockerfile-tgbot @@ -8,6 +8,6 @@ RUN apk update \ && apk add gcc python3-dev musl-dev libffi-dev openssl-dev make cargo COPY . . -WORKDIR tgbot/tgbot +WORKDIR tgbot RUN pip install --upgrade pip; pip install poetry; poetry config virtualenvs.create false; poetry install CMD poetry shell; python bot.py diff --git a/apps/tgbot/tgbot/bot.py b/apps/tgbot/bot.py similarity index 95% rename from apps/tgbot/tgbot/bot.py rename to apps/tgbot/bot.py index e1ac336..38d73c6 100644 --- a/apps/tgbot/tgbot/bot.py +++ b/apps/tgbot/bot.py @@ -5,8 +5,8 @@ from tortoise import Tortoise from tortoise.exceptions import DBConnectionError from loguru import logger from asyncio import sleep -import config -import handlers +import tgbot.config as config +import tgbot.handlers as handlers storage = MemoryStorage() telegram_bot = Bot(token=config.TELEGRAM_BOT_TOKEN) diff --git a/apps/tgbot/test/__init__.py b/apps/tgbot/test/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apps/tgbot/test/__init__.py @@ -0,0 +1 @@ + diff --git a/apps/tgbot/tgbot/test/test_port_parsers.py b/apps/tgbot/test/test_port_parsers.py similarity index 95% rename from apps/tgbot/tgbot/test/test_port_parsers.py rename to apps/tgbot/test/test_port_parsers.py index 926e674..c07bb2e 100644 --- a/apps/tgbot/tgbot/test/test_port_parsers.py +++ b/apps/tgbot/test/test_port_parsers.py @@ -1,7 +1,7 @@ from unittest import TestCase -from ..handlers.default.tcp import TCPCheckerHandler -from ..handlers.base import process_args_for_host_port,\ +from tgbot.handlers.default.tcp import TCPCheckerHandler +from tgbot.handlers.base import process_args_for_host_port,\ NotEnoughArgs, InvalidPort diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index c8b9538..141a7e1 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -1,7 +1,7 @@ from aiogram.types import Message from typing import Tuple, Any, List -from tgbot.nodes import nodes as all_nodes +from ..nodes import nodes as all_nodes from httpx import Response from aiogram.bot import Bot from datetime import datetime @@ -9,7 +9,7 @@ from core.coretypes import APINodeInfo from .helpers import send_api_requests from .errors import NotEnoughArgs, InvalidPort, LocalhostForbidden from .validators import BaseValidator, LocalhostValidator -from tgbot.middlewares.throttling import rate_limit +from ..middlewares.throttling import rate_limit from loguru import logger from uuid import uuid4 from time import time diff --git a/apps/tgbot/tgbot/handlers/default/icmp.py b/apps/tgbot/tgbot/handlers/default/icmp.py index a79a3c4..47a8e69 100644 --- a/apps/tgbot/tgbot/handlers/default/icmp.py +++ b/apps/tgbot/tgbot/handlers/default/icmp.py @@ -3,7 +3,7 @@ from httpx import Response from core.coretypes import ErrorPayload, ICMPCheckerResponse, ResponseStatus from ..base import CheckerBaseHandler, NotEnoughArgs, LocalhostForbidden from ..metrics import push_status_metric -from tgbot.middlewares.throttling import rate_limit +from ...middlewares.throttling import rate_limit icmp_help_message = """ ❓ Производит проверку хоста по протоколу ICMP. diff --git a/apps/tgbot/tgbot/handlers/default/ipcalc.py b/apps/tgbot/tgbot/handlers/default/ipcalc.py index 83d18a4..79a45fc 100644 --- a/apps/tgbot/tgbot/handlers/default/ipcalc.py +++ b/apps/tgbot/tgbot/handlers/default/ipcalc.py @@ -2,8 +2,8 @@ from aiogram.types import Message from typing import Union import ipaddress -from tgbot.handlers.base import SimpleCommandHandler, NotEnoughArgs -from tgbot.middlewares.throttling import rate_limit +from ..base import SimpleCommandHandler, NotEnoughArgs +from ...middlewares.throttling import rate_limit ipcalc_help_message = """ ❓ Калькулятор IP подсетей. diff --git a/apps/tgbot/tgbot/handlers/default/minecraft.py b/apps/tgbot/tgbot/handlers/default/minecraft.py index cdbf9c4..4e75cab 100644 --- a/apps/tgbot/tgbot/handlers/default/minecraft.py +++ b/apps/tgbot/tgbot/handlers/default/minecraft.py @@ -1,8 +1,8 @@ from core.coretypes import ResponseStatus, ErrorPayload, MinecraftResponse from httpx import Response -from tgbot.handlers.base import CheckerTargetPortHandler, process_args_for_host_port -from tgbot.handlers.metrics import push_status_metric +from ..base import CheckerTargetPortHandler, process_args_for_host_port +from ..metrics import push_status_metric minecraft_help_message = """ ❓ Получает статистику о Minecraft сервере diff --git a/apps/tgbot/tgbot/handlers/default/start.py b/apps/tgbot/tgbot/handlers/default/start.py index 3b1e54c..f6e8a1e 100644 --- a/apps/tgbot/tgbot/handlers/default/start.py +++ b/apps/tgbot/tgbot/handlers/default/start.py @@ -1,7 +1,7 @@ from aiogram.types import Message -from tgbot.models.user import User -from tgbot.middlewares.throttling import rate_limit -from tgbot.middlewares.userdata import userdata_required +from ...models.user import User +from ...middlewares.throttling import rate_limit +from ...middlewares.userdata import userdata_required start_message = """ diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index 29549dc..a71e35c 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -3,9 +3,9 @@ from aiogram.types import Message from core.coretypes import ResponseStatus, ErrorPayload, PortResponse from httpx import Response -from tgbot.handlers.base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort, parse_host_port -from tgbot.handlers.metrics import push_status_metric -from tgbot.middlewares.throttling import rate_limit +from ..base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort, parse_host_port +from ..metrics import push_status_metric +from ...middlewares.throttling import rate_limit tcp_help_message = """ ❓ Производит проверку TCP порта, открыт ли он или нет diff --git a/apps/tgbot/tgbot/handlers/default/whois.py b/apps/tgbot/tgbot/handlers/default/whois.py index 98188e8..687ac3c 100644 --- a/apps/tgbot/tgbot/handlers/default/whois.py +++ b/apps/tgbot/tgbot/handlers/default/whois.py @@ -6,10 +6,10 @@ from dataclasses import dataclass from whois_vu.api import WhoisSource from whois_vu.errors import IncorrectZone, QueryNotMatchRegexp -from tgbot.handlers.whois_zones import ZONES -from tgbot.handlers.base import SimpleCommandHandler -from tgbot.handlers.errors import NotEnoughArgs, LocalhostForbidden -from tgbot.middlewares.throttling import rate_limit +from ..whois_zones import ZONES +from ..base import SimpleCommandHandler +from ..errors import NotEnoughArgs, LocalhostForbidden +from ...middlewares.throttling import rate_limit whois_help_message = """ ❓ Вернёт информацию о домене. diff --git a/apps/tgbot/tgbot/handlers/helpers.py b/apps/tgbot/tgbot/handlers/helpers.py index f20db15..63b4d92 100644 --- a/apps/tgbot/tgbot/handlers/helpers.py +++ b/apps/tgbot/tgbot/handlers/helpers.py @@ -5,8 +5,8 @@ from ipaddress import ip_address from contextlib import suppress from loguru import logger from aiogram.bot import Bot -from tgbot.handlers.metrics import push_api_request_status -from tgbot.config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS +from .metrics import push_api_request_status +from ..config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS from traceback import format_exc import asyncio diff --git a/apps/tgbot/tgbot/handlers/metrics.py b/apps/tgbot/tgbot/handlers/metrics.py index 3f33bc5..3943be7 100644 --- a/apps/tgbot/tgbot/handlers/metrics.py +++ b/apps/tgbot/tgbot/handlers/metrics.py @@ -1,6 +1,6 @@ from aioinflux import InfluxDBClient from typing import Dict -from tgbot.config import INFLUX_DB, INFLUX_HOST, INFLUX_PORT, INFLUX_PASSWORD, INFLUX_USERNAME +from ..config import INFLUX_DB, INFLUX_HOST, INFLUX_PORT, INFLUX_PASSWORD, INFLUX_USERNAME async def push_metric(measurement, tags: Dict, fields: Dict): diff --git a/apps/tgbot/tgbot/middlewares/__init__.py b/apps/tgbot/tgbot/middlewares/__init__.py index e923a5f..498c6e6 100644 --- a/apps/tgbot/tgbot/middlewares/__init__.py +++ b/apps/tgbot/tgbot/middlewares/__init__.py @@ -1,4 +1,4 @@ -from tgbot.middlewares.write_command_metric import WriteCommandMetric -from tgbot.middlewares.logging import LoggingMiddleware -from tgbot.middlewares.throttling import ThrottlingMiddleware -from tgbot.middlewares.userdata import UserMiddleware +from .write_command_metric import WriteCommandMetric +from .logging import LoggingMiddleware +from .throttling import ThrottlingMiddleware +from .userdata import UserMiddleware diff --git a/apps/tgbot/tgbot/middlewares/userdata.py b/apps/tgbot/tgbot/middlewares/userdata.py index 99fb07e..2a6e559 100644 --- a/apps/tgbot/tgbot/middlewares/userdata.py +++ b/apps/tgbot/tgbot/middlewares/userdata.py @@ -2,7 +2,7 @@ from aiogram.dispatcher.handler import current_handler from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.types import Message, CallbackQuery -from tgbot.models import User +from ..models import User def userdata_required(func): diff --git a/apps/tgbot/tgbot/middlewares/write_command_metric.py b/apps/tgbot/tgbot/middlewares/write_command_metric.py index 67d1053..13b0ac5 100644 --- a/apps/tgbot/tgbot/middlewares/write_command_metric.py +++ b/apps/tgbot/tgbot/middlewares/write_command_metric.py @@ -1,7 +1,7 @@ from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.types import Message -from tgbot.handlers.metrics import push_metric -from tgbot.models import User, UserCheckRequests +from ..handlers.metrics import push_metric +from ..models import User, UserCheckRequests class WriteCommandMetric(BaseMiddleware): diff --git a/apps/tgbot/tgbot/test/__init__.py b/apps/tgbot/tgbot/test/__init__.py deleted file mode 100644 index aec7c3e..0000000 --- a/apps/tgbot/tgbot/test/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .test_port_parsers import * From 784cae145d1d82d890ef7791402811d88419d914 Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sun, 21 Feb 2021 13:14:55 +0300 Subject: [PATCH 7/8] better structure --- apps/Dockerfile-tgbot | 2 +- apps/tgbot/pyproject.toml | 3 +++ apps/tgbot/{ => tgbot}/bot.py | 13 +++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) rename apps/tgbot/{ => tgbot}/bot.py (89%) diff --git a/apps/Dockerfile-tgbot b/apps/Dockerfile-tgbot index 61dce7b..8bd8efc 100644 --- a/apps/Dockerfile-tgbot +++ b/apps/Dockerfile-tgbot @@ -10,4 +10,4 @@ RUN apk update \ COPY . . WORKDIR tgbot RUN pip install --upgrade pip; pip install poetry; poetry config virtualenvs.create false; poetry install -CMD poetry shell; python bot.py +CMD poetry run tgbot diff --git a/apps/tgbot/pyproject.toml b/apps/tgbot/pyproject.toml index fd54c57..97db224 100644 --- a/apps/tgbot/pyproject.toml +++ b/apps/tgbot/pyproject.toml @@ -23,3 +23,6 @@ flake8 = "^3.8.4" [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" + +[tool.poetry.scripts] +tgbot = "tgbot.bot:main" \ No newline at end of file diff --git a/apps/tgbot/bot.py b/apps/tgbot/tgbot/bot.py similarity index 89% rename from apps/tgbot/bot.py rename to apps/tgbot/tgbot/bot.py index 38d73c6..bf2a7fc 100644 --- a/apps/tgbot/bot.py +++ b/apps/tgbot/tgbot/bot.py @@ -1,12 +1,12 @@ from aiogram import Bot, Dispatcher, executor from aiogram.contrib.fsm_storage.memory import MemoryStorage -from tgbot.middlewares import UserMiddleware, WriteCommandMetric, LoggingMiddleware, ThrottlingMiddleware +from .middlewares import UserMiddleware, WriteCommandMetric, LoggingMiddleware, ThrottlingMiddleware from tortoise import Tortoise from tortoise.exceptions import DBConnectionError from loguru import logger from asyncio import sleep -import tgbot.config as config -import tgbot.handlers as handlers +from . import config +from . import handlers storage = MemoryStorage() telegram_bot = Bot(token=config.TELEGRAM_BOT_TOKEN) @@ -42,5 +42,10 @@ async def on_startup(disp: Dispatcher): disp.middleware.setup(LoggingMiddleware()) disp.middleware.setup(UserMiddleware()) -if __name__ == '__main__': + +def main(): executor.start_polling(dp, skip_updates=True, on_startup=on_startup) + + +if __name__ == '__main__': + main() From 7a8990bc74ebae0d43857da15ee0857497549701 Mon Sep 17 00:00:00 2001 From: Crystal Melting Dot Date: Sun, 21 Feb 2021 13:43:32 +0300 Subject: [PATCH 8/8] Sort imports --- apps/tgbot/tgbot/bot.py | 12 +++++---- apps/tgbot/tgbot/handlers/base.py | 27 ++++++++++--------- apps/tgbot/tgbot/handlers/default/__init__.py | 8 +++--- apps/tgbot/tgbot/handlers/default/icmp.py | 7 ++--- apps/tgbot/tgbot/handlers/default/ipcalc.py | 7 ++--- .../tgbot/tgbot/handlers/default/minecraft.py | 2 +- apps/tgbot/tgbot/handlers/default/start.py | 3 ++- apps/tgbot/tgbot/handlers/default/tcp.py | 8 +++--- apps/tgbot/tgbot/handlers/default/web.py | 4 ++- apps/tgbot/tgbot/handlers/default/whois.py | 11 ++++---- apps/tgbot/tgbot/handlers/helpers.py | 22 ++++++++------- apps/tgbot/tgbot/handlers/metrics.py | 7 +++-- apps/tgbot/tgbot/handlers/validators.py | 5 ++-- apps/tgbot/tgbot/middlewares/__init__.py | 2 +- apps/tgbot/tgbot/middlewares/throttling.py | 3 ++- apps/tgbot/tgbot/middlewares/userdata.py | 2 +- .../tgbot/middlewares/write_command_metric.py | 1 + apps/tgbot/tgbot/nodes.py | 3 ++- 18 files changed, 77 insertions(+), 57 deletions(-) diff --git a/apps/tgbot/tgbot/bot.py b/apps/tgbot/tgbot/bot.py index bf2a7fc..954a8ee 100644 --- a/apps/tgbot/tgbot/bot.py +++ b/apps/tgbot/tgbot/bot.py @@ -1,12 +1,14 @@ +from asyncio import sleep + from aiogram import Bot, Dispatcher, executor from aiogram.contrib.fsm_storage.memory import MemoryStorage -from .middlewares import UserMiddleware, WriteCommandMetric, LoggingMiddleware, ThrottlingMiddleware +from loguru import logger from tortoise import Tortoise from tortoise.exceptions import DBConnectionError -from loguru import logger -from asyncio import sleep -from . import config -from . import handlers + +from . import config, handlers +from .middlewares import (LoggingMiddleware, ThrottlingMiddleware, + UserMiddleware, WriteCommandMetric) storage = MemoryStorage() telegram_bot = Bot(token=config.TELEGRAM_BOT_TOKEN) diff --git a/apps/tgbot/tgbot/handlers/base.py b/apps/tgbot/tgbot/handlers/base.py index 141a7e1..27de800 100644 --- a/apps/tgbot/tgbot/handlers/base.py +++ b/apps/tgbot/tgbot/handlers/base.py @@ -1,18 +1,19 @@ -from aiogram.types import Message -from typing import Tuple, Any, List - -from ..nodes import nodes as all_nodes -from httpx import Response -from aiogram.bot import Bot from datetime import datetime -from core.coretypes import APINodeInfo -from .helpers import send_api_requests -from .errors import NotEnoughArgs, InvalidPort, LocalhostForbidden -from .validators import BaseValidator, LocalhostValidator -from ..middlewares.throttling import rate_limit -from loguru import logger -from uuid import uuid4 from time import time +from typing import Any, List, Tuple +from uuid import uuid4 + +from aiogram.bot import Bot +from aiogram.types import Message +from core.coretypes import APINodeInfo +from httpx import Response +from loguru import logger + +from ..middlewares.throttling import rate_limit +from ..nodes import nodes as all_nodes +from .errors import InvalidPort, LocalhostForbidden, NotEnoughArgs +from .helpers import send_api_requests +from .validators import BaseValidator, LocalhostValidator header = "Отчет о проверке хоста:" \ "\n\n— Хост: {target_fq}"\ diff --git a/apps/tgbot/tgbot/handlers/default/__init__.py b/apps/tgbot/tgbot/handlers/default/__init__.py index 149d615..dbdfc8d 100644 --- a/apps/tgbot/tgbot/handlers/default/__init__.py +++ b/apps/tgbot/tgbot/handlers/default/__init__.py @@ -1,12 +1,12 @@ from aiogram import Dispatcher +from .icmp import ICMPCheckerHandler +from .ipcalc import IPCalcCommandHandler +from .minecraft import MinecraftCheckerHandler from .start import start_cmd +from .tcp import TCPCheckerHandler from .web import WebCheckerHandler from .whois import WhoisCommandHandler -from .icmp import ICMPCheckerHandler -from .tcp import TCPCheckerHandler -from .minecraft import MinecraftCheckerHandler -from .ipcalc import IPCalcCommandHandler def setup(dp: Dispatcher): diff --git a/apps/tgbot/tgbot/handlers/default/icmp.py b/apps/tgbot/tgbot/handlers/default/icmp.py index 47a8e69..1ee495d 100644 --- a/apps/tgbot/tgbot/handlers/default/icmp.py +++ b/apps/tgbot/tgbot/handlers/default/icmp.py @@ -1,9 +1,10 @@ from aiogram.types import Message -from httpx import Response from core.coretypes import ErrorPayload, ICMPCheckerResponse, ResponseStatus -from ..base import CheckerBaseHandler, NotEnoughArgs, LocalhostForbidden -from ..metrics import push_status_metric +from httpx import Response + from ...middlewares.throttling import rate_limit +from ..base import CheckerBaseHandler, LocalhostForbidden, NotEnoughArgs +from ..metrics import push_status_metric icmp_help_message = """ ❓ Производит проверку хоста по протоколу ICMP. diff --git a/apps/tgbot/tgbot/handlers/default/ipcalc.py b/apps/tgbot/tgbot/handlers/default/ipcalc.py index 79a45fc..d40a7ef 100644 --- a/apps/tgbot/tgbot/handlers/default/ipcalc.py +++ b/apps/tgbot/tgbot/handlers/default/ipcalc.py @@ -1,9 +1,10 @@ -from aiogram.types import Message -from typing import Union import ipaddress +from typing import Union + +from aiogram.types import Message -from ..base import SimpleCommandHandler, NotEnoughArgs from ...middlewares.throttling import rate_limit +from ..base import NotEnoughArgs, SimpleCommandHandler ipcalc_help_message = """ ❓ Калькулятор IP подсетей. diff --git a/apps/tgbot/tgbot/handlers/default/minecraft.py b/apps/tgbot/tgbot/handlers/default/minecraft.py index 4e75cab..3e76070 100644 --- a/apps/tgbot/tgbot/handlers/default/minecraft.py +++ b/apps/tgbot/tgbot/handlers/default/minecraft.py @@ -1,4 +1,4 @@ -from core.coretypes import ResponseStatus, ErrorPayload, MinecraftResponse +from core.coretypes import ErrorPayload, MinecraftResponse, ResponseStatus from httpx import Response from ..base import CheckerTargetPortHandler, process_args_for_host_port diff --git a/apps/tgbot/tgbot/handlers/default/start.py b/apps/tgbot/tgbot/handlers/default/start.py index f6e8a1e..978636e 100644 --- a/apps/tgbot/tgbot/handlers/default/start.py +++ b/apps/tgbot/tgbot/handlers/default/start.py @@ -1,7 +1,8 @@ from aiogram.types import Message -from ...models.user import User + from ...middlewares.throttling import rate_limit from ...middlewares.userdata import userdata_required +from ...models.user import User start_message = """ diff --git a/apps/tgbot/tgbot/handlers/default/tcp.py b/apps/tgbot/tgbot/handlers/default/tcp.py index a71e35c..d3797ee 100644 --- a/apps/tgbot/tgbot/handlers/default/tcp.py +++ b/apps/tgbot/tgbot/handlers/default/tcp.py @@ -1,11 +1,13 @@ from typing import Tuple + from aiogram.types import Message -from core.coretypes import ResponseStatus, ErrorPayload, PortResponse +from core.coretypes import ErrorPayload, PortResponse, ResponseStatus from httpx import Response -from ..base import CheckerTargetPortHandler, NotEnoughArgs, InvalidPort, parse_host_port -from ..metrics import push_status_metric from ...middlewares.throttling import rate_limit +from ..base import (CheckerTargetPortHandler, InvalidPort, NotEnoughArgs, + parse_host_port) +from ..metrics import push_status_metric tcp_help_message = """ ❓ Производит проверку TCP порта, открыт ли он или нет diff --git a/apps/tgbot/tgbot/handlers/default/web.py b/apps/tgbot/tgbot/handlers/default/web.py index 7fce4a3..83b4ef4 100644 --- a/apps/tgbot/tgbot/handlers/default/web.py +++ b/apps/tgbot/tgbot/handlers/default/web.py @@ -1,5 +1,7 @@ +from core.coretypes import (HTTP_EMOJI, ErrorPayload, HttpCheckerResponse, + ResponseStatus) from httpx import Response -from core.coretypes import ResponseStatus, HTTP_EMOJI, HttpCheckerResponse, ErrorPayload + from ..base import CheckerTargetPortHandler, process_args_for_host_port from ..metrics import push_status_metric diff --git a/apps/tgbot/tgbot/handlers/default/whois.py b/apps/tgbot/tgbot/handlers/default/whois.py index 687ac3c..da3e298 100644 --- a/apps/tgbot/tgbot/handlers/default/whois.py +++ b/apps/tgbot/tgbot/handlers/default/whois.py @@ -1,15 +1,16 @@ +from dataclasses import dataclass from typing import Optional -from whois import whois, parser from aiogram.types import Message -from dataclasses import dataclass from whois_vu.api import WhoisSource from whois_vu.errors import IncorrectZone, QueryNotMatchRegexp -from ..whois_zones import ZONES -from ..base import SimpleCommandHandler -from ..errors import NotEnoughArgs, LocalhostForbidden +from whois import parser, whois + from ...middlewares.throttling import rate_limit +from ..base import SimpleCommandHandler +from ..errors import LocalhostForbidden, NotEnoughArgs +from ..whois_zones import ZONES whois_help_message = """ ❓ Вернёт информацию о домене. diff --git a/apps/tgbot/tgbot/handlers/helpers.py b/apps/tgbot/tgbot/handlers/helpers.py index 63b4d92..3d41ee7 100644 --- a/apps/tgbot/tgbot/handlers/helpers.py +++ b/apps/tgbot/tgbot/handlers/helpers.py @@ -1,14 +1,16 @@ -from httpx import AsyncClient, Timeout, Response -from typing import List, Callable -from core.coretypes import APINode -from ipaddress import ip_address -from contextlib import suppress -from loguru import logger -from aiogram.bot import Bot -from .metrics import push_api_request_status -from ..config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS -from traceback import format_exc import asyncio +from contextlib import suppress +from ipaddress import ip_address +from traceback import format_exc +from typing import Callable, List + +from aiogram.bot import Bot +from core.coretypes import APINode +from httpx import AsyncClient, Response, Timeout +from loguru import logger + +from ..config import NOTIFICATION_BOT_TOKEN, NOTIFICATION_USERS +from .metrics import push_api_request_status async def send_api_request(client: AsyncClient, endpoint: str, data: dict, node: APINode): diff --git a/apps/tgbot/tgbot/handlers/metrics.py b/apps/tgbot/tgbot/handlers/metrics.py index 3943be7..42559a1 100644 --- a/apps/tgbot/tgbot/handlers/metrics.py +++ b/apps/tgbot/tgbot/handlers/metrics.py @@ -1,6 +1,9 @@ -from aioinflux import InfluxDBClient from typing import Dict -from ..config import INFLUX_DB, INFLUX_HOST, INFLUX_PORT, INFLUX_PASSWORD, INFLUX_USERNAME + +from aioinflux import InfluxDBClient + +from ..config import (INFLUX_DB, INFLUX_HOST, INFLUX_PASSWORD, INFLUX_PORT, + INFLUX_USERNAME) async def push_metric(measurement, tags: Dict, fields: Dict): diff --git a/apps/tgbot/tgbot/handlers/validators.py b/apps/tgbot/tgbot/handlers/validators.py index 160317e..44d7f6c 100644 --- a/apps/tgbot/tgbot/handlers/validators.py +++ b/apps/tgbot/tgbot/handlers/validators.py @@ -1,6 +1,7 @@ -from .errors import LocalhostForbidden -from ipaddress import ip_address from contextlib import suppress +from ipaddress import ip_address + +from .errors import LocalhostForbidden class BaseValidator: diff --git a/apps/tgbot/tgbot/middlewares/__init__.py b/apps/tgbot/tgbot/middlewares/__init__.py index 498c6e6..641ad65 100644 --- a/apps/tgbot/tgbot/middlewares/__init__.py +++ b/apps/tgbot/tgbot/middlewares/__init__.py @@ -1,4 +1,4 @@ -from .write_command_metric import WriteCommandMetric from .logging import LoggingMiddleware from .throttling import ThrottlingMiddleware from .userdata import UserMiddleware +from .write_command_metric import WriteCommandMetric diff --git a/apps/tgbot/tgbot/middlewares/throttling.py b/apps/tgbot/tgbot/middlewares/throttling.py index 462cb90..0cfed7d 100644 --- a/apps/tgbot/tgbot/middlewares/throttling.py +++ b/apps/tgbot/tgbot/middlewares/throttling.py @@ -1,9 +1,10 @@ +import asyncio + from aiogram import Dispatcher, types from aiogram.dispatcher import DEFAULT_RATE_LIMIT from aiogram.dispatcher.handler import CancelHandler, current_handler from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.utils.exceptions import Throttled -import asyncio def rate_limit(func): diff --git a/apps/tgbot/tgbot/middlewares/userdata.py b/apps/tgbot/tgbot/middlewares/userdata.py index 2a6e559..72a1b99 100644 --- a/apps/tgbot/tgbot/middlewares/userdata.py +++ b/apps/tgbot/tgbot/middlewares/userdata.py @@ -1,6 +1,6 @@ from aiogram.dispatcher.handler import current_handler from aiogram.dispatcher.middlewares import BaseMiddleware -from aiogram.types import Message, CallbackQuery +from aiogram.types import CallbackQuery, Message from ..models import User diff --git a/apps/tgbot/tgbot/middlewares/write_command_metric.py b/apps/tgbot/tgbot/middlewares/write_command_metric.py index 13b0ac5..2729fa2 100644 --- a/apps/tgbot/tgbot/middlewares/write_command_metric.py +++ b/apps/tgbot/tgbot/middlewares/write_command_metric.py @@ -1,5 +1,6 @@ from aiogram.dispatcher.middlewares import BaseMiddleware from aiogram.types import Message + from ..handlers.metrics import push_metric from ..models import User, UserCheckRequests diff --git a/apps/tgbot/tgbot/nodes.py b/apps/tgbot/tgbot/nodes.py index ba5208d..1c02cab 100644 --- a/apps/tgbot/tgbot/nodes.py +++ b/apps/tgbot/tgbot/nodes.py @@ -1,6 +1,7 @@ -from core.coretypes import APINode from typing import List +from core.coretypes import APINode + nodes: List[APINode] = [ APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"), APINode("http://localhost:8080", "CHANGE_TOKEN_BY_ENV"),