mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge pull request #130 from gabbhack/different_bot_tokens
Different bot tokens
This commit is contained in:
commit
403d93d490
1 changed files with 20 additions and 0 deletions
|
|
@ -21,6 +21,7 @@ class BaseBot:
|
|||
Base class for bot. It's raw bot.
|
||||
"""
|
||||
_ctx_timeout = ContextVar('TelegramRequestTimeout')
|
||||
_ctx_token = ContextVar('BotDifferentToken')
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -57,6 +58,7 @@ class BaseBot:
|
|||
# Authentication
|
||||
if validate_token:
|
||||
api.check_token(token)
|
||||
self._token = None
|
||||
self.__token = token
|
||||
|
||||
self.proxy = proxy
|
||||
|
|
@ -136,6 +138,24 @@ class BaseBot:
|
|||
finally:
|
||||
self._ctx_timeout.reset(token)
|
||||
|
||||
@property
|
||||
def __token(self):
|
||||
return self._ctx_token.get(self._token)
|
||||
|
||||
@__token.setter
|
||||
def __token(self, value):
|
||||
self._token = value
|
||||
|
||||
@contextlib.contextmanager
|
||||
def with_token(self, bot_token: base.String, validate_token: Optional[base.Boolean] = True):
|
||||
if validate_token:
|
||||
api.check_token(bot_token)
|
||||
token = self._ctx_token.set(bot_token)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._ctx_token.reset(token)
|
||||
|
||||
async def close(self):
|
||||
"""
|
||||
Close all client sessions
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue