Closed
Description
Feature request
Discussion: @aiogram_ru#37921
In some cases need to create temporary instance of bot with different tokens. For example for managers of bot's and etc.
Now it can be done as follows:
You need to create new instance of bot and after all required actions close the session
await tmp_bot.get_me() # -> @FooBot
try:
tmp_bot = Bot('TOKEN HERE') # Create instance of bot
tmp_bot.get_me() # -> @BarBot
finally:
await tmp_bot.close() # Close bot session
await tmp_bot.get_me() # -> @FooBot
What can a solution look like?
contextvars
can be used for do that with context manager.
await tmp_bot.get_me() # -> @FooBot
with bot.with_token('TOKEN HERE'): # Setup token into context
await bot.get_me() # This request will be use token from context. // -> @FooBot
await bot.get_me() # -> @BarBot
But need to don't forget about cachable property (await bot.me). Need prevent to use that while token is changed.
This solution use one instance of bot, don't create more aiohttp client sessions, more easy for configuring and doesn't require a lot of additional code from bot developer.