From 7cd2882023553d4cc27db04e8d05a6b8653645a6 Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Fri, 04 Dec 2026 07:27:09 +0000
Subject: [PATCH] Add Telegram Mini App for admin panel

---
 web/views.py |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/web/views.py b/web/views.py
index 644d0c8..27853b6 100644
--- a/web/views.py
+++ b/web/views.py
@@ -95,6 +95,43 @@
     return render(request, "chats.html", active="chats", chats=chats, cfgs=cfgs, alarms=alarms)
 
 
+async def miniapp_page(request) -> web.Response:
+    """Выбирает стартовый экран Mini App по проверенному initData Telegram."""
+    user = await _user(request)
+    chats = await allowed_chats(user["user_id"])
+    available_ids = {c["chat_id"] for c in chats}
+    init_data = request.query.get("tgWebAppData", "")
+    context = None
+    if init_data:
+        from web.auth import verify_webapp_init_data
+
+        verified = verify_webapp_init_data(init_data)
+        if verified:
+            try:
+                tg_user = json.loads(verified["user"])
+                if int(tg_user["id"]) == user["user_id"]:
+                    chat = json.loads(verified.get("chat", "{}"))
+                    candidate = int(chat.get("id", 0)) if chat else 0
+                    if not candidate and verified.get("chat_instance"):
+                        instance = verified["chat_instance"]
+                        candidate = int(instance) if instance.lstrip("-").isdigit() else 0
+                    if candidate in available_ids:
+                        context = candidate
+            except (KeyError, TypeError, ValueError):
+                pass
+    if context is None:
+        context = request.query.get("chat_id", "")
+        try:
+            context = int(context)
+        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 chat_page(request) -> web.Response:
     user = await _user(request)
     chat_id = int(request.match_info["chat_id"])

--
Gitblit v1.9.3