Бот для капчи в чатах
Roman Sergienko
12 hours ago 9318fc842964d6de64b778cd5c617c6859ca56a2
web/templates/messages.html
@@ -11,17 +11,31 @@
</div>
<section class="overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm dark:border-gray-800 dark:bg-gray-900">
    <div id="msg-list" class="flex max-h-[55vh] flex-col gap-1 overflow-y-auto p-2 md:max-h-[60vh]">
    <div id="msg-list" class="flex max-h-[58vh] flex-col gap-2 overflow-y-auto p-3 md:max-h-[62vh]">
        {% for m in msgs %}
        <div class="msg rounded-lg px-3 py-2 text-sm" data-id="{{ m.id }}" data-tg="{{ m.tg_msg_id or 0 }}" data-name="{{ m.name }}">
            <div class="flex flex-wrap items-center justify-between gap-2">
                <span class="font-medium {% if m.name and m.name.startswith('Amadeus') %}text-brand-600 dark:text-brand-400{% endif %}">{{ m.name }}</span>
                <span class="flex items-center gap-2">
                    <span class="text-xs text-gray-400">{{ fmt_dt(m.created_at) }}</span>
                    <button type="button" class="reply-btn text-xs text-gray-400 hover:text-brand-500">ответить</button>
                </span>
        {% set is_bot = m.name and m.name.startswith('Amadeus') %}
        <div class="msg flex {{ 'justify-end' if is_bot else 'justify-start' }}"
             data-id="{{ m.id }}" data-tg="{{ m.tg_msg_id or 0 }}" data-name="{{ m.name }}">
            <div class="max-w-[82%] rounded-2xl px-3 py-2 text-sm shadow-sm
                {% if is_bot %}bg-brand-600 text-white{% else %}bg-gray-100 text-gray-900 dark:bg-gray-800 dark:text-gray-100{% endif %}">
                <div class="mb-0.5 text-xs font-semibold {% if is_bot %}text-white/80{% else %}text-brand-600 dark:text-brand-400{% endif %}">{{ m.name }}</div>
                {% if m.reply_to_tg %}
                <div class="mb-1 rounded-lg border-l-2 px-2 py-1 text-[11px]
                    {% if is_bot %}border-white/50 bg-white/10 text-white/80{% else %}border-brand-400 bg-black/5 text-gray-600 dark:bg-white/5 dark:text-gray-300{% endif %}">
                    <div class="font-medium">↪ {{ m.reply_to_name or "сообщение" }}</div>
                    {% if m.reply_to_text %}<div class="truncate">{{ m.reply_to_text }}</div>{% endif %}
                </div>
                {% endif %}
                {% if m.media_type %}
                <img src="/media/{{ m.id }}" alt="{{ m.media_type }}" loading="lazy"
                     class="mb-1 max-h-60 rounded-lg" onerror="this.style.display='none'">
                {% endif %}
                {% if m.text %}<div class="whitespace-pre-wrap break-words">{{ m.text }}</div>{% endif %}
                <div class="mt-1 flex items-center justify-end gap-2 text-[10px] {% if is_bot %}text-white/70{% else %}text-gray-400{% endif %}">
                    <span>{{ fmt_dt(m.created_at) }}</span>
                    <button type="button" class="reply-btn hover:underline">ответить</button>
                </div>
            </div>
            <div class="mt-0.5 whitespace-pre-wrap break-words">{{ m.text }}</div>
        </div>
        {% else %}
        <div id="empty" class="px-4 py-8 text-center text-gray-500">Сообщений пока нет.</div>
@@ -63,12 +77,12 @@
    function atBottom() { return list.scrollTop + list.clientHeight >= list.scrollHeight - 40; }
    function toBottom() { list.scrollTop = list.scrollHeight; }
    function bindReply(el) {
        const btn = el.querySelector(".reply-btn");
    function bindReply(wrap) {
        const btn = wrap.querySelector(".reply-btn");
        if (!btn) return;
        btn.addEventListener("click", function () {
            replyTo.value = el.dataset.tg || 0;
            replyName.textContent = el.dataset.name || "";
            replyTo.value = wrap.dataset.tg || 0;
            replyName.textContent = wrap.dataset.name || "";
            replyBar.classList.remove("hidden");
            replyBar.classList.add("flex");
            text.focus();
@@ -82,23 +96,42 @@
        replyBar.classList.remove("flex");
    });
    function bubble(m) {
        const isBot = (m.name || "").startsWith("Amadeus");
        const wrap = document.createElement("div");
        wrap.className = "msg flex " + (isBot ? "justify-end" : "justify-start");
        wrap.dataset.id = m.id;
        wrap.dataset.tg = m.tg_msg_id || 0;
        wrap.dataset.name = m.name || "";
        const box = document.createElement("div");
        box.className = "max-w-[82%] rounded-2xl px-3 py-2 text-sm shadow-sm " +
            (isBot ? "bg-brand-600 text-white" : "bg-gray-100 text-gray-900 dark:bg-gray-800 dark:text-gray-100");
        let html = '<div class="mb-0.5 text-xs font-semibold ' +
            (isBot ? "text-white/80" : "text-brand-600 dark:text-brand-400") + '">' + esc(m.name) + "</div>";
        if (m.reply_to_tg) {
            html += '<div class="mb-1 rounded-lg border-l-2 px-2 py-1 text-[11px] ' +
                (isBot ? "border-white/50 bg-white/10 text-white/80" : "border-brand-400 bg-black/5 text-gray-600 dark:bg-white/5 dark:text-gray-300") + '">' +
                '<div class="font-medium">↪ ' + esc(m.reply_to_name || "сообщение") + "</div>" +
                (m.reply_to_text ? '<div class="truncate">' + esc(m.reply_to_text) + "</div>" : "") + "</div>";
        }
        if (m.media_type && m.id) {
            html += '<img src="/media/' + m.id + '" class="mb-1 max-h-60 rounded-lg" loading="lazy" onerror="this.style.display=\'none\'">';
        }
        if (m.text) html += '<div class="whitespace-pre-wrap break-words">' + esc(m.text) + "</div>";
        html += '<div class="mt-1 flex items-center justify-end gap-2 text-[10px] ' +
            (isBot ? "text-white/70" : "text-gray-400") + '"><span>' + esc(m.time || "") +
            '</span><button type="button" class="reply-btn hover:underline">ответить</button></div>';
        box.innerHTML = html;
        wrap.appendChild(box);
        return wrap;
    }
    function addMsg(m, scroll) {
        const empty = document.getElementById("empty");
        if (empty) empty.remove();
        const el = document.createElement("div");
        el.className = "msg rounded-lg px-3 py-2 text-sm";
        el.dataset.id = m.id;
        el.dataset.tg = m.tg_msg_id || 0;
        el.dataset.name = m.name || "";
        const isBot = (m.name || "").startsWith("Amadeus");
        el.innerHTML =
            '<div class="flex flex-wrap items-center justify-between gap-2">' +
            '<span class="font-medium ' + (isBot ? "text-brand-600 dark:text-brand-400" : "") + '">' + esc(m.name) + "</span>" +
            '<span class="flex items-center gap-2"><span class="text-xs text-gray-400">' + esc(m.time || "") + "</span>" +
            '<button type="button" class="reply-btn text-xs text-gray-400 hover:text-brand-500">ответить</button></span></div>' +
            '<div class="mt-0.5 whitespace-pre-wrap break-words">' + esc(m.text) + "</div>";
        list.appendChild(el);
        bindReply(el);
        const wrap = bubble(m);
        list.appendChild(wrap);
        bindReply(wrap);
        if (scroll) toBottom();
    }
@@ -139,7 +172,6 @@
        }
    });
    // Реалтайм: подтягиваем новые сообщения.
    async function poll() {
        try {
            const r = await fetch("/api/messages?chat_id=" + encodeURIComponent(chatId) + "&after=" + lastId);
@@ -149,7 +181,7 @@
                j.messages.forEach(function (m) { if (m.id > lastId) { addMsg(m, false); lastId = m.id; } });
                if (stick) toBottom();
            }
        } catch (err) { /* тихо игнорируем */ }
        } catch (err) { /* тихо */ }
    }
    toBottom();