mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-14 13:39:42 +03:00
Added getting bot settings from server
This commit is contained in:
@@ -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())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
13
neuroapi/_methods/bot_settings.py
Normal file
13
neuroapi/_methods/bot_settings.py
Normal file
@@ -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
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
29
neuroapi/types/_bot_settings.py
Normal file
29
neuroapi/types/_bot_settings.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user