diff --git a/olgram/commands/bots.py b/olgram/commands/bots.py index 8afa0cb..ae04c31 100644 --- a/olgram/commands/bots.py +++ b/olgram/commands/bots.py @@ -15,6 +15,11 @@ from server.server import register_token from olgram.router import dp +import logging + +logger = logging.getLogger(__name__) + + token_pattern = r'[0-9]{8,10}:[a-zA-Z0-9_-]{35}' @@ -95,9 +100,6 @@ async def bot_added(message: types.Message, state: FSMContext): except TelegramAPIError: return await on_unknown_error() - if not register_token(token): - return await on_unknown_error() - user, _ = await User.get_or_create(telegram_id=message.from_user.id) bot = Bot(token=token, owner=user, name=test_bot_info.username, super_chat_id=message.from_user.id) try: @@ -105,5 +107,9 @@ async def bot_added(message: types.Message, state: FSMContext): except IntegrityError: return await on_duplication_bot() + if not register_token(bot): + await bot.delete() + return await on_unknown_error() + await message.answer("Бот добавлен! Список ваших ботов: /mybots") await state.reset_state() diff --git a/server/server.py b/server/server.py index 4459fd8..cd18338 100644 --- a/server/server.py +++ b/server/server.py @@ -28,6 +28,7 @@ async def register_token(bot: Bot) -> bool: logger.info(f"register token {bot.name}") a_bot = AioBot(bot.token) res = await a_bot.set_webhook(url_for_bot(bot)) + await a_bot.session.close() del a_bot return res @@ -41,6 +42,7 @@ async def unregister_token(token: str): logger.info(f"unregister token {token}") bot = AioBot(token) await bot.delete_webhook() + await bot.session.close() del bot