fix: импорт bot в perms (права не проверялись) + аватарки инстанса и чатов
1 files added
6 files modified
| | |
| | | |
| | | # Имя бота без @ — нужно для Telegram Login Widget. Заполняется на старте из get_me(). |
| | | BOT_USERNAME: str = "" |
| | | # ID бота — для получения аватарки инстанса в панели. |
| | | BOT_ID: int = 0 |
| | | |
| | | # Запрет на отправку контента: используется для капчи и мутов. |
| | | NO_PERMS = ChatPermissions( |
| | |
| | | |
| | | |
| | | async def load_bot_username() -> None: |
| | | # Имя бота нужно для отрисовки Login Widget; кэшируется на время работы. |
| | | global BOT_USERNAME |
| | | # Имя и ID бота нужны для Login Widget и аватарки инстанса; кэшируются. |
| | | global BOT_USERNAME, BOT_ID |
| | | try: |
| | | me = await bot.get_me() |
| | | BOT_USERNAME = me.username or "" |
| | | BOT_ID = me.id |
| | | except Exception as e: |
| | | logger.error("get_me failed: %s", e) |
| | |
| | | from aiogram import Bot |
| | | |
| | | import db |
| | | from cfg import OWNER_ID, logger |
| | | from cfg import OWNER_ID, bot, logger |
| | | from util import refuse_phrase |
| | | |
| | | # Ключи прав. Владелец бота и создатель чата имеют все права без исключений. |
| | |
| | | from aiohttp import web |
| | | |
| | | from cfg import WEB_HOST, WEB_PORT, logger |
| | | from web import auth, views |
| | | from web import auth, avatar, views |
| | | |
| | | BASE = pathlib.Path(__file__).resolve().parent |
| | | |
| | |
| | | app.router.add_post("/action", views.action) |
| | | app.router.add_get("/captcha", views.captcha_page) |
| | | app.router.add_get("/log", views.log_page) |
| | | app.router.add_get("/avatar/{chat_id}", avatar.avatar_handler) |
| | | app.router.add_static("/static", BASE / "static") |
| | | return app |
| | | |
| New file |
| | |
| | | import io |
| | | from typing import Dict, Optional |
| | | |
| | | from aiohttp import web |
| | | |
| | | from cfg import BOT_ID, bot, logger |
| | | |
| | | # Кэш аватарок в памяти: chat_id -> байты изображения. |
| | | _photo: Dict[int, bytes] = {} |
| | | # Кэш идентификаторов файлов: chat_id -> file_id (или None, если аватарки нет). |
| | | _fid: Dict[int, Optional[str]] = {} |
| | | |
| | | |
| | | async def _file_id(chat_id: int) -> Optional[str]: |
| | | if chat_id in _fid: |
| | | return _fid[chat_id] |
| | | fid: Optional[str] = None |
| | | try: |
| | | chat = await bot.get_chat(chat_id) |
| | | photo = getattr(chat, "photo", None) |
| | | if photo is not None: |
| | | fid = photo.big_file_id or photo.small_file_id |
| | | except Exception as e: |
| | | logger.warning("avatar get_chat %s: %s", chat_id, e) |
| | | _fid[chat_id] = fid |
| | | return fid |
| | | |
| | | |
| | | async def get_avatar(chat_id: int) -> Optional[bytes]: |
| | | if chat_id in _photo: |
| | | return _photo[chat_id] |
| | | fid = await _file_id(chat_id) |
| | | if not fid: |
| | | return None |
| | | try: |
| | | file = await bot.get_file(fid) |
| | | buf = io.BytesIO() |
| | | await bot.download_file(file.file_path, buf) |
| | | data = buf.getvalue() |
| | | except Exception as e: |
| | | logger.warning("avatar download %s: %s", chat_id, e) |
| | | return None |
| | | _photo[chat_id] = data |
| | | return data |
| | | |
| | | |
| | | async def avatar_handler(request) -> web.Response: |
| | | raw = request.match_info["chat_id"] |
| | | chat_id = BOT_ID if raw == "bot" else int(raw) |
| | | if not chat_id: |
| | | raise web.HTTPNotFound() |
| | | data = await get_avatar(chat_id) |
| | | if not data: |
| | | raise web.HTTPNotFound() |
| | | return web.Response( |
| | | body=data, |
| | | content_type="image/jpeg", |
| | | headers={"Cache-Control": "public, max-age=3600"}, |
| | | ) |
| | |
| | | <div class="flex min-h-screen"> |
| | | <aside class="hidden md:flex w-64 shrink-0 flex-col border-r border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900"> |
| | | <div class="h-16 flex items-center gap-2 px-5 border-b border-gray-200 dark:border-gray-800"> |
| | | <span class="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-brand-600 text-white font-bold">A</span> |
| | | <span class="relative inline-flex h-8 w-8 items-center justify-center overflow-hidden rounded-lg bg-brand-600 text-white font-bold"> |
| | | A |
| | | <img src="/avatar/bot" alt="" class="absolute inset-0 h-full w-full object-cover" onerror="this.remove()"> |
| | | </span> |
| | | <span class="text-lg font-semibold">Amadeus</span> |
| | | </div> |
| | | <nav class="flex-1 p-3 space-y-1 text-sm"> |
| | |
| | | {% block header %}{{ chat.title or chat.chat_id }}{% endblock %} |
| | | {% block content %} |
| | | |
| | | <div class="text-sm text-gray-500">id {{ chat.chat_id }} · {{ chat.type }}</div> |
| | | <div class="flex items-center gap-3"> |
| | | <img src="/avatar/{{ chat.chat_id }}" alt="" class="h-10 w-10 rounded-full bg-gray-200 object-cover dark:bg-gray-700" onerror="this.style.visibility='hidden'"> |
| | | <div class="text-sm text-gray-500">id {{ chat.chat_id }} · {{ chat.type }}</div> |
| | | </div> |
| | | |
| | | <section class="rounded-xl border border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900 p-5 shadow-sm"> |
| | | <h2 class="font-semibold mb-4">Настройки</h2> |
| | |
| | | {% set cfg = cfgs.get(c.chat_id) %} |
| | | <tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50"> |
| | | <td class="px-5 py-3"> |
| | | <div class="font-medium">{{ c.title or c.chat_id }}</div> |
| | | <div class="text-xs text-gray-500">id {{ c.chat_id }} · {{ c.type }}</div> |
| | | <div class="flex items-center gap-3"> |
| | | <img src="/avatar/{{ c.chat_id }}" alt="" class="h-8 w-8 rounded-full bg-gray-200 object-cover dark:bg-gray-700" onerror="this.style.visibility='hidden'"> |
| | | <div> |
| | | <div class="font-medium">{{ c.title or c.chat_id }}</div> |
| | | <div class="text-xs text-gray-500">id {{ c.chat_id }} · {{ c.type }}</div> |
| | | </div> |
| | | </div> |
| | | </td> |
| | | <td class="px-5 py-3 text-gray-500">{{ cfg.btn_count }} кнопки · {{ cfg.timeout }} с · {{ cfg.max_attempts }} попытки</td> |
| | | <td class="px-5 py-3 text-gray-500">{{ cfg.warn_limit }} · {{ "бан" if cfg.warn_action == "ban" else "мут" }}</td> |