mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
регистрация токенов
This commit is contained in:
parent
869df66cd3
commit
bf7c18c076
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
34
server/server.py
Normal file
34
server/server.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user