From bf7c18c0764ac23571cef60e670dc747bf16634e Mon Sep 17 00:00:00 2001 From: mihalin Date: Thu, 9 Sep 2021 20:38:33 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=82=D0=BE=D0=BA=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- olgram/commands/bot_actions.py | 2 ++ olgram/commands/bots.py | 4 ++++ olgram/settings.py | 10 ++++++++++ server/server.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 server/server.py diff --git a/olgram/commands/bot_actions.py b/olgram/commands/bot_actions.py index 19a89cd..268d28c 100644 --- a/olgram/commands/bot_actions.py +++ b/olgram/commands/bot_actions.py @@ -4,12 +4,14 @@ from aiogram import types from aiogram.utils.exceptions import TelegramAPIError from olgram.models.models import Bot +from server.server import unregister_token async def delete_bot(bot: Bot, call: types.CallbackQuery): """ Пользователь решил удалить бота """ + await unregister_token(bot.token) await bot.delete() await call.answer("Бот удалён") try: diff --git a/olgram/commands/bots.py b/olgram/commands/bots.py index 4ee46e9..8afa0cb 100644 --- a/olgram/commands/bots.py +++ b/olgram/commands/bots.py @@ -11,6 +11,7 @@ from textwrap import dedent from olgram.models.models import Bot, User from olgram.settings import OlgramSettings from olgram.commands.menu import send_bots_menu +from server.server import register_token from olgram.router import dp @@ -94,6 +95,9 @@ 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: diff --git a/olgram/settings.py b/olgram/settings.py index e32c578..d4a76d8 100644 --- a/olgram/settings.py +++ b/olgram/settings.py @@ -29,6 +29,16 @@ class OlgramSettings(AbstractSettings): return "0.0.0" +class ServerSettings(AbstractSettings): + @classmethod + def hook_host(cls) -> str: + return cls._get_env("WEBHOOK_HOST") + + @classmethod + def hook_port(cls) -> int: + return int(cls._get_env("WEBHOOK_PORT")) + + class BotSettings(AbstractSettings): @classmethod def token(cls) -> str: diff --git a/server/server.py b/server/server.py new file mode 100644 index 0000000..ad128dc --- /dev/null +++ b/server/server.py @@ -0,0 +1,34 @@ +from aiogram import Bot +import hashlib + +from olgram.settings import ServerSettings + + +def path_for_bot(token: str) -> str: + return "/" + hashlib.md5(token.encode("UTF-8")).hexdigest() + + +def url_for_bot(token: str) -> str: + return f"https://{ServerSettings.hook_host()}:{ServerSettings.hook_port()}" + path_for_bot(token) + + +async def register_token(token: str) -> bool: + """ + Зарегистрировать токен + :param token: токен + :return: получилось ли + """ + bot = Bot(token) + res = await bot.set_webhook(url_for_bot(token)) + await bot.session.close() + return res + + +async def unregister_token(token: str): + """ + Удалить токен + :param token: токен + :return: + """ + bot = Bot(token) + await bot.delete_webhook()