Начальная реализация транспорта ws для max web и прочие улучшения

This commit is contained in:
Alexey Polyakov
2026-04-07 12:36:30 +03:00
parent 52949602af
commit 0ffc649dd9
19 changed files with 873 additions and 228 deletions

View File

@@ -1,6 +1,6 @@
import json
import logging
import random
import json
import time
from textwrap import dedent
@@ -8,10 +8,11 @@ from aiogram import Bot, Dispatcher, Router
from aiogram.filters import Command
from aiogram.types import Message
from common.static import Static
from common.sql_queries import SQLQueries
from common.static import Static
from common.tools import Tools
class TelegramBot:
def __init__(self, token, enabled, db_pool, whitelist_ids=None):
self.bot = Bot(token=token)
@@ -27,7 +28,7 @@ class TelegramBot:
self.msg_types = Static().BotMessageTypes()
self.static = Static()
self.sql_queries = SQLQueries()
self.router.message.register(self.handle_start, Command("start"))
self.router.message.register(self.handle_register, Command("register"))
@@ -37,7 +38,7 @@ class TelegramBot:
async def handle_start(self, message: Message):
tg_id = str(message.from_user.id)
# Ищем привязанный аккаунт пользователя
# Ищем привязанный аккаунт пользователя
async with self.db_pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute(self.sql_queries.SELECT_USER_BY_TG_ID, (tg_id,))
@@ -45,24 +46,26 @@ class TelegramBot:
if account:
# Извлекаем id аккаунта с телефоном
phone = account.get('phone')
phone = account.get("phone")
await message.answer(
self.get_bot_message(self.msg_types.WELCOME_ALREADY_REGISTERED).format(phone=phone)
self.get_bot_message(self.msg_types.WELCOME_ALREADY_REGISTERED).format(
phone=phone
)
)
else:
await message.answer(
self.get_bot_message(self.msg_types.WELCOME_NEW_USER)
)
await message.answer(self.get_bot_message(self.msg_types.WELCOME_NEW_USER))
async def handle_register(self, message: Message):
tg_id = str(message.from_user.id)
# Проверка ID на наличие в белом списке
if tg_id not in self.whitelist_ids:
await message.answer(self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED))
await message.answer(
self.get_bot_message(self.msg_types.ID_NOT_WHITELISTED)
)
return
async with self.db_pool.acquire() as conn:
async with conn.cursor() as cursor:
# Проверка на существование
@@ -77,40 +80,42 @@ class TelegramBot:
new_phone = f"7900{random.randint(1000000, 9999999)}"
updatetime = str(int(time.time() * 1000))
lastseen = str(int(time.time()))
try:
# Создаем юзера
await cursor.execute(
self.sql_queries.INSERT_USER,
(
self.tools.generate_id(),
new_phone, # phone
tg_id, # telegram_id
message.from_user.first_name[:59], # firstname
(message.from_user.last_name or "")[:59], # lastname
(message.from_user.username or "")[:60], # username
json.dumps([]), # profileoptions
json.dumps(["TT", "ONEME"]), # options
0, # accountstatus
new_phone, # phone
tg_id, # telegram_id
message.from_user.first_name[:59], # firstname
(message.from_user.last_name or "")[:59], # lastname
(message.from_user.username or "")[:60], # username
json.dumps([]), # profileoptions
json.dumps(["TT", "ONEME"]), # options
0, # accountstatus
updatetime,
lastseen,
)
),
)
# Добавляем данные о аккаунте
await cursor.execute(
self.sql_queries.INSERT_USER_DATA,
(
new_phone, # phone
json.dumps([]), # contacts
json.dumps(self.static.USER_FOLDERS), # folders
json.dumps(self.static.USER_SETTINGS), # user settings
json.dumps({}), # chat_config
)
new_phone, # phone
json.dumps([]), # contacts
json.dumps(self.static.USER_FOLDERS), # folders
json.dumps(self.static.USER_SETTINGS), # user settings
json.dumps({}), # chat_config
),
)
await message.answer(
self.get_bot_message(self.msg_types.REGISTRATION_SUCCESS).format(new_phone=new_phone)
self.get_bot_message(
self.msg_types.REGISTRATION_SUCCESS
).format(new_phone=new_phone)
)
except Exception as e:
self.logger.error(f"Ошибка при регистрации: {e}")
@@ -119,7 +124,7 @@ class TelegramBot:
)
async def start(self):
if self.enabled == True:
if self.enabled:
try:
await self.dp.start_polling(self.bot)
except Exception as e:
@@ -130,8 +135,10 @@ class TelegramBot:
async def send_auth_code(self, chat_id, phone, code):
try:
await self.bot.send_message(
chat_id,
self.get_bot_message(self.msg_types.INCOMING_CODE).format(phone=phone, code=code)
chat_id,
self.get_bot_message(self.msg_types.INCOMING_CODE).format(
phone=phone, code=code
),
)
except Exception as e:
self.logger.error(f"Ошибка отправки кода в Telegram: {e}")
self.logger.error(f"Ошибка отправки кода в Telegram: {e}")