init new bot

This commit is contained in:
ada-dmitry
2024-08-26 20:22:16 +03:00
parent 8208bd226e
commit 564398d0dd
1675 changed files with 164420 additions and 115 deletions
+46
View File
@@ -0,0 +1,46 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, Generic, TypeVar, cast
from aiogram.types import Update
if TYPE_CHECKING:
from aiogram import Bot
T = TypeVar("T")
class BaseHandlerMixin(Generic[T]):
if TYPE_CHECKING:
event: T
data: Dict[str, Any]
class BaseHandler(BaseHandlerMixin[T], ABC):
"""
Base class for all class-based handlers
"""
def __init__(self, event: T, **kwargs: Any) -> None:
self.event: T = event
self.data: Dict[str, Any] = kwargs
@property
def bot(self) -> Bot:
from aiogram import Bot
if "bot" in self.data:
return cast(Bot, self.data["bot"])
raise RuntimeError("Bot instance not found in the context")
@property
def update(self) -> Update:
return cast(Update, self.data.get("update", self.data.get("event_update")))
@abstractmethod
async def handle(self) -> Any: # pragma: no cover
pass
def __await__(self) -> Any:
return self.handle().__await__()