From 3025d981be08a089526de80768e463246007efa0 Mon Sep 17 00:00:00 2001 From: Nikita <43146729+gabbhack@users.noreply.github.com> Date: Fri, 3 May 2019 15:45:08 +0500 Subject: [PATCH 1/2] Implements opportunity to create temporary instance of bot with different tokens --- aiogram/bot/base.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/aiogram/bot/base.py b/aiogram/bot/base.py index 4f55dbd7..644390fe 100644 --- a/aiogram/bot/base.py +++ b/aiogram/bot/base.py @@ -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 @@ -135,6 +137,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 is True: + 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 From 9ef3a630d15a8ee0cd2969406b1cc8365be2b01c Mon Sep 17 00:00:00 2001 From: Nikita <43146729+gabbhack@users.noreply.github.com> Date: Sun, 12 May 2019 11:35:45 +0500 Subject: [PATCH 2/2] Update base.py --- aiogram/bot/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/bot/base.py b/aiogram/bot/base.py index 644390fe..961f213d 100644 --- a/aiogram/bot/base.py +++ b/aiogram/bot/base.py @@ -147,7 +147,7 @@ class BaseBot: @contextlib.contextmanager def with_token(self, bot_token: base.String, validate_token: Optional[base.Boolean] = True): - if validate_token is True: + if validate_token: api.check_token(bot_token) token = self._ctx_token.set(bot_token) try: