""" Telegram bot + admin panel: port of the Cloudflare Worker to Python (FastAPI). Runs on a Hugging Face Gradio/ZeroGPU Space (the tiny Gradio app at the bottom only exists to satisfy the ZeroGPU startup check). """ import os # Gradio's SSR mode (default on Spaces) starts a Node server that can hold port 7860. # We run our own uvicorn on 7860, so it must be off. Set before importing gradio. os.environ["GRADIO_SSR_MODE"] = "False" import asyncio import base64 import contextvars import hashlib import json import logging import re import secrets import socket import time from collections import OrderedDict, defaultdict from datetime import datetime, timedelta, timezone from pathlib import Path from urllib.parse import quote import gradio as gr import httpx import uvicorn from fastapi import APIRouter, BackgroundTasks, Depends, FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, Response from fastapi.staticfiles import StaticFiles logging.basicConfig(level=logging.INFO) log = logging.getLogger("tgbot") # ---- startup diagnostics: if the container ever exits "cleanly" with no error # (HF shows "Exit code: 0"), these lines say who loaded the file and why the # process ended, instead of leaving an empty log. ---- import atexit import faulthandler import sys faulthandler.enable() log.info("app.py loaded: __name__=%s python=%s pid=%s", __name__, sys.version.split()[0], os.getpid()) atexit.register(lambda: log.warning("process exiting (pid=%s, __name__=%s)", os.getpid(), __name__)) BASE = Path(__file__).parent # ----------------------------- config (Space secrets) ----------------------------- BOT_TOKEN = os.environ.get("BOT_TOKEN", "") GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "") GEMINI_MODEL = os.environ.get("GEMINI_MODEL", "gemma-4-26b-a4b-it") WEB_PASSWORD = os.environ.get("WEB_PASSWORD", "") WEBHOOK_SECRET = os.environ.get("WEBHOOK_SECRET", "") PUBLIC_BASE_URL = os.environ.get("PUBLIC_BASE_URL", "").rstrip("/") # e.g. https://panel.yourdomain.com — needed to register each tenant bot's webhook ADMIN_GROUP_ID = os.environ.get("ADMIN_GROUP_ID", "") # private group: /send_msg forwards here, errors are reported here too AI_BATCH_DELAY = float(os.environ.get("AI_BATCH_DELAY", "28")) # seconds of silence before AI answers ASSISTANT_LABEL = os.environ.get("ASSISTANT_LABEL", "sai:dev AI Assistant:") VAPID_SUB = os.environ.get("VAPID_SUB", "mailto:admin@example.com") # Storage backend: prefer a mounted HF Storage Bucket (plain local folder, no # operation limits). Falls back to Cloudflare KV over REST only if DATA_DIR is unset. DATA_DIR = os.environ.get("DATA_DIR", "").strip() CF_ACCOUNT_ID = os.environ.get("CF_ACCOUNT_ID", "") CF_KV_NAMESPACE_ID = os.environ.get("CF_KV_NAMESPACE_ID", "") CF_API_TOKEN = os.environ.get("CF_API_TOKEN", "") try: from zoneinfo import ZoneInfo TZ = ZoneInfo(os.environ.get("BOT_TZ", "Asia/Tehran")) except Exception: TZ = timezone(timedelta(hours=3, minutes=30)) _FA = str.maketrans("0123456789", "۰۱۲۳۴۵۶۷۸۹") def now_str() -> str: return datetime.now(TZ).strftime("%H:%M").translate(_FA) def now_ts() -> int: """Epoch milliseconds, used so the panel can group full history by real calendar day (now_str() above only ever returns a bare HH:MM with no date at all, which is why every day used to render as "امروز").""" return int(datetime.now(TZ).timestamp() * 1000) def load_default_prompt() -> str: p = BASE / "prompt.txt" if p.exists(): return p.read_text(encoding="utf-8") return "تو دستیار هوش مصنوعی sai:dev هستی. کوتاه، صمیمی و مودبانه پاسخ بده." DEFAULT_PROMPT = load_default_prompt() http = httpx.AsyncClient(timeout=30) # ----------------------------- tenants ----------------------------- # Every panel account is a tenant with its own bot token and its own slice of # storage. The tenant for the current request/task lives in a ContextVar, so # kv.get/put, tg(), the per-chat locks/timers etc. all pick it up without # every helper in the file needing a new parameter. "owner" is the original # single-tenant setup: no key prefix at all, so existing data stays exactly # where it is. Any other tenant's keys are stored as T//. # Keys starting with PANEL_ (accounts, sessions, reset tokens, webhook # routes) are global by design. current_tenant: contextvars.ContextVar = contextvars.ContextVar("current_tenant", default="owner") GLOBAL_KEY_PREFIXES = ("PANEL_",) def scoped_key(key: str) -> str: t = current_tenant.get() if t == "owner" or key.startswith(GLOBAL_KEY_PREFIXES): return key return f"T/{t}/{key}" class TDict: """dict (optionally defaultdict-like) whose keys are implicitly scoped to the current tenant — the same Telegram chat id can exist under two different bots, so per-chat in-memory state must never be shared.""" def __init__(self, factory=None): self._d: dict = {} self._factory = factory def _k(self, key): return (current_tenant.get(), key) def __getitem__(self, key): k = self._k(key) if k not in self._d: if self._factory is None: raise KeyError(key) self._d[k] = self._factory() return self._d[k] def __setitem__(self, key, value): self._d[self._k(key)] = value def get(self, key, default=None): return self._d.get(self._k(key), default) def pop(self, key, default=None): return self._d.pop(self._k(key), default) def __contains__(self, key): return self._k(key) in self._d async def run_as_tenant(tenant: str, fn, *args): token = current_tenant.set(tenant) try: return await fn(*args) finally: current_tenant.reset(token) async def resolve_bot_token() -> str: t = current_tenant.get() if t == "owner": return BOT_TOKEN return ((await get_users()).get(t) or {}).get("bot_token", "") # ----------------------------- admin error reporting (throttled) ----------------------------- _last_admin_notify: dict = {} _ADMIN_NOTIFY_COOLDOWN = 300 # seconds; avoid spamming the admin group on repeated failures async def _post_markdown(token: str, chat_id: str, text: str): try: p = {"chat_id": chat_id, "text": text, "parse_mode": "Markdown"} r = await http.post(f"https://api.telegram.org/bot{token}/sendMessage", json=p) if r.status_code != 200: p.pop("parse_mode") await http.post(f"https://api.telegram.org/bot{token}/sendMessage", json=p) except Exception: log.exception("failed to post to admin group") async def current_admin_group() -> str: """The admin group of the CURRENT account: the owner uses the ADMIN_GROUP_ID env var; every other account has its own, set in Settings.""" if current_tenant.get() == "owner": return str(ADMIN_GROUP_ID or "") return str((await get_global_settings()).get("admin_group_id") or "").strip() async def notify_admin_error(key: str, context: str, exc: Exception | None = None): """Error report, at most once per `key` every 5 minutes. The owner's admin group hears about EVERY account's errors (tagged with the account name); a non-owner account's own admin group hears about that account's errors too.""" tenant = current_tenant.get() key = f"{tenant}:{key}" now = time.time() if now - _last_admin_notify.get(key, 0) < _ADMIN_NOTIFY_COOLDOWN: return _last_admin_notify[key] = now detail = f"⚠️ خطای ربات ({key}):\n{context}" if exc: detail += f"\n\n`{type(exc).__name__}: {exc}`"[:900] if ADMIN_GROUP_ID: await _post_markdown(BOT_TOKEN, ADMIN_GROUP_ID, detail if tenant == "owner" else f"👤 حساب «{tenant}»\n" + detail) else: log.error("ADMIN_GROUP_ID not set; cannot notify owner. [%s] %s | %s", key, context, exc) if tenant != "owner": try: gid, token = await current_admin_group(), await resolve_bot_token() if gid and token: await _post_markdown(token, gid, detail) except Exception: log.exception("failed to notify the account's own admin group") # ----------------------------- storage (local Bucket file, or Cloudflare KV fallback) ----------------------------- class Realtime: """Live push channel (WebSocket) for the panel and, later, the native apps. It carries *hints*, never data: "the chat list changed" / "chat X changed". The client reacts by re-fetching through the normal, ETag-cached REST endpoints, so permissions, formatting and pagination stay in exactly one place and a dropped socket can never cause a missed or half-applied update (the REST poll is still there as a slow safety net). Hints are produced centrally in KV.put(): every write of a chat's history, the chat index or a chat's config announces itself, so no individual endpoint / webhook branch has to remember to notify. Bursts are coalesced into one frame per tenant per FLUSH_DELAY.""" FLUSH_DELAY = 0.05 def __init__(self): self.conns: dict = defaultdict(set) # tenant -> {WebSocket} self.pending: dict = {} # tenant -> {"chats": bool, "chat": set[str]} self.flushing: set = set() # Change log behind GET /api/sync: a per-tenant counter plus "last counter value at which X changed". # Kept in memory on purpose — `epoch` is new on every process start, so a cursor from before a restart # is recognised and the client simply does one full refresh instead of trusting a log that is gone. self.epoch = secrets.token_hex(4) self.seq: dict = defaultdict(int) self.index_rev: dict = defaultdict(int) self.chat_rev: dict = defaultdict(dict) def cursor(self, tenant: str) -> str: return f"{self.epoch}:{self.seq[tenant]}" def changes_since(self, tenant: str, cursor: str): """What changed after `cursor`; None = cursor unusable (other epoch / malformed) → caller must full-refresh.""" try: epoch, n = str(cursor).split(":") n = int(n) except ValueError: return None if epoch != self.epoch or n < 0 or n > self.seq[tenant]: return None return {"index": self.index_rev[tenant] > n, "chats": [c for c, r in self.chat_rev[tenant].items() if r > n]} def publish(self, tenant: str, chats: bool = False, chat_id=None): # always recorded (even with nobody connected) so an offline client can sync later self.seq[tenant] += 1 if chats: self.index_rev[tenant] = self.seq[tenant] if chat_id: self.chat_rev[tenant][str(chat_id)] = self.seq[tenant] if not self.conns.get(tenant): return p = self.pending.setdefault(tenant, {"chats": False, "chat": set()}) if chats: p["chats"] = True if chat_id: p["chat"].add(str(chat_id)) if tenant in self.flushing: return try: loop = asyncio.get_running_loop() except RuntimeError: return self.flushing.add(tenant) loop.create_task(self._flush(tenant)) async def _flush(self, tenant: str): await asyncio.sleep(self.FLUSH_DELAY) self.flushing.discard(tenant) p = self.pending.pop(tenant, None) if not p: return frame = json.dumps({"t": "dirty", "chats": p["chats"], "chat": sorted(p["chat"]), "cursor": self.cursor(tenant)}, ensure_ascii=False) for ws in list(self.conns.get(tenant, ())): try: await ws.send_text(frame) except Exception: self.conns[tenant].discard(ws) async def kick(self, tenant: str): """Drop a user's sockets (their sessions were revoked); live clients re-auth on reconnect.""" for ws in list(self.conns.get(tenant, ())): try: await ws.close(code=4401) except Exception: pass self.conns.pop(tenant, None) realtime = Realtime() class KV: """Write-through cache in front of a storage backend. - local (preferred): a mounted HF Storage Bucket, used as a plain folder — no operation limits, no network round-trip. - cloudflare (fallback): Cloudflare KV over REST, used only if DATA_DIR isn't set. Free-tier KV caps out at ~1,000 writes/day, which a chat bot burns through fast.""" def __init__(self): self.cache: dict = {} self.locks: defaultdict = defaultdict(asyncio.Lock) self.backend = "none" if DATA_DIR: try: self.dir = Path(DATA_DIR) self.dir.mkdir(parents=True, exist_ok=True) probe = self.dir / ".write_test" probe.write_text("ok", encoding="utf-8") probe.unlink(missing_ok=True) self.backend = "local" log.info("KV backend: local bucket at %s", self.dir) except Exception as e: log.error("DATA_DIR (%s) is not writable, falling back: %s", DATA_DIR, e) if self.backend == "none" and CF_ACCOUNT_ID and CF_KV_NAMESPACE_ID and CF_API_TOKEN: self.base = ( f"https://api.cloudflare.com/client/v4/accounts/{CF_ACCOUNT_ID}" f"/storage/kv/namespaces/{CF_KV_NAMESPACE_ID}/values" ) self.headers = {"Authorization": f"Bearer {CF_API_TOKEN}"} self.backend = "cloudflare" log.info("KV backend: Cloudflare KV (fallback)") if self.backend == "none": log.error("No storage backend configured (set DATA_DIR, or the CF_* secrets). Data will not persist.") @staticmethod def _announce(raw_key: str): """Turn a write of a chat-related key into a realtime hint for the current tenant.""" if raw_key == "CHAT_INDEX" or raw_key.startswith("CHAT_CFG_"): realtime.publish(current_tenant.get(), chats=True) elif raw_key.startswith("CHAT_"): realtime.publish(current_tenant.get(), chat_id=raw_key[5:]) def _path(self, key: str) -> Path: return self.dir / (quote(key, safe="") + ".dat") async def get(self, key: str): key = scoped_key(key) if key in self.cache: return self.cache[key] value = None if self.backend == "local": value = await self._local_get(key) elif self.backend == "cloudflare": value = await self._cf_get(key) self.cache[key] = value return value async def put(self, key: str, value: str, ttl: int | None = None) -> bool: self._announce(key) key = scoped_key(key) self.cache[key] = value if self.backend == "local": return await self._local_put(key, value) elif self.backend == "cloudflare": return await self._cf_put(key, value, ttl) await notify_admin_error("kv_no_backend", f"ذخیره‌سازی برای کلید {key} انجام نشد: هیچ بک‌اندی تنظیم نشده.") return False async def purge_prefix(self, prefix: str): """Erases every stored key starting with `prefix` (a deleted tenant's whole slice). Returns the number removed, or None when the backend (Cloudflare KV fallback) has no cheap way to list/delete by prefix.""" for k in [k for k in self.cache if k.startswith(prefix)]: self.cache.pop(k, None) if self.backend != "local": return None pattern = quote(prefix, safe="") + "*.dat" def _rm(): n = 0 for p in self.dir.glob(pattern): p.unlink(missing_ok=True) n += 1 return n return await asyncio.to_thread(_rm) async def _local_get(self, key: str): async with self.locks[key]: p = self._path(key) if not p.exists(): return None try: return await asyncio.to_thread(p.read_text, encoding="utf-8") except Exception as e: log.error("LocalKV get %s failed: %s", key, e) await notify_admin_error("kv_local_get", f"خواندن {key} از Storage Bucket شکست خورد.", e) return None async def _local_put(self, key: str, value: str) -> bool: async with self.locks[key]: p = self._path(key) tmp = p.with_suffix(p.suffix + ".tmp") try: await asyncio.to_thread(tmp.write_text, value, encoding="utf-8") await asyncio.to_thread(os.replace, str(tmp), str(p)) return True except Exception as e: log.error("LocalKV put %s failed: %s", key, e) await notify_admin_error("kv_local_put", f"نوشتن {key} روی Storage Bucket شکست خورد.", e) return False async def _cf_get(self, key: str): try: r = await http.get(f"{self.base}/{quote(key, safe='')}", headers=self.headers) if r.status_code == 404: return None if r.status_code == 200: return r.text log.error("KV get %s -> %s %s", key, r.status_code, r.text[:200]) await notify_admin_error("kv_cf_get", f"خواندن {key} از Cloudflare KV شکست خورد ({r.status_code}).") return None except Exception as e: log.error("KV get %s failed: %s", key, e) await notify_admin_error("kv_cf_get_exc", f"خواندن {key} از Cloudflare KV با خطا مواجه شد.", e) return None async def _cf_put(self, key: str, value: str, ttl: int | None) -> bool: params = {"expiration_ttl": max(60, ttl)} if ttl else None try: r = await http.put( f"{self.base}/{quote(key, safe='')}", headers={**self.headers, "Content-Type": "text/plain"}, params=params, content=value.encode("utf-8"), ) if r.status_code == 200: return True log.error("KV put %s -> %s %s", key, r.status_code, r.text[:200]) await notify_admin_error( "kv_cf_put", f"نوشتن {key} روی Cloudflare KV شکست خورد ({r.status_code}). " "احتمالاً سقف روزانه‌ی نوشتن (۱۰۰۰ عملیات) پر شده — بهتره به Storage Bucket سوییچ کنی.", ) return False except Exception as e: log.error("KV put %s failed: %s", key, e) await notify_admin_error("kv_cf_put_exc", f"نوشتن {key} روی Cloudflare KV با خطا مواجه شد.", e) return False kv = KV() chat_locks = TDict(asyncio.Lock) index_lock = asyncio.Lock() bg_tasks: set = set() async def send_and_log(chat_id: str, text: str, biz_id=None, *, role: str = "bot_auto", sender_label: str = "ربات", title=None, is_group=None, reply_to: int | None = None) -> dict: """Sends a text message as the bot/business-connection AND records it in local history + the sidebar index — so anything the bot says on its own (welcome message, /send_msg confirmation, error notices, AI replies) is visible in the panel too, not just messages sent manually from the UI.""" res = await send_tg(chat_id, text, biz_id, reply_to=reply_to) if res.get("ok"): msg_id = (res.get("result") or {}).get("message_id") await append_history(chat_id, { "id": str(int(datetime.now().timestamp() * 1000)), "msg_id": msg_id, "role": role, "text": text, "sender": sender_label, "time": now_str(), "ts": now_ts(), }) await update_index(chat_id, text, sender_label, title, biz_id, is_group=is_group, bot_direct=(biz_id is None)) return res # per-chat AI debounce state ai_timers = TDict() ai_biz_ids = TDict() def track(coro): t = asyncio.create_task(coro) bg_tasks.add(t) t.add_done_callback(bg_tasks.discard) return t # ----------------------------- data helpers ----------------------------- def fmt_id(x) -> str: if not x: return "" x = str(x).strip() if re.fullmatch(r"-?\d+", x): return x return x if x.startswith("@") else f"@{x}" async def get_global_settings() -> dict: s = { "ai_mode": True, "system_prompt": DEFAULT_PROMPT, "poll_interval": 3000, "gemini_model": GEMINI_MODEL, "batch_delay_seconds": AI_BATCH_DELAY, "owner_user_id": "", # your own Telegram numeric user id, for group-message attribution "sticker_packs": "", # comma-separated sticker-set short names for the composer's sticker panel "gemini_api_key": "", # each account uses its OWN Google AI Studio key (owner falls back to the GEMINI_API_KEY env) "admin_group_id": "", # this account's own admin group: /send_msg + error reports (owner uses ADMIN_GROUP_ID env) } raw = await kv.get("GLOBAL_SETTINGS") if raw: s.update(json.loads(raw)) return s async def get_chat_config(chat_id) -> dict: if not chat_id: return {"ai_mode": True, "system_prompt": ""} raw = await kv.get(f"CHAT_CFG_{chat_id}") return json.loads(raw) if raw else {"system_prompt": ""} async def get_history(chat_id) -> list: raw = await kv.get(f"CHAT_{chat_id}") return json.loads(raw) if raw else [] HISTORY_MAX_MESSAGES = None # no cap — every message (and every voice/photo/ # video/sticker file_id alongside it) is kept, regardless of how long or # heavy a chat gets. There used to be a cap here (first 60, then 5000); both # silently deleted older messages once a chat passed that size. async def append_history(chat_id: str, item: dict) -> list: async with chat_locks[chat_id]: h = await get_history(chat_id) h.append(item) if HISTORY_MAX_MESSAGES: h = h[-HISTORY_MAX_MESSAGES:] await kv.put(f"CHAT_{chat_id}", json.dumps(h, ensure_ascii=False), ttl=604800) return h async def get_index() -> list: raw = await kv.get("CHAT_INDEX") return json.loads(raw) if raw else [] async def update_index(chat_id, last_text, last_sender, title=None, biz_id=None, is_group=None, bot_direct=None): """`bot_direct=True` records that the bot has a plain (non-business) chat with this id — i.e. it can sendMessage there directly. This is sticky (once True, always True) so it doesn't flicker as different message types come in. `is_group` and `business_connection_id` are likewise only ever written when actually known, never cleared.""" async with index_lock: index = await get_index() item = next((i for i in index if i["id"] == chat_id), None) if not item: item = {"id": chat_id, "title": title or f"چت {chat_id}"} item["last_text"] = last_text item["last_sender"] = last_sender item["time"] = now_str() if title: item["title"] = title if biz_id: item["business_connection_id"] = biz_id if is_group is not None: item["is_group"] = is_group if bot_direct: item["has_bot_chat"] = True index = [i for i in index if i["id"] != chat_id] index.insert(0, item) # No cap, on purpose: this used to silently keep only the 50 # most-recently-active chats and drop the rest from the sidebar list # — the chat's actual history was still there in storage, it just # became impossible to find or open. Every chat stays listed now, # however many there are. await kv.put("CHAT_INDEX", json.dumps(index, ensure_ascii=False)) # ----------------------------- Telegram helpers ----------------------------- def humanize_tg_error(desc: str | None) -> str: """Telegram's raw API error strings are in English and often confusing (e.g. "Forbidden: bot can't initiate conversation with a user" when the panel tried to send "as bot" to someone who only ever talked to the connected Business account). Translate the common ones.""" if not desc: return "خطای نامشخصی از تلگرام دریافت شد." d = desc.lower() if "can't initiate conversation" in d or "bot can't initiate" in d: return ("این کاربر مستقیماً به ربات پیام نداده (فقط با اکانت بیزینسی چت کرده)، " "پس ربات نمی‌تونه بهش پیام شروع کنه. حالت ارسال «من» رو انتخاب کن.") if "chat not found" in d: return "این چت/کاربر پیدا نشد. شناسه رو بررسی کن." if "bot was blocked" in d or ("blocked" in d and "user" in d): return "کاربر ربات رو بلاک کرده." if "not enough rights" in d or "have no rights" in d or "not enough permission" in d: return "ربات دسترسی لازم رو نداره (باید ادمین گروه باشه)." if "business connection" in d and ("not found" in d or "invalid" in d or "expired" in d): return "اتصال بیزینسی معتبر نیست یا قطع شده." if "message to edit not found" in d: return "پیام اصلی برای ویرایش پیدا نشد (شاید حذف شده)." if "message can't be deleted" in d or "message to delete not found" in d: return "این پیام قابل حذف نیست یا قبلاً حذف شده." if "too many requests" in d: m = re.search(r"retry after (\d+)", d) wait = f" (حدود {max(1, int(m.group(1)) // 60)} دقیقه‌ی دیگر)" if m else "" return "تلگرام این تغییر را موقتاً محدود کرده؛ بعداً دوباره امتحان کن" + wait + "." return desc async def tg(method: str, payload: dict | None = None) -> dict: token = await resolve_bot_token() if not token: return {"ok": False, "description": "برای این حساب هنوز رباتی متصل نشده است."} try: r = await http.post(f"https://api.telegram.org/bot{token}/{method}", json=payload or {}) return r.json() except Exception as e: log.error("tg %s failed: %s", method, e) return {"ok": False, "description": str(e)} async def send_tg(chat_id, text, biz_id=None, reply_to: int | None = None) -> dict: p = {"chat_id": chat_id, "text": text, "parse_mode": "Markdown"} if biz_id: p["business_connection_id"] = biz_id if reply_to: p["reply_parameters"] = {"message_id": int(reply_to), "allow_sending_without_reply": True} res = await tg("sendMessage", p) if not res.get("ok") and "parse" in (res.get("description") or "").lower(): p.pop("parse_mode") res = await tg("sendMessage", p) return res async def send_media_tg(chat_id, method: str, field: str, filename: str, content: bytes, mime: str, caption: str = "", biz_id=None, reply_to: int | None = None) -> dict: """multipart upload to sendPhoto/sendVideo/sendVoice/sendDocument.""" data = {"chat_id": str(chat_id)} if caption: data["caption"] = caption if biz_id: data["business_connection_id"] = biz_id if reply_to: data["reply_parameters"] = json.dumps({"message_id": int(reply_to), "allow_sending_without_reply": True}) token = await resolve_bot_token() if not token: return {"ok": False, "description": "برای این حساب هنوز رباتی متصل نشده است."} try: r = await http.post( f"https://api.telegram.org/bot{token}/{method}", data=data, files={field: (filename, content, mime)}, timeout=120, ) return r.json() except Exception as e: log.error("send_media_tg %s failed: %s", method, e) return {"ok": False, "description": str(e)} _bot_id_cache: dict = {} async def get_bot_id() -> int | None: """Cached bot user id, needed to tell "my reaction" apart from the contact's.""" tkey = current_tenant.get() if tkey in _bot_id_cache: return _bot_id_cache[tkey] res = await tg("getMe") bid = (res.get("result") or {}).get("id") if res.get("ok") else None if bid: _bot_id_cache[tkey] = bid return bid async def edit_tg(chat_id, message_id, text, biz_id=None) -> dict: p = {"chat_id": chat_id, "message_id": int(message_id), "text": text, "parse_mode": "Markdown"} if biz_id: p["business_connection_id"] = biz_id res = await tg("editMessageText", p) if not res.get("ok") and "parse" in (res.get("description") or "").lower(): p.pop("parse_mode") res = await tg("editMessageText", p) return res # ----------------------------- Web Push (VAPID) ----------------------------- def _b64url(b: bytes) -> str: return base64.urlsafe_b64encode(b).rstrip(b"=").decode() async def get_vapid() -> dict: raw = await kv.get("VAPID_KEYS") if raw: d = json.loads(raw) if d.get("privatePem"): return d from cryptography.hazmat.primitives import serialization from py_vapid import Vapid v = Vapid() v.generate_keys() pub = v.public_key.public_bytes( serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint ) d = {"publicKey": _b64url(pub), "privatePem": v.private_pem().decode()} await kv.put("VAPID_KEYS", json.dumps(d)) return d def _push_sync(sub: dict, payload: str, pem: str): from py_vapid import Vapid from pywebpush import webpush webpush( subscription_info=sub, data=payload, vapid_private_key=Vapid.from_pem(pem.encode()), vapid_claims={"sub": VAPID_SUB}, ttl=86400, ) async def send_push(title: str, body: str): try: raw = await kv.get("PUSH_SUBSCRIPTION") sub = json.loads(raw) if raw else None if not sub: return keys = await get_vapid() payload = json.dumps({"title": title, "body": body, "url": "/"}, ensure_ascii=False) await asyncio.to_thread(_push_sync, sub, payload, keys["privatePem"]) except Exception as e: log.error("Push error: %s", e) # ----------------------------- AI mode: single source of truth ----------------------------- async def resolve_gemini_key() -> str: """Every account brings its own Google AI Studio key. Only the owner may fall back to the shared GEMINI_API_KEY env; other accounts without a key simply get no AI replies (they never spend the owner's quota).""" own = str((await get_global_settings()).get("gemini_api_key") or "").strip() if own: return own return GEMINI_API_KEY if current_tenant.get() == "owner" else "" async def get_ai_status(chat_id: str) -> tuple[bool, str, str, float]: """Per-chat setting always wins when explicitly set; otherwise falls back to the global switch/prompt. Re-read at both schedule time and flush time so a toggle flipped mid-wait is always respected. Model + batch delay are always the global (panel-configured) values — no per-chat override for those.""" cfg = await get_chat_config(chat_id) gs = await get_global_settings() enabled = cfg["ai_mode"] if cfg.get("ai_mode") is not None else gs["ai_mode"] prompt = (cfg.get("system_prompt") or "").strip() or gs["system_prompt"] model = (gs.get("gemini_model") or GEMINI_MODEL).strip() delay = float(gs.get("batch_delay_seconds") or AI_BATCH_DELAY) return bool(enabled), prompt, model, delay # ----------------------------- AI batching (debounce) ----------------------------- async def schedule_ai_flush(chat_id: str, biz_id): """Cancel any pending flush for this chat and schedule a new one. Every new message from the user pushes the AI reply back, so a burst of messages is answered once, together, ~batch_delay_seconds after the last one.""" ai_biz_ids[chat_id] = biz_id old = ai_timers.get(chat_id) if old and not old.done(): old.cancel() gs = await get_global_settings() delay = float(gs.get("batch_delay_seconds") or AI_BATCH_DELAY) async def _wait_then_flush(): try: await asyncio.sleep(delay) await flush_ai(chat_id) except asyncio.CancelledError: pass finally: if ai_timers.get(chat_id) is asyncio.current_task(): ai_timers.pop(chat_id, None) realtime.publish(current_tenant.get(), chats=True) # indicator cleared ai_timers[chat_id] = asyncio.create_task(_wait_then_flush()) realtime.publish(current_tenant.get(), chats=True) # chat now shows "preparing a reply" async def flush_ai(chat_id: str): biz_id = ai_biz_ids.get(chat_id) try: ai_on, prompt, model, _delay = await get_ai_status(chat_id) if not ai_on: return history = await get_history(chat_id) if not history: return contents = [ {"role": "user" if h["role"] in ("user", "owner") else "model", "parts": [{"text": h["text"]}]} for h in history if h.get("text") ] if not contents: return key = await resolve_gemini_key() if not key: await notify_admin_error("gemini_key_missing", "کلید Gemini برای این حساب تنظیم نشده، پس هوش مصنوعی پاسخی نمی‌دهد (تنظیمات ← هوش مصنوعی).") return r = await http.post( f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent", headers={"x-goog-api-key": key}, json={"systemInstruction": {"parts": [{"text": prompt}]}, "contents": contents}, timeout=90, ) data = r.json() parts = ((data.get("candidates") or [{}])[0].get("content") or {}).get("parts") or [] if not parts: raise RuntimeError(f"Gemini returned no parts: {str(data)[:300]}") answer = [p for p in parts if not p.get("thought")] reply = "\n".join(p.get("text", "") for p in answer) if answer else parts[-1].get("text", "") reply = reply.strip() if not reply: return branded = f"{ASSISTANT_LABEL}\n{reply}" res = await send_and_log(chat_id, branded, biz_id, role="model", sender_label="دستیار (AI)") if not res.get("ok"): raise RuntimeError(f"Telegram sendMessage failed: {res.get('description')}") except Exception as e: log.exception("flush_ai failed for chat %s", chat_id) await notify_admin_error("flush_ai", f"پردازش هوش مصنوعی برای چت `{chat_id}` شکست خورد.", e) try: await send_and_log( chat_id, "متاسفم، الان در پردازش پیامت خطایی پیش اومد 🙏 چند لحظه دیگه دوباره امتحان کن، " "یا برای اطمینان از دستور زیر استفاده کن:\n/send_msg <پیام>", biz_id, ) except Exception: log.exception("failed to notify user about AI error") # ----------------------------- bot commands (never go to AI) ----------------------------- async def forward_to_admin_group(msg: dict, text: str, sender_name: str) -> bool: gid = await current_admin_group() if not gid: log.error("no admin group configured for account %s", current_tenant.get()) return False frm = msg.get("from") or {} uid = frm.get("id") username = frm.get("username") if username: link = f"[{sender_name}](https://t.me/{username})" elif uid: link = f"[{sender_name}](tg://user?id={uid})" else: link = sender_name formatted = f"{link}:\n{text}\n\nساعت ارسال: {now_str()}" res = await send_tg(gid, formatted) return bool(res.get("ok")) _biz_owner_cache = TDict() async def business_owner_id(biz_id): """Who owns a business connection = the account holder, as Telegram itself reports it. No hand-typed numeric id needed (and none to get wrong) to tell the account holder's own messages from their customers'.""" if not biz_id: return None cached = _biz_owner_cache.get(biz_id) if cached: return cached res = await tg("getBusinessConnection", {"business_connection_id": biz_id}) uid = ((res.get("result") or {}).get("user") or {}).get("id") if res.get("ok") else None if uid: _biz_owner_cache[biz_id] = uid return uid async def _remember_owner_id(uid): """First time we learn the account holder's id from a business connection, save it (only if empty): that also makes DM detection and password recovery work without anyone typing the id.""" gs = dict(await get_global_settings()) if str(gs.get("owner_user_id") or "").strip(): return gs["owner_user_id"] = str(uid) await kv.put("GLOBAL_SETTINGS", json.dumps(gs, ensure_ascii=False)) ADMIN_USER_COMMANDS = {"/signup", "/userlist", "/resetpass", "/suspend", "/unsuspend", "/deleteuser", "/setbot", "/settgid"} TENANT_WEBHOOK_UPDATES = ["message", "business_connection", "business_message", "edited_business_message", "deleted_business_messages", "message_reaction", "channel_post"] # ---- account management: ONE implementation, used by both the admin-group # commands below and the owner's graphical panel (/api/admin/users...). Each # returns (ok, message_for_the_owner). Nothing here ever returns or logs a # password, hash or bot token. ---- USERNAME_RE = re.compile(r"[a-zA-Z0-9_]{3,32}") def _public_user(uname: str, u: dict) -> dict: return { "username": uname, "suspended": bool(u.get("suspended")), "has_bot": bool(u.get("bot_token")), "bot_username": u.get("bot_username", ""), "created_ts": u.get("created_ts"), } TELEGRAM_ID_RE = re.compile(r"\d{1,15}") async def _read_tenant_owner_id(username: str) -> str: gs = await run_as_tenant(username, get_global_settings) return str(gs.get("owner_user_id") or "").strip() async def _write_tenant_owner_id(username: str, tid: str): async def _w(): gs = dict(await get_global_settings()) gs["owner_user_id"] = tid await kv.put("GLOBAL_SETTINGS", json.dumps(gs, ensure_ascii=False)) await run_as_tenant(username, _w) async def acct_list() -> list[dict]: out = [] for n, u in (await get_users()).items(): d = _public_user(n, u) try: d["telegram_id"] = await _read_tenant_owner_id(n) except Exception: d["telegram_id"] = "" d["recovery_ready"] = bool(d["telegram_id"] and d["has_bot"]) out.append(d) return out async def acct_create(username: str, password: str, telegram_id: str = "") -> tuple[bool, str]: username = (username or "").strip() telegram_id = (telegram_id or "").strip() if not USERNAME_RE.fullmatch(username): return False, "یوزرنیم فقط حروف لاتین/عدد/آندرلاین، ۳ تا ۳۲ کاراکتر." if len(password or "") < 6: return False, "رمز عبور باید حداقل ۶ کاراکتر باشد." if telegram_id and not TELEGRAM_ID_RE.fullmatch(telegram_id): return False, "آیدی عددی تلگرام فقط عدد است (مثل 123456789)." users = await get_users() if username in users or username in ("owner", "admin"): return False, f"یوزرنیم «{username}» قبلاً وجود دارد." digest, salt = hash_password(password) users[username] = {"password_hash": digest, "salt": salt, "suspended": False, "created_ts": now_ts()} await save_users(users) await kv.purge_prefix(f"T/{username}/") # never inherit leftovers from an earlier account with this name if telegram_id: await _write_tenant_owner_id(username, telegram_id) return True, f"حساب «{username}» ساخته شد." async def acct_set_password(username: str, new_password: str) -> tuple[bool, str]: if len(new_password or "") < 6: return False, "رمز عبور باید حداقل ۶ کاراکتر باشد." users = await get_users() if username not in users: return False, f"کاربر «{username}» پیدا نشد." digest, salt = hash_password(new_password) users[username]["password_hash"], users[username]["salt"] = digest, salt await save_users(users) await revoke_sessions_for(username) return True, f"رمز «{username}» تغییر کرد و از پنل خارج شد." async def acct_set_suspended(username: str, suspended: bool) -> tuple[bool, str]: users = await get_users() if username not in users: return False, f"کاربر «{username}» پیدا نشد." users[username]["suspended"] = bool(suspended) await save_users(users) if suspended: await revoke_sessions_for(username) return True, f"«{username}» تعلیق شد." return True, f"تعلیق «{username}» برداشته شد." async def acct_set_telegram_id(username: str, telegram_id: str) -> tuple[bool, str]: """The account holder's own Telegram id — used both to tell their own messages from their customers' and as the address for password recovery.""" telegram_id = (telegram_id or "").strip() if telegram_id and not TELEGRAM_ID_RE.fullmatch(telegram_id): return False, "آیدی عددی تلگرام فقط عدد است (مثل 123456789)." if username not in await get_users(): return False, f"کاربر «{username}» پیدا نشد." await _write_tenant_owner_id(username, telegram_id) return True, "آیدی تلگرام ذخیره شد." if telegram_id else "آیدی تلگرام پاک شد." async def acct_set_bot(username: str, token: str) -> tuple[bool, str]: token = (token or "").strip() users = await get_users() if username not in users: return False, f"کاربر «{username}» پیدا نشد." if not PUBLIC_BASE_URL: return False, "متغیر PUBLIC_BASE_URL (مثلاً https://panel.yourdomain.com) روی Space تنظیم نشده؛ بدونش نمی‌شه webhook ثبت کرد." if not token: return False, "توکن ربات خالیه." if token == BOT_TOKEN or any(u.get("bot_token") == token for n, u in users.items() if n != username): return False, "این توکن قبلاً به حساب دیگری وصل شده." try: me = (await http.post(f"https://api.telegram.org/bot{token}/getMe")).json() except Exception: me = {} if not me.get("ok"): return False, "توکن نامعتبره (getMe شکست خورد)." secret = secrets.token_urlsafe(24) try: sw = (await http.post(f"https://api.telegram.org/bot{token}/setWebhook", json={ "url": f"{PUBLIC_BASE_URL}/webhook/{secret}", "secret_token": secret, "allowed_updates": TENANT_WEBHOOK_UPDATES, })).json() except Exception as e: sw = {"ok": False, "description": str(e)} if not sw.get("ok"): return False, f"ثبت webhook شکست خورد: {sw.get('description')}" routes = await get_tenant_routes() old_secret, old_token = users[username].get("webhook_secret"), users[username].get("bot_token") if old_secret: routes.pop(old_secret, None) if old_token and old_token != token: try: await http.post(f"https://api.telegram.org/bot{old_token}/deleteWebhook") except Exception: pass routes[secret] = username await kv.put(TENANT_ROUTES_KEY, json.dumps(routes, ensure_ascii=False)) bot_username = (me.get("result") or {}).get("username", "") users[username].update(bot_token=token, webhook_secret=secret, bot_username=bot_username) await save_users(users) return True, f"ربات @{bot_username} به حساب «{username}» وصل شد." async def acct_delete(username: str) -> tuple[bool, str]: users = await get_users() if username not in users: return False, f"کاربر «{username}» پیدا نشد." gone = users.pop(username) or {} await save_users(users) await revoke_sessions_for(username) if gone.get("webhook_secret"): routes = await get_tenant_routes() routes.pop(gone["webhook_secret"], None) await kv.put(TENANT_ROUTES_KEY, json.dumps(routes, ensure_ascii=False)) if gone.get("bot_token"): try: await http.post(f"https://api.telegram.org/bot{gone['bot_token']}/deleteWebhook") except Exception: pass purged = await kv.purge_prefix(f"T/{username}/") note = f"{purged} فایل داده پاک شد." if purged is not None else "داده‌ها پاک نشد (ذخیره‌سازی فعلی Bucket محلی نیست)." return True, f"کاربر «{username}» حذف شد؛ webhook ربات برداشته شد. {note}" async def handle_admin_command(chat_id: str, text: str, biz_id): """Telegram-group front-end for the account services above — owner-only, admin-group-only (checked by the caller).""" async def say(msg): await send_and_log(chat_id, msg, biz_id) try: parts = text.strip().split() cmd, args = parts[0].split("@")[0].lower(), parts[1:] if cmd == "/userlist": users = await acct_list() if not users: return await say("هنوز هیچ کاربری ثبت نشده.") lines = [f"{u['username']} — {'🔴 تعلیق‌شده' if u['suspended'] else '🟢 فعال'} — " + (f"🤖 @{u['bot_username']}" if u["has_bot"] else "⚪️ بدون ربات") + (f" — 🆔 {u['telegram_id']}" if u["telegram_id"] else " — ⚠️ بدون آیدی تلگرام") for u in users] return await say("کاربران پنل:\n" + "\n".join(lines)) usage = { "/signup": "/signup [telegram_id]", "/resetpass": "/resetpass ", "/setbot": "/setbot ", "/settgid": "/settgid ", "/suspend": "/suspend ", "/unsuspend": "/unsuspend ", "/deleteuser": "/deleteuser ", } need = 2 if cmd in ("/signup", "/resetpass", "/setbot", "/settgid") else 1 if len(args) < need: return await say("فرمت درست:\n" + usage.get(cmd, cmd)) if cmd == "/signup": ok, msg = await acct_create(args[0], args[1], args[2] if len(args) > 2 else "") return await say((f"✅ {msg}\nبرای وصل کردن رباتش:\n/setbot {args[0]} ") if ok else msg) if cmd == "/resetpass": ok, msg = await acct_set_password(args[0], args[1]) elif cmd == "/setbot": ok, msg = await acct_set_bot(args[0], args[1]) elif cmd == "/settgid": ok, msg = await acct_set_telegram_id(args[0], args[1]) elif cmd in ("/suspend", "/unsuspend"): ok, msg = await acct_set_suspended(args[0], cmd == "/suspend") elif cmd == "/deleteuser": ok, msg = await acct_delete(args[0]) else: return await say(("✅ " if ok else "") + msg) except Exception as e: log.exception("handle_admin_command failed") await notify_admin_error("handle_admin_command", f"دستور «{text.split()[0]}» شکست خورد.", e) async def handle_command(chat_id: str, msg: dict, text: str, biz_id, sender_name: str): try: parts = text.strip().split(maxsplit=1) cmd = parts[0].split("@")[0].lower() # strip /cmd@BotName form arg = parts[1].strip() if len(parts) > 1 else "" if cmd == "/start": await send_and_log( chat_id, f"{ASSISTANT_LABEL}\nسلام! من دستیار هوش مصنوعی sai:dev هستم. " "هر چی بنویسی می‌خونم و در جریانش می‌ذارمش. برای رسوندن پیام مهم و فوری هم از دستور زیر استفاده کن:\n" "/send_msg <پیام شما>", biz_id, ) return if cmd == "/send_msg": if not arg: await send_and_log( chat_id, "لطفاً پیامت رو همراه دستور بنویس. مثال:\n/send_msg سلام، می‌خواستم در مورد ... باهات صحبت کنم.", biz_id, ) return ok = await forward_to_admin_group(msg, arg, sender_name) if ok: await send_and_log(chat_id, "پیامت با موفقیت ارسال شد ✅ به‌زودی جوابتو می‌گیری.", biz_id) else: await send_and_log(chat_id, "متاسفم، در ارسال پیام مشکلی پیش اومد. لطفاً دوباره امتحان کن.", biz_id) await notify_admin_error("send_msg_forward", f"فوروارد /send_msg از چت `{chat_id}` شکست خورد (آیدی گروه ادمین درست تنظیم شده و ربات عضو گروهه؟).") return await send_and_log( chat_id, "این دستور رو نمی‌شناسم. برای رسوندن پیامت به من از دستور زیر استفاده کن:\n/send_msg <پیام شما>", biz_id, ) except Exception as e: log.exception("handle_command failed") await notify_admin_error("handle_command", f"اجرای دستور از چت `{chat_id}` شکست خورد: {text[:80]}", e) try: await send_and_log(chat_id, "متاسفم، در انجام عملیات خطایی پیش اومد. لطفاً دوباره امتحان کن.", biz_id) except Exception: pass # ----------------------------- incoming Telegram updates ----------------------------- async def handle_update(update: dict): chat_id = None biz = None try: msg = update.get("business_message") or update.get("message") or update.get("channel_post") if not msg: return chat = msg["chat"] chat_id = str(chat["id"]) sender = (msg.get("from") or {}).get("first_name") or chat.get("title") or "مخاطب" biz = msg.get("business_connection_id") is_group = chat.get("type") in ("group", "supergroup") from_id = (msg.get("from") or {}).get("id") gs_owner = str((await get_global_settings()).get("owner_user_id") or "").strip() is_configured_owner = bool(gs_owner) and str(from_id) == gs_owner # strict: gates the admin commands is_owner = is_configured_owner if not is_owner and biz: bo = await business_owner_id(biz) if bo and str(from_id) == str(bo): is_owner = True if not gs_owner: await _remember_owner_id(bo) # Title bug root cause: `sender` is whoever actually typed THIS # message — for a business chat that's the OWNER half the time, so # relying on it (or on correctly configuring owner_user_id) let the # contact's name get overwritten with the owner's own name. `chat` # itself is far more reliable: for a private chat, Telegram always # fills chat.first_name/last_name with the identity of the OTHER # party in that conversation — the actual contact — regardless of # who sent any particular message. Prefer that; fall back to # `sender` only if it's ever missing, and never use it for a # message that came from the owner. if is_group: title = chat.get("title") else: chat_name = " ".join(p for p in (chat.get("first_name"), chat.get("last_name")) if p).strip() title = chat_name or (None if is_owner else sender) text = msg.get("text") or msg.get("caption") or "" media_type = None file_id = None if msg.get("photo"): media_type, file_id = "photo", msg["photo"][-1]["file_id"] elif msg.get("video"): media_type, file_id = "video", msg["video"]["file_id"] elif msg.get("voice"): media_type, file_id = "voice", msg["voice"]["file_id"] elif msg.get("animation"): media_type, file_id = "animation", msg["animation"]["file_id"] elif msg.get("sticker"): media_type = "sticker" file_id = msg["sticker"]["file_id"] if not text: text = msg["sticker"].get("emoji") or "🎭" elif msg.get("document"): media_type, file_id = "document", msg["document"]["file_id"] if not text and not media_type: return # nothing we can show/process (e.g. stickers, service messages) is_command = text.startswith("/") # Panel-account commands never touch history/index/push: /signup and # /resetpass carry a password and /setbot a bot token, and recording # them (as the first version of this did) would keep them in # plaintext in the chat history. Owner-only, admin-group-only, and # only for the owner's own tenant. The original message is deleted # from the group right after. if (is_command and current_tenant.get() == "owner" and ADMIN_GROUP_ID and is_configured_owner and chat_id == str(ADMIN_GROUP_ID) and text.split()[0].split("@")[0].lower() in ADMIN_USER_COMMANDS): cmd0 = text.split()[0].split("@")[0].lower() await handle_admin_command(chat_id, text, biz) if cmd0 in ("/signup", "/resetpass", "/setbot"): dres = await tg("deleteMessage", {"chat_id": chat_id, "message_id": msg.get("message_id")}) if not dres.get("ok"): await send_and_log(chat_id, "⚠️ نتونستم پیام دستور رو پاک کنم؛ رمز/توکن توی گروه مونده. ربات رو ادمینِ گروه با دسترسی «حذف پیام» کن و این پیام رو دستی پاک کن.", biz) return item = { "id": str(int(datetime.now().timestamp() * 1000)), "msg_id": msg.get("message_id"), "role": "owner" if is_owner else "user", "text": text, "sender": "شما" if is_owner else sender, "time": now_str(), "ts": now_ts(), } if media_type: item["type"] = media_type item["file_id"] = file_id if not text: item["text"] = { "photo": "[عکس]", "video": "[ویدیو]", "voice": "[پیام صوتی]", "document": "[فایل]", "animation": "[GIF]", "sticker": "🎭", }.get(media_type, "[رسانه]") reply_src = msg.get("reply_to_message") if reply_src: rtext = reply_src.get("text") or reply_src.get("caption") or "[رسانه]" rsender = (reply_src.get("from") or {}).get("first_name") or "پیام" item["reply_to_msg_id"] = reply_src.get("message_id") item["reply_to_preview"] = f"{rsender}: {rtext}"[:120] fwd_name = None origin = msg.get("forward_origin") if origin: otype = origin.get("type") if otype == "user": fwd_name = (origin.get("sender_user") or {}).get("first_name") elif otype == "hidden_user": fwd_name = origin.get("sender_user_name") elif otype == "chat": fwd_name = (origin.get("sender_chat") or {}).get("title") elif otype == "channel": fwd_name = (origin.get("chat") or {}).get("title") elif msg.get("forward_from"): fwd_name = msg["forward_from"].get("first_name") elif msg.get("forward_from_chat"): fwd_name = msg["forward_from_chat"].get("title") elif msg.get("forward_sender_name"): fwd_name = msg.get("forward_sender_name") if fwd_name: item["forwarded_from"] = fwd_name await append_history(chat_id, item) if media_type in ("sticker", "animation"): await _track_recent( RECENT_STICKERS_KEY if media_type == "sticker" else RECENT_GIFS_KEY, file_id, {"emoji": item.get("text", "")} if media_type == "sticker" else None, ) preview = item["text"] await update_index(chat_id, preview, item["sender"], title, biz, is_group=is_group, bot_direct=(biz is None)) track(send_push(item["sender"], preview)) if is_command: await handle_command(chat_id, msg, text, biz, sender) return if is_owner: return # your own message in a group/chat — not a query for the AI to answer ai_on, _, _, _ = await get_ai_status(chat_id) if ai_on: await schedule_ai_flush(chat_id, biz) except Exception as e: log.exception("handle_update error") await notify_admin_error("handle_update", f"پردازش پیام ورودی از چت `{chat_id}` شکست خورد.", e) if chat_id: try: await send_and_log(chat_id, "متاسفم، در انجام عملیات خطایی پیش اومد. لطفاً دوباره امتحان کن.", biz) except Exception: log.exception("failed to notify user about update error") # ----------------------------- incoming reaction updates ----------------------------- async def handle_message_reaction(update: dict): """Telegram sends this separately from `message` when someone changes their reaction. We only care about the contact's own reaction here — our own reaction (set via /api/react) is already recorded locally when we set it.""" try: mr = update.get("message_reaction") if not mr: return chat_id = str(mr["chat"]["id"]) message_id = mr.get("message_id") actor = mr.get("user") or {} actor_id = actor.get("id") bot_id = await get_bot_id() if actor_id and bot_id and actor_id == bot_id: return # our own reaction; already tracked when we set it new_reaction = mr.get("new_reaction") or [] emojis = [r.get("emoji") for r in new_reaction if r.get("type") == "emoji" and r.get("emoji")] value = emojis[0] if emojis else None async with chat_locks[chat_id]: history = await get_history(chat_id) for h in history: if h.get("msg_id") == message_id: h["their_reaction"] = value await kv.put(f"CHAT_{chat_id}", json.dumps(history, ensure_ascii=False), ttl=604800) break except Exception as e: log.exception("handle_message_reaction error") await notify_admin_error("message_reaction", "پردازش آپدیت ری‌اکشن شکست خورد.", e) async def convert_to_ogg_opus(content: bytes) -> bytes | None: """Browsers record voice as webm/opus; Telegram wants an .ogg/opus container. The codec is already opus, so this is a fast remux (-c:a copy), not a re-encode.""" import subprocess import tempfile def _run(): with tempfile.NamedTemporaryFile(suffix=".webm", delete=False) as fin: fin.write(content) in_path = fin.name out_path = in_path + ".ogg" try: r = subprocess.run( ["ffmpeg", "-y", "-i", in_path, "-c:a", "libopus", "-b:a", "64k", out_path], capture_output=True, timeout=30, ) if r.returncode != 0: log.error("ffmpeg failed: %s", r.stderr.decode(errors="ignore")[:500]) return None return Path(out_path).read_bytes() finally: for p in (in_path, out_path): try: os.remove(p) except OSError: pass try: return await asyncio.to_thread(_run) except Exception as e: log.error("convert_to_ogg_opus failed: %s", e) return None # ----------------------------- accounts / auth ----------------------------- # Multi-tenant login: username+password (created only via /signup in the # admin group, by the owner) instead of one shared WEB_PASSWORD for # everyone. The owner still logs in with WEB_PASSWORD (bootstrap — there # has to be a way in before any /signup has ever run), as a special # "owner" pseudo-account; everyone else is a real account in PANEL_USERS. # Session tokens (not passwords) are what the frontend actually carries # around afterward, same as the old PASS value was used everywhere. USERS_KEY = "PANEL_USERS" SESSIONS_KEY = "PANEL_SESSIONS" SESSION_MAX = 300 TEMP_PASSWORD_TTL_SECONDS = 900 # a recovery password is good for 15 minutes, once RESET_COOLDOWN_SECONDS = 60 # at most one recovery message per account per minute async def get_users() -> dict: raw = await kv.get(USERS_KEY) return json.loads(raw) if raw else {} async def save_users(users: dict) -> bool: return await kv.put(USERS_KEY, json.dumps(users, ensure_ascii=False)) def hash_password(password: str, salt: str | None = None) -> tuple[str, str]: salt = salt or secrets.token_hex(16) digest = hashlib.pbkdf2_hmac("sha256", password.encode(), salt.encode(), 100_000).hex() return digest, salt def verify_password(password: str, digest: str, salt: str) -> bool: check, _ = hash_password(password, salt) return secrets.compare_digest(check, digest) async def get_sessions() -> dict: raw = await kv.get(SESSIONS_KEY) return json.loads(raw) if raw else {} # "Last active" per session. The panel hits the API every few seconds, so this is NOT written per request: # a session is stamped at most once per SESSION_TOUCH_EVERY seconds (and once after each server start), in a # background task so the request never waits on storage. Resolution is therefore ~5 minutes — fine for a # devices list, and a few writes per hour per signed-in device. SESSION_TOUCH_EVERY = 300 _last_touch: dict = {} _bg_tasks: set = set() async def _touch_session(token: str): try: sessions = await get_sessions() s = sessions.get(token) if not s: # revoked meanwhile — never write a stale copy back return s["last_seen"] = now_ts() await kv.put(SESSIONS_KEY, json.dumps(sessions, ensure_ascii=False)) except Exception: log.exception("session touch failed") def _maybe_touch(token: str): now = time.time() if now - _last_touch.get(token, 0) < SESSION_TOUCH_EVERY: return if len(_last_touch) > 5000: _last_touch.clear() _last_touch[token] = now task = asyncio.get_running_loop().create_task(_touch_session(token)) _bg_tasks.add(task) task.add_done_callback(_bg_tasks.discard) def _device_from(b: dict) -> dict: """Optional label a client may send at login: {"deviceName": "Pixel 8", "platform": "android" | "web" | "pwa" | "windows", "appVersion": "1.0.0"}.""" return {"name": " ".join(str(b.get("deviceName") or "").split())[:60], "platform": str(b.get("platform") or "web")[:20], "app": str(b.get("appVersion") or "")[:20]} def _sid(token: str) -> str: """Public handle for a session — safe to show in a list, useless as a credential.""" return hashlib.sha256(token.encode()).hexdigest()[:16] async def create_session(username: str, must_change: bool = False, device: dict | None = None) -> str: sessions = await get_sessions() token = secrets.token_urlsafe(32) sessions[token] = {"username": username, "created": now_ts(), "must_change": must_change, "device": device or {}} if len(sessions) > SESSION_MAX: for old_token in sorted(sessions, key=lambda t: sessions[t].get("created", 0))[: len(sessions) - SESSION_MAX]: sessions.pop(old_token, None) await kv.put(SESSIONS_KEY, json.dumps(sessions, ensure_ascii=False)) return token async def revoke_sessions_for(username: str): sessions = await get_sessions() sessions = {t: s for t, s in sessions.items() if s.get("username") != username} await kv.put(SESSIONS_KEY, json.dumps(sessions, ensure_ascii=False)) await realtime.kick(username) # ----------------------------- API ----------------------------- def _token_from(request: Request) -> str: """Raw token (what the web panel sends) or the standard `Bearer `; `?auth=` for /