From 2ebbde9f330759be7e77ec9485fe8c938c8ee108 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 26 Aug 2024 14:28:46 +0300 Subject: [PATCH] feat: started proxy alerts --- handlers/filters/proxy_status.py | 8 ++++ handlers/filters/refresh_proxy.py | 7 ++++ handlers/handler.py | 15 +++++++- .../proxy/proxy_status_command.py | 17 +++++++++ .../proxy/refresh_proxy_status_command.py | 18 +++++++++ .../message_handlers/proxy/start_command.py | 38 +++++++++++++++++++ handlers/proxy_commands.py | 17 +++++++++ main.py | 3 +- 8 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 handlers/filters/proxy_status.py create mode 100644 handlers/filters/refresh_proxy.py create mode 100644 handlers/message_handlers/proxy/proxy_status_command.py create mode 100644 handlers/message_handlers/proxy/refresh_proxy_status_command.py create mode 100644 handlers/message_handlers/proxy/start_command.py create mode 100644 handlers/proxy_commands.py diff --git a/handlers/filters/proxy_status.py b/handlers/filters/proxy_status.py new file mode 100644 index 0000000..9096f9e --- /dev/null +++ b/handlers/filters/proxy_status.py @@ -0,0 +1,8 @@ +from aiogram import types +from aiogram.filters import Filter + + +class ProxyStatusFilter(Filter): + async def __call__(self, data: types.CallbackQuery) -> bool: + return data.data.startswith('proxy_status') + \ No newline at end of file diff --git a/handlers/filters/refresh_proxy.py b/handlers/filters/refresh_proxy.py new file mode 100644 index 0000000..6819d2c --- /dev/null +++ b/handlers/filters/refresh_proxy.py @@ -0,0 +1,7 @@ +from aiogram import types +from aiogram.filters import Filter + + +class RefreshProxyFilter(Filter): + async def __call__(self, data: types.CallbackQuery) -> bool: + return data.data.startswith('refresh_proxy_status') \ No newline at end of file diff --git a/handlers/handler.py b/handlers/handler.py index 7e90d2b..567794d 100644 --- a/handlers/handler.py +++ b/handlers/handler.py @@ -34,6 +34,11 @@ class NeuroApiRouter(Router): self.message.register(handler.handler, handler.filter) + def add_callback_query_handler(self, callback: MessageHandlerABC, *args: Any): + handler = callback(self.bot, *args) + self.callback_query.register(handler.handler, handler.filter) + + class Handler: bot: Bot @@ -71,4 +76,12 @@ class Handler: args = handler[1:] if len(handler)>1 else [] self.router.add_message_handler(handler[0], *args) else: - self.router.add_message_handler(handler) \ No newline at end of file + self.router.add_message_handler(handler) + + def add_callback_handlers(self, handlers: List[MessageHandlerABC] | List[Tuple[MessageHandlerABC] | Optional[Tuple[Any, ...]]]): + for handler in handlers: + if isinstance(handler, tuple): + args = handler[1:] if len(handler)>1 else [] + self.router.add_callback_query_handler(handler[0], *args) + else: + self.router.add_callback_query_handler(handler) \ No newline at end of file diff --git a/handlers/message_handlers/proxy/proxy_status_command.py b/handlers/message_handlers/proxy/proxy_status_command.py new file mode 100644 index 0000000..d53cea3 --- /dev/null +++ b/handlers/message_handlers/proxy/proxy_status_command.py @@ -0,0 +1,17 @@ +from datetime import datetime + +from aiogram import types +from aiogram.utils.keyboard import InlineKeyboardBuilder + +from handlers.filters.proxy_status import ProxyStatusFilter + +from ..handler import MessageHandlerABC + + +class ProxyStatusCallback(MessageHandlerABC): + filter = ProxyStatusFilter() + async def _command(self, data: types.CallbackQuery): + builder = InlineKeyboardBuilder() + builder.button(text="Обновить статус🔄", callback_data="refresh_proxy_status") + date = datetime.now().strftime("%d.%m.%Y %H:%M:%S") + await self.bot.edit_message_text(f'{date} - Работаит', data.message.chat.id, data.message.message_id, reply_markup=builder.as_markup()) \ No newline at end of file diff --git a/handlers/message_handlers/proxy/refresh_proxy_status_command.py b/handlers/message_handlers/proxy/refresh_proxy_status_command.py new file mode 100644 index 0000000..76370fe --- /dev/null +++ b/handlers/message_handlers/proxy/refresh_proxy_status_command.py @@ -0,0 +1,18 @@ +from datetime import datetime + +from aiogram import types +from aiogram.utils.keyboard import InlineKeyboardBuilder + +from handlers.filters.refresh_proxy import RefreshProxyFilter + +from ..handler import MessageHandlerABC + + +class RefreshProxyStatusCallback(MessageHandlerABC): + filter = RefreshProxyFilter() + async def _command(self, data: types.CallbackQuery): + builder = InlineKeyboardBuilder() + builder.button(text="Обновить статус🔄", callback_data="refresh_proxy_status") + date = datetime.now().strftime("%d.%m.%Y %H:%M:%S") + await self.bot.edit_message_text(f'{date} - Работаит', data.message.chat.id, data.message.message_id, reply_markup=builder.as_markup()) + \ No newline at end of file diff --git a/handlers/message_handlers/proxy/start_command.py b/handlers/message_handlers/proxy/start_command.py new file mode 100644 index 0000000..45ec70f --- /dev/null +++ b/handlers/message_handlers/proxy/start_command.py @@ -0,0 +1,38 @@ +import json +import os + +from aiogram.filters import CommandStart +from aiogram.types import Message +from aiogram.utils.keyboard import InlineKeyboardBuilder + +from neuroapi import neuroapi + +from ..handler import MessageHandlerABC + + +class ProxyStartCommand(MessageHandlerABC): + filter = CommandStart() + async def _command(self, message: Message): + builder = InlineKeyboardBuilder() + builder.button(text="Статус прокси📝", callback_data="proxy_status") + is_admin = await neuroapi.admin.is_admin(message.from_user.id) + if is_admin: + builder.button(text="Всё плохо", callback_data="proxy_disable") + + markup = None # builder.as_markup() + + #TODO: Users to send list + users = dict() + + if os.path.exists('users.json'): + with open('users.json', 'r') as f: + users = json.load(f)['users'] + else: + with open('users.json', 'w+') as f: + json.dump({'users': [message.from_user.id]}, f) + + + + + + await message.answer('Приветствуем, теперь вы будете получать оповещения о статусе прокси.', reply_markup=markup) \ No newline at end of file diff --git a/handlers/proxy_commands.py b/handlers/proxy_commands.py new file mode 100644 index 0000000..7ff5c3e --- /dev/null +++ b/handlers/proxy_commands.py @@ -0,0 +1,17 @@ +from aiogram import Bot + +from handlers.handler import Handler +from handlers.message_handlers.proxy.proxy_status_command import \ + ProxyStatusCallback +from handlers.message_handlers.proxy.refresh_proxy_status_command import \ + RefreshProxyStatusCallback +from handlers.message_handlers.proxy.start_command import ProxyStartCommand +from neuroapi.types import BotSettings as BotSettingsType + + +class ProxyCommands(Handler): + settings: BotSettingsType + def __init__(self, bot: Bot) -> None: + super().__init__(bot) + self.add_handlers([ProxyStartCommand]) + self.add_callback_handlers([ProxyStatusCallback, RefreshProxyStatusCallback]) \ No newline at end of file diff --git a/main.py b/main.py index d57999a..0e5df2d 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from aiogram.fsm.storage.redis import RedisStorage from redis import asyncio as redis from handlers.admin_commands import AdminCommands +from handlers.proxy_commands import ProxyCommands from handlers.user_commands import UserCommands from neuroapi.config import GlobalConfig as Config from neuroapi.types import NeuroApiBot @@ -31,7 +32,7 @@ async def proxy_bot()->None: logging.warning('Proxy bot needs token in environment') return bot = NeuroApiBot(config.proxy_token) - bot.include_router() + bot.include_router(ProxyCommands) await bot.start() async def main() -> None: