Бот для капчи в чатах
Roman Sergienko
12 hours ago 5387fa65bbf77233fc2f42d0a986ba275d067d08
web/views.py
@@ -63,7 +63,11 @@
async def dashboard(request) -> web.Response:
    user = await _user(request)
    user = request["user"]
    if user is None:
        if request.headers.get("X-Telegram-Web-App-Platform"):
            raise web.HTTPFound("/miniapp")
        raise web.HTTPFound("/login")
    chats = await allowed_chats(user["user_id"])
    ids = {c["chat_id"] for c in chats}
    names = {c["chat_id"]: c["title"] for c in chats}
@@ -88,13 +92,43 @@
async def chats_page(request) -> web.Response:
    user = await _user(request)
    user = request["user"]
    if user is None:
        if request.headers.get("X-Telegram-Web-App-Platform"):
            raise web.HTTPFound("/miniapp")
        raise web.HTTPFound("/login")
    chats = await allowed_chats(user["user_id"])
    cfgs = {c["chat_id"]: await db.get_chat_cfg(c["chat_id"]) for c in chats}
    alarms = {r["chat_id"]: r for r in await db.list_alarm_chats()}
    return render(request, "chats.html", active="chats", chats=chats, cfgs=cfgs, alarms=alarms)
async def miniapp_page(request) -> web.Response:
    """Выбирает стартовый экран Mini App по chat_id из Telegram launch URL."""
    if request["user"] is None:
        raise web.HTTPFound("/miniapp/login")
    user_id = request["user"]["user_id"]
    chats = await allowed_chats(user_id)
    available_ids = {c["chat_id"] for c in chats}
    try:
        context = int(request.query.get("chat_id", "") or 0)
    except (TypeError, ValueError):
        context = 0
    if context not in available_ids:
        context = 0
    if context:
        raise web.HTTPFound(f"/chats/{context}")
    raise web.HTTPFound("/chats")
async def miniapp_login_page(request) -> web.Response:
    """Стартовая HTML-страница: получает initData через Telegram WebApp JS API."""
    if request["user"] is not None:
        raise web.HTTPFound("/miniapp")
    return render(request, "miniapp_login.html")
async def chat_page(request) -> web.Response:
    user = await _user(request)
    chat_id = int(request.match_info["chat_id"])