mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-15 05:59:43 +03:00
Added redis storage using for future
This commit is contained in:
12
main.py
12
main.py
@@ -5,6 +5,8 @@ import signal
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from aiogram.fsm.storage.redis import RedisStorage
|
||||||
|
from redis import asyncio as redis
|
||||||
|
|
||||||
from handlers.admin_commands import AdminCommands
|
from handlers.admin_commands import AdminCommands
|
||||||
from handlers.user_commands import UserCommands
|
from handlers.user_commands import UserCommands
|
||||||
@@ -13,18 +15,20 @@ from neuroapi.types import NeuroApiBot
|
|||||||
|
|
||||||
|
|
||||||
async def delay_bot()->None:
|
async def delay_bot()->None:
|
||||||
if Config().token is None:
|
config = Config()
|
||||||
|
if config.token is None:
|
||||||
logging.warning('Delay bot needs token in environment')
|
logging.warning('Delay bot needs token in environment')
|
||||||
return
|
return
|
||||||
bot = NeuroApiBot(Config().token)
|
bot = NeuroApiBot(config.token, storage=RedisStorage(redis.from_url(config.redis_url)))
|
||||||
bot.include_router(AdminCommands, UserCommands)
|
bot.include_router(AdminCommands, UserCommands)
|
||||||
await bot.start()
|
await bot.start()
|
||||||
|
|
||||||
async def proxy_bot()->None:
|
async def proxy_bot()->None:
|
||||||
if Config().proxy_token is None:
|
config = Config()
|
||||||
|
if config.proxy_token is None:
|
||||||
logging.warning('Proxy bot needs token in environment')
|
logging.warning('Proxy bot needs token in environment')
|
||||||
return
|
return
|
||||||
bot = NeuroApiBot(Config().proxy_token)
|
bot = NeuroApiBot(config.proxy_token)
|
||||||
bot.include_router()
|
bot.include_router()
|
||||||
await bot.start()
|
await bot.start()
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,20 @@ from pydantic_settings import BaseSettings
|
|||||||
|
|
||||||
class GlobalConfig(BaseSettings):
|
class GlobalConfig(BaseSettings):
|
||||||
api_url: str = Field("http://localhost:3000", alias='API_URL')
|
api_url: str = Field("http://localhost:3000", alias='API_URL')
|
||||||
|
|
||||||
|
# Redis config
|
||||||
|
redis_host: str = Field("localhost", alias="REDIS_HOST")
|
||||||
|
redis_port: int = Field(6379, alias="REDIS_PORT")
|
||||||
|
redis_password: str = Field('', alias="REDIS_PASSWORD")
|
||||||
|
redis_db: int = Field(0, alias='REDIS_DB')
|
||||||
|
|
||||||
|
# Bot tokens
|
||||||
token: Optional[str] = Field(None, alias='TOKEN')
|
token: Optional[str] = Field(None, alias='TOKEN')
|
||||||
proxy_token: Optional[str] = Field(None, alias='PROXY_TOKEN')
|
proxy_token: Optional[str] = Field(None, alias='PROXY_TOKEN')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def redis_url(self):
|
||||||
|
return f'redis://:{self.redis_password}@{self.redis_host}:{self.redis_port}/{self.redis_db}'
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
env_file = '.env'
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
|
from aiogram.fsm.storage.redis import RedisStorage
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from handlers.handler import Handler
|
from handlers.handler import Handler
|
||||||
@@ -13,12 +14,12 @@ class NeuroApiBot:
|
|||||||
|
|
||||||
_instances = {}
|
_instances = {}
|
||||||
|
|
||||||
def __init__(self, token: str) -> None:
|
def __init__(self, token: str, storage: RedisStorage | None = None) -> None:
|
||||||
token_data = Token(token=token)
|
token_data = Token(token=token)
|
||||||
self.bot = Bot(token_data.token)
|
self.bot = Bot(token_data.token)
|
||||||
self.dp = Dispatcher()
|
self.dp = Dispatcher(storage=storage)
|
||||||
|
|
||||||
def __new__(cls, token: str) -> 'NeuroApiBot':
|
def __new__(cls, token: str, storage: RedisStorage | None = None) -> 'NeuroApiBot':
|
||||||
token_data = Token(token=token)
|
token_data = Token(token=token)
|
||||||
if token_data.token not in cls._instances:
|
if token_data.token not in cls._instances:
|
||||||
cls._instances[token_data.token] = super(NeuroApiBot, cls).__new__(cls)
|
cls._instances[token_data.token] = super(NeuroApiBot, cls).__new__(cls)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
aiogram==3.1.1
|
aiogram==3.3.0
|
||||||
aioschedule @ https://github.com/AleksHeller/python-aioschedule/archive/refs/heads/master.zip
|
aioschedule @ https://github.com/AleksHeller/python-aioschedule/archive/refs/heads/master.zip
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
python-dateutil==2.8.2
|
python-dateutil==2.8.2
|
||||||
aiohttp==3.8.6
|
aiohttp==3.9.3
|
||||||
pydantic==2.3.0
|
pydantic==2.5.3
|
||||||
pydantic-settings==2.1.0
|
pydantic-settings==2.1.0
|
||||||
uvloop==0.19.0; sys.platform == 'linux'
|
uvloop==0.19.0; sys.platform == 'linux'
|
||||||
|
aioredis==2.0.1
|
||||||
Reference in New Issue
Block a user