This repository has been archived on 2025-09-07. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
MouseyBot/Mousey/Bot/Middlewares/database.py

27 lines
836 B
Python

from typing import Callable, Awaitable, Dict, Any
from aiogram import BaseMiddleware
from aiogram.types import TelegramObject
from sqlalchemy.ext.asyncio import async_sessionmaker
class DatabaseSessionMiddleware(BaseMiddleware):
"""
Мидлвари для получения сессии в БД для фильтров, сообщений и прочего.
"""
def __init__(self, session_pool: async_sessionmaker) -> None:
self.session_pool = session_pool
async def __call__(
self,
handler: Callable[
[TelegramObject, Dict[str, Any]], Awaitable[Any]
],
event: TelegramObject,
data: Dict[str, Any],
) -> Any:
async with self.session_pool() as session:
data["session"] = session
return await handler(event, data)