Бот для капчи в чатах
track.py
@@ -7,13 +7,28 @@
@dp.message.middleware()
async def track_msg(handler, event: types.Message, data: dict):
    # Фиксируем чаты, где присутствует бот, для веб-панели.
    # Фиксируем чаты и пользователей, чтобы панель и /aperm могли их находить.
    chat = event.chat
    ts = now_ts()
    if chat and chat.type in ("group", "supergroup", "channel"):
        try:
            await db.upsert_chat(chat.id, chat.title, chat.type, now_ts())
            await db.upsert_chat(chat.id, chat.title, chat.type, ts)
        except Exception as e:
            logger.warning("upsert_chat failed chat=%s: %s", chat.id, e)
    u = event.from_user
    if u is not None and not u.is_bot:
        try:
            await db.upsert_user(u.id, u.username, u.first_name, u.last_name, ts)
        except Exception as e:
            logger.warning("upsert_user failed user=%s: %s", u.id, e)
    # Лог последних сообщений для панели.
    if chat and chat.type in ("group", "supergroup") and u is not None and not u.is_bot:
        content = event.text or event.caption or ""
        if content:
            try:
                await db.add_msg(chat.id, u.id, u.full_name, content[:1000], ts)
            except Exception as e:
                logger.warning("add_msg failed chat=%s: %s", chat.id, e)
    return await handler(event, data)