регистрация токенов

This commit is contained in:
mihalin 2021-09-09 20:38:33 +03:00
parent 869df66cd3
commit bf7c18c076
4 changed files with 50 additions and 0 deletions

View File

@ -4,12 +4,14 @@
from aiogram import types from aiogram import types
from aiogram.utils.exceptions import TelegramAPIError from aiogram.utils.exceptions import TelegramAPIError
from olgram.models.models import Bot from olgram.models.models import Bot
from server.server import unregister_token
async def delete_bot(bot: Bot, call: types.CallbackQuery): async def delete_bot(bot: Bot, call: types.CallbackQuery):
""" """
Пользователь решил удалить бота Пользователь решил удалить бота
""" """
await unregister_token(bot.token)
await bot.delete() await bot.delete()
await call.answer("Бот удалён") await call.answer("Бот удалён")
try: try:

View File

@ -11,6 +11,7 @@ from textwrap import dedent
from olgram.models.models import Bot, User from olgram.models.models import Bot, User
from olgram.settings import OlgramSettings from olgram.settings import OlgramSettings
from olgram.commands.menu import send_bots_menu from olgram.commands.menu import send_bots_menu
from server.server import register_token
from olgram.router import dp from olgram.router import dp
@ -94,6 +95,9 @@ async def bot_added(message: types.Message, state: FSMContext):
except TelegramAPIError: except TelegramAPIError:
return await on_unknown_error() 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) 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) bot = Bot(token=token, owner=user, name=test_bot_info.username, super_chat_id=message.from_user.id)
try: try:

View File

@ -29,6 +29,16 @@ class OlgramSettings(AbstractSettings):
return "0.0.0" 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): class BotSettings(AbstractSettings):
@classmethod @classmethod
def token(cls) -> str: def token(cls) -> str:

34
server/server.py Normal file
View File

@ -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()