mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
More docstrings
This commit is contained in:
parent
54d7940691
commit
02cefa1cf3
2 changed files with 60 additions and 4 deletions
|
|
@ -13,7 +13,13 @@ T = TypeVar("T")
|
|||
|
||||
|
||||
class BaseBot(ContextInstanceMixin, DataMixin):
|
||||
def __init__(self, token: str, session: BaseSession = None, parse_mode: Optional[str] = None):
|
||||
"""
|
||||
Base class for bots
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, token: str, session: BaseSession = None, parse_mode: Optional[str] = None
|
||||
) -> None:
|
||||
validate_token(token)
|
||||
|
||||
if session is None:
|
||||
|
|
@ -24,17 +30,37 @@ class BaseBot(ContextInstanceMixin, DataMixin):
|
|||
self.__token = token
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
def id(self) -> int:
|
||||
"""
|
||||
Get bot ID from token
|
||||
|
||||
:return:
|
||||
"""
|
||||
return extract_bot_id(self.__token)
|
||||
|
||||
async def emit(self, method: TelegramMethod[T]) -> T:
|
||||
"""
|
||||
Call API method
|
||||
|
||||
:param method:
|
||||
:return:
|
||||
"""
|
||||
return await self.session.make_request(self.__token, method)
|
||||
|
||||
async def close(self):
|
||||
async def close(self) -> None:
|
||||
"""
|
||||
Close bot session
|
||||
"""
|
||||
await self.session.close()
|
||||
|
||||
@asynccontextmanager
|
||||
async def context(self, auto_close: bool = True):
|
||||
"""
|
||||
Generate bot context
|
||||
|
||||
:param auto_close:
|
||||
:return:
|
||||
"""
|
||||
token = self.set_current(self)
|
||||
try:
|
||||
yield self
|
||||
|
|
@ -43,10 +69,21 @@ class BaseBot(ContextInstanceMixin, DataMixin):
|
|||
await self.close()
|
||||
self.reset_current(token)
|
||||
|
||||
def __hash__(self):
|
||||
def __hash__(self) -> int:
|
||||
"""
|
||||
Get hash for the token
|
||||
|
||||
:return:
|
||||
"""
|
||||
return hash(self.__token)
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
"""
|
||||
Compare current bot with another bot instance
|
||||
|
||||
:param other:
|
||||
:return:
|
||||
"""
|
||||
if not isinstance(other, BaseBot):
|
||||
return False
|
||||
return hash(self) == hash(other)
|
||||
|
|
|
|||
|
|
@ -3,16 +3,35 @@ from dataclasses import dataclass
|
|||
|
||||
@dataclass
|
||||
class TelegramAPIServer:
|
||||
"""
|
||||
Base config for API Endpoints
|
||||
"""
|
||||
|
||||
base: str
|
||||
file: str
|
||||
|
||||
def api_url(self, token: str, method: str) -> str:
|
||||
"""
|
||||
Generate URL for API methods
|
||||
|
||||
:param token: Bot token
|
||||
:param method: API method name (case insensitive)
|
||||
:return: URL
|
||||
"""
|
||||
return self.base.format(token=token, method=method)
|
||||
|
||||
def file_url(self, token: str, path: str) -> str:
|
||||
"""
|
||||
Generate URL for downloading files
|
||||
|
||||
:param token: Bot token
|
||||
:param path: file path
|
||||
:return: URL
|
||||
"""
|
||||
return self.file.format(token=token, path=path)
|
||||
|
||||
|
||||
# Main API server
|
||||
PRODUCTION = TelegramAPIServer(
|
||||
base="https://api.telegram.org/bot{token}/{method}",
|
||||
file="https://api.telegram.org/file/bot{token}/{path}",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue