diff --git a/handlers/admin_commands.py b/handlers/admin_commands.py index 7ebbbcb..f5f0728 100644 --- a/handlers/admin_commands.py +++ b/handlers/admin_commands.py @@ -1,5 +1,7 @@ +import asyncio from typing import List +import aioschedule as schedule from aiogram import Bot, F, types from aiogram.filters import Command from aiogram.fsm.context import FSMContext @@ -13,6 +15,7 @@ from handlers.handler import Handler from handlers.middlewares.user import AdminMiddleware from handlers.states.change_post import ChangePost from neuroapi import neuroapi +from neuroapi.types import BotSettings as BotSettingsType def get_post_info(post: neuroTypes.Post, post_id: int) -> str: @@ -25,10 +28,11 @@ def get_post_info(post: neuroTypes.Post, post_id: int) -> str: class AdminCommands(Handler): + settings: BotSettingsType def __init__(self, bot: Bot) -> None: super().__init__(bot) self.router.message.middleware(AdminMiddleware()) - + @self.router.message(NewPostFilter()) async def new_post(message: types.Message): post: neuroTypes.Post = await neuroapi.post.get_by_media_group_id(message.media_group_id) @@ -208,5 +212,22 @@ class AdminCommands(Handler): await message.reply('Ваше сообщение было отправлено!') except Exception as e: print(e) + + @self.router.message(Command('update_settings')) + async def update_settings(mes: types.Message| None = None): + self.settings = await neuroapi.bot_settings.get() + if mes: await mes.answer('Настройки обновлены!') + + + async def schedule_checker(): + await update_settings() + schedule.every().minute.do(update_settings, None) + while 1: + await schedule.run_pending() + await asyncio.sleep(1) + + asyncio.create_task(schedule_checker()) + + diff --git a/neuroapi/_methods/bot_settings.py b/neuroapi/_methods/bot_settings.py new file mode 100644 index 0000000..12cc228 --- /dev/null +++ b/neuroapi/_methods/bot_settings.py @@ -0,0 +1,13 @@ +from aiohttp import ClientSession + +from neuroapi.types import BotSettings as BotSettingsType + +from .api_method import ApiMethod + + +class BotSettings(ApiMethod): + async def get(self)-> BotSettingsType: + async with ClientSession() as session: + response = await session.get(self.api_url+'/settings') + settings = BotSettingsType.from_dict(await response.json()) + return settings \ No newline at end of file diff --git a/neuroapi/_neuroapi.py b/neuroapi/_neuroapi.py index 6949754..f22f373 100644 --- a/neuroapi/_neuroapi.py +++ b/neuroapi/_neuroapi.py @@ -1,4 +1,5 @@ from ._methods.admin import Admin +from ._methods.bot_settings import BotSettings from ._methods.image import Image from ._methods.post import Post from ._methods.user import User @@ -9,3 +10,4 @@ class neuroapi: admin = Admin() user = User() image = Image() + bot_settings = BotSettings() diff --git a/neuroapi/types/__init__.py b/neuroapi/types/__init__.py index f3e3efe..7e8f67d 100644 --- a/neuroapi/types/__init__.py +++ b/neuroapi/types/__init__.py @@ -1,5 +1,6 @@ from ._admin import Admin from ._bot import NeuroApiBot +from ._bot_settings import BotSettings from ._image import Image from ._post import Post from ._singleton import Singleton diff --git a/neuroapi/types/_bot_settings.py b/neuroapi/types/_bot_settings.py new file mode 100644 index 0000000..f774261 --- /dev/null +++ b/neuroapi/types/_bot_settings.py @@ -0,0 +1,29 @@ +from dataclasses import dataclass +from uuid import UUID + +from ._helpers import * + + +@dataclass +class BotSettings: + uuid: UUID + message_times: List[str] + channel: str + is_active: bool + + @staticmethod + def from_dict(obj: Any) -> 'BotSettings': + assert isinstance(obj, dict) + uuid = UUID(obj.get("uuid")) + message_times = from_list(from_str, obj.get("messageTimes")) + channel = from_str(obj.get("channel")) + is_active = from_bool(obj.get("isActive")) + return BotSettings(uuid, message_times, channel, is_active) + + def to_dict(self) -> dict: + result: dict = {} + result["uuid"] = str(self.uuid) + result["messageTimes"] = from_list(from_str, self.message_times) + result["channel"] = from_str(self.channel) + result["isActive"] = from_bool(self.is_active) + return result