Spaces:
Running on Zero
Running on Zero
File size: 16,772 Bytes
1ccd9a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | function syncSendMic() {
const has = document.getElementById('msgIn').value.trim().length > 0;
document.getElementById('sendBtn').classList.toggle('d-none', !has);
document.getElementById('micBtn').classList.toggle('d-none', has);
}
async function sendMsg() {
const ta = document.getElementById('msgIn');
const txt = ta.value.trim();
if (!txt || !curChatId) return;
ta.value = ''; syncSendMic();
const selR = document.querySelector('input[name="sender"]:checked');
const senderMode = (selR && selR.value === 'me') ? 'me' : 'bot';
const body = { chatId: curChatId, text: txt, senderMode, businessConnectionId: curBizId };
if (replyTarget) body.replyToMsgId = replyTarget.msgId;
const res = await apiFetch('/api/send', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
cancelReply();
if (res?.error) showToast(res.error, 'error'); else { await fetchHistory(curChatId, false); scrollBot(true); }
}
function pickFile(acc, type) {
document.getElementById('attMenu').classList.remove('show');
const fi = document.getElementById('fileIn');
fi.accept = acc === '*' ? '' : acc;
fi.dataset.kind = type; fi.value = ''; fi.click();
}
async function onFilePick(e) {
const f = e.target.files[0];
if (!f || !curChatId) return;
await uploadMedia(f, e.target.dataset.kind, f.name);
}
async function uploadMedia(blob, type, name) {
const fd = new FormData();
fd.append('chatId', curChatId); fd.append('type', type); fd.append('file', blob, name);
const selR = document.querySelector('input[name="sender"]:checked');
const senderMode = (selR && selR.value === 'me') ? 'me' : 'bot';
fd.append('senderMode', senderMode);
if (curBizId) fd.append('businessConnectionId', curBizId);
if (replyTarget) fd.append('replyToMsgId', replyTarget.msgId);
const res = await apiFetch('/api/send-media', { method: 'POST', body: fd });
cancelReply();
if (res?.error) showToast(res.error, 'error'); else { await fetchHistory(curChatId, false); scrollBot(true); }
}
// ---- voice recording ----
// Real tweb layout: cancel(×) — pill(dot + timer) — send(✓), every control
// live and clickable throughout the recording — not just a "لغو" text link
// with no way to actually send, which is what this replaces.
function recTimerText() {
const mn = Math.floor(recSec / 60), sc = recSec % 60;
return mn + ':' + String(sc).padStart(2, '0');
}
async function toggleRec() {
if (mrec && mrec.state === 'recording') { stopRec(true); return; }
if (!navigator.mediaDevices || !window.MediaRecorder) { showToast('مرورگر از ضبط صدا پشتیبانی نمیکند', 'warn'); return; }
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
rchunks = [];
mrec = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
mrec.ondataavailable = e => { if (e.data.size > 0) rchunks.push(e.data); };
mrec.start();
document.getElementById('inpRow').style.display = 'none';
document.getElementById('voiceRecPanel').classList.add('show');
recSec = 0;
document.getElementById('voiceRecTimer').textContent = recTimerText();
recInt = setInterval(() => { recSec++; document.getElementById('voiceRecTimer').textContent = recTimerText(); }, 1000);
} catch (e) {
showToast('دسترسی به میکروفون رد شد', 'warn');
}
}
function cancelRec() { stopRec(false); }
function stopRec(send) {
if (!mrec) return;
const doSend = send;
mrec.onstop = () => {
clearInterval(recInt);
document.getElementById('voiceRecPanel').classList.remove('show');
document.getElementById('inpRow').style.display = 'flex';
if (doSend && rchunks.length) {
const blob = new Blob(rchunks, { type: 'audio/webm' });
uploadMedia(blob, 'voice', 'voice.webm');
}
mrec = null;
};
mrec.stream.getTracks().forEach(t => t.stop());
mrec.stop();
}
// ---- emoji picker ----
// Real tweb source: assets/emoji_test.js category groupings, condensed to
// the 8 top-level categories the dropdown itself uses.
const EMOJI_CATS = [
{ icon: "😊", name: "smileys", emojis: ["😀","😃","😄","😁","😆","😅","🤣","😂","🙂","🙃","😉","😊","😇","🥰","😍","🤩","😘","😗","☺","😚","😙","🥲","😋","😛","😜","🤪","😝","🤑","🤗","🤭","🤫","🤔","🤐","🤨","😐","😑","😶","😏","😒","🙄","😬","🤥","😌","😔","😪","🤤","😴","😷","🤒","🤕","🤢","🤮","🤧","🥵","🥶","🥴","😵","💫","🤯","🤠","🥳","🥸","😎","🤓","🧐","😕","😟","🙁","☹","😮","😯","😲","😳","🥺","😦","😧","😨","😰","😥","😢","😭","😱","😖","😣","😞","😓","😩","😫","🥱","😤","😡","😠","🤬","😈","👿","💀","☠","💩","🤡","👹","👺","👻","👽","👾","🤖","🫨","😵💫","🫠","🫡"] },
{ icon: "👋", name: "hands", emojis: ["👋","🤚","🖐","✋","🖖","👌","🤌","🤏","✌","🤞","🤟","🤘","🤙","👈","👉","👆","🖕","👇","☝","👍","👎","✊","👊","🤛","🤜","👏","🙌","👐","🤲","🤝","🙏","✍","💅","🤳","💪","🦾","🦵","🦿","🦶","👂","🦻","👃","🫀","🫁","🧠","🦷","🦴","👀","👁","👅","👄","🫦"] },
{ icon: "🐶", name: "animals", emojis: ["🐶","🐱","🐭","🐹","🐰","🦊","🐻","🐼","🐻❄️","🐨","🐯","🦁","🐮","🐷","🐽","🐸","🐵","🙈","🙉","🙊","🐒","🐔","🐧","🐦","🐤","🦆","🦅","🦉","🦇","🐺","🐗","🐴","🦄","🐝","🪱","🐛","🦋","🐌","🐞","🐜","🪲","🦟","🦗","🪳","🕷","🦂","🐢","🐍","🦎","🦖","🦕","🐙","🦑","🦐","🦞","🦀","🐡","🐠","🐟","🐬","🐳","🐋","🦈","🦭","🐊","🐅","🐆","🦓","🦍","🦧","🦣","🐘","🦛","🦏","🐪","🐫","🦒","🦘","🦬","🐃","🐂","🐄","🐎","🐖","🐏","🐑","🦙","🐐","🦌","🐕","🐩","🦮","🐕🦺","🐈","🐈⬛","🪶","🐓","🦃","🦤","🦚","🦜","🦢","🦩","🕊","🐇","🦝","🦨","🦡","🦫","🦦","🦥","🐁","🐀","🐿","🦔"] },
{ icon: "🍕", name: "food", emojis: ["🍏","🍎","🍐","🍊","🍋","🍌","🍉","🍇","🍓","🫐","🍈","🍒","🍑","🥭","🍍","🥥","🥝","🍅","🍆","🥑","🥦","🥬","🥒","🌶","🫑","🧄","🧅","🥔","🌽","🥕","🫛","🧇","🥞","🧈","🍳","🥚","🧆","🥙","🧀","🍖","🍗","🥩","🥓","🌮","🌯","🫔","🧆","🥫","🍱","🍘","🍙","🍚","🍛","🍜","🍝","🍠","🍢","🍣","🍤","🍥","🥮","🍡","🥟","🥠","🥡","🦀","🦞","🦐","🦑","🦪","🍦","🍧","🍨","🍩","🍪","🎂","🍰","🧁","🥧","🍫","🍬","🍭","🍮","🍯","🍼","🥛","☕","🫖","🍵","🧃","🥤","🧋","🍶","🍺","🍻","🥂","🍷","🥃","🍸","🍹","🧉","🍾","🍴","🥄"] },
{ icon: "🎮", name: "activities", emojis: ["⚽","🏀","🏈","⚾","🥎","🎾","🏐","🏉","🥏","🎱","🪀","🏓","🏸","🏒","🏑","🥍","🏏","🪃","🥅","⛳","🪁","🏹","🎣","🤿","🥊","🥋","🎽","🛹","🛼","🛷","⛸","🥌","🎿","⛷","🏂","🪂","🏋","🤼","🤸","🤺","🏇","⛹","🤾","🏌","🏄","🚣","🧗","🚵","🚴","🏆","🥇","🥈","🥉","🏅","🎖","🏵","🎗","🎫","🎟","🎪","🤹","🎭","🩰","🎨","🎬","🎤","🎧","🎼","🎹","🥁","🪘","🎷","🎺","🎸","🪕","🎻","🎲","♟","🎯","🎳","🎮","🎰","🧩"] },
{ icon: "🚗", name: "travel", emojis: ["🚗","🚕","🚙","🚌","🚎","🏎","🚓","🚑","🚒","🚐","🛻","🚚","🚛","🚜","🏍","🛵","🛺","🚲","🛴","🛹","🚏","🛣","🛤","⛽","🚦","🚥","🚧","⚓","🛟","⛵","🚤","🛥","🛳","⛴","🚢","✈","🛩","🛫","🛬","🪂","💺","🚁","🚟","🚠","🚡","🛰","🚀","🛸","🪐","💫","⭐","🌟","✨","⚡","☄","🌈","☀","🌤","⛅","🌦","🌧","⛈","🌩","🌨","❄","☃","⛄","🌬","💨","💧","💦","☔","☂","🌊","🌫"] },
{ icon: "💡", name: "objects", emojis: ["⌚","📱","📲","💻","⌨","🖥","🖨","🖱","🖲","🕹","🗜","💽","💾","💿","📀","📼","📷","📸","📹","🎥","📽","🎞","📞","☎","📟","📠","📺","📻","🧭","⏱","⏲","⏰","🕰","⌛","⏳","📡","🔋","🔌","💡","🔦","🕯","🪔","🧯","🛢","💰","💵","💴","💶","💷","💸","💳","🪙","💎","⚖","🪜","🧰","🪛","🔧","🔨","⚒","🛠","⛏","🪚","🔩","🪤","🧲","🔮","🪄","💈","🛒","🎁","🎀","🪅","🎊","🎉","🎎","🎏","🎐","🎑","🎃","🎄","🎆","🎇","🧧","🎈","🎋","🧨","✉","📩","📨","📧","📥","📤","📦","🏷","📪","📫","📬","📭","📮","📯","📜","📃","📄","📑","🧾","📊","📈","📉","🗒","🗓","📆","📅","🗑","📁","📂","🗂","🗃","🗄","🗑"] },
{ icon: "#️⃣", name: "symbols", emojis: ["❤","🧡","💛","💚","💙","💜","🖤","🤍","🤎","💔","❣","💕","💞","💓","💗","💖","💘","💝","💟","☮","✝","☪","🕉","☸","✡","🔯","🕎","☯","☦","🛐","⛎","♈","♉","♊","♋","♌","♍","♎","♏","♐","♑","♒","♓","🆔","⚛","🉑","☢","☣","📴","📳","🈶","🈚","🈸","🈺","🈷","✴","🆚","💮","🉐","㊙","㊗","🈴","🈵","🈹","🈲","🅰","🅱","🆎","🆑","🅾","🆘","❌","⭕","🛑","⛔","📛","🚫","💯","💢","♨","🚷","🚯","🚳","🚱","🔞","📵","🚭","❗","❕","❓","❔","‼","⁉","🔅","🔆","〽","⚠","🚸","🔱","⚜","🔰","♻","✅","🈯","💹","❎","🌐","💠","Ⓜ","🌀","💤","🏧","🚾","♿","🅿","🛗","🈳","🈹","🈵","🉐","🈴","🉑","🈶","🈚","🎦","📶","🈸","🏬","🔀","🔁","🔂","▶","⏩","⏭","⏯","◀","⏪","⏮","🔼","⏫","🔽","⏬","⏸","⏹","⏺","⏏","🎵","🎶","➕","➖","➗","✖","♾","💲","💱","™","©","®","〰","➰","➿","🔚","🔙","🔛","🔝","🔜","✔","☑","🔲","🔳","⬛","⬜","◼","◻","◾","◽","▪","▫","🔶","🔷","🔸","🔹","🔺","🔻","💠","🔘","🔵","🟤","⚫","🟣","🟢","🟡","🟠","🔴"] }
];
// ---- most-recently/most-used emoji tracking (localStorage) ----
// A "🕐 اخیر" category is prepended, sorted by click count descending —
// the emoji you actually use rise to the top over time.
const EMOJI_FREQ_KEY = 'tg_emoji_freq';
function getEmojiFreq() {
try { return JSON.parse(localStorage.getItem(EMOJI_FREQ_KEY) || '{}'); } catch (e) { return {}; }
}
function bumpEmojiFreq(em) {
const f = getEmojiFreq();
f[em] = (f[em] || 0) + 1;
localStorage.setItem(EMOJI_FREQ_KEY, JSON.stringify(f));
}
function recentEmojiList() {
const f = getEmojiFreq();
return Object.keys(f).sort((a, b) => f[b] - f[a]).slice(0, 40);
}
function buildEmojiPicker() {
const cats = document.getElementById('emojiCats');
cats.innerHTML = '';
const recent = recentEmojiList();
const allCats = recent.length ? [{ icon: '🕐', name: 'recent', emojis: recent }, ...EMOJI_CATS] : EMOJI_CATS;
allCats.forEach((c, i) => {
const span = document.createElement('span');
span.className = 'ec' + (i === 0 ? ' active' : '');
span.textContent = c.icon;
span.onclick = () => switchEmojiCat(i, allCats);
cats.appendChild(span);
});
applyEmojiImages(cats);
renderEmojiCat(0, allCats);
}
function switchEmojiCat(i, cats) {
curEmojiCat = i;
document.querySelectorAll('.ec').forEach((e, j) => e.classList.toggle('active', j === i));
renderEmojiCat(i, cats || [{ emojis: recentEmojiList() }, ...EMOJI_CATS]);
}
function renderEmojiCat(i, cats) {
const list = (cats || EMOJI_CATS)[i];
const grid = document.getElementById('emojiGrid');
grid.innerHTML = '';
(list.emojis || []).forEach(em => {
const s = document.createElement('span');
s.textContent = em;
s.onclick = () => {
bumpEmojiFreq(em);
const ta = document.getElementById('msgIn');
ta.value += em; ta.dispatchEvent(new Event('input'));
};
grid.appendChild(s);
});
applyEmojiImages(grid);
}
function toggleEmojiPicker() {
emojiOpen = !emojiOpen;
if (emojiOpen) buildEmojiPicker(); // ~150 emoji: built on open (also refreshes the "recent" tab), not at page load
document.getElementById('emojiPicker').classList.toggle('show', emojiOpen);
if (emojiOpen) { document.getElementById('stickerPicker').classList.remove('show'); stickerOpen = false; }
}
// ---- sticker / GIF panel ----
// The Bot API has no "search/browse all stickers" call — only a known
// pack's getStickerSet, plus whatever the admin has actually sent before
// (tracked server-side as "recent"). Clicking a tile resends it by
// file_id (no re-upload needed).
var stickerOpen = false;
var stickerTab = 'sticker';
function toggleStickerPicker() {
stickerOpen = !stickerOpen;
document.getElementById('stickerPicker').classList.toggle('show', stickerOpen);
if (stickerOpen) {
document.getElementById('emojiPicker').classList.remove('show'); emojiOpen = false;
loadStickerPanel();
}
}
function switchStickerTab(tab) {
stickerTab = tab;
document.querySelectorAll('.sk-tab').forEach(t => t.classList.toggle('active', t.dataset.tab === tab));
loadStickerPanel();
}
async function loadStickerPanel() {
const grid = document.getElementById('stickerGrid');
grid.innerHTML = '<div class="sk-empty">در حال بارگذاری...</div>';
const items = [];
if (stickerTab === 'sticker') {
const recent = await apiFetch('/api/recent-media?type=sticker');
(recent || []).forEach(s => items.push({ file_id: s.file_id, emoji: s.emoji || '🎭', kind: 'sticker' }));
const gs = await apiFetch('/api/settings');
const packs = (gs?.sticker_packs || '').split(',').map(s => s.trim()).filter(Boolean);
for (const packName of packs) {
const set = await apiFetch('/api/sticker-set?name=' + encodeURIComponent(packName));
if (set && !set.error) (set.stickers || []).forEach(s => items.push({ file_id: s.file_id, emoji: s.emoji || '🎭', kind: 'sticker' }));
}
} else {
const recent = await apiFetch('/api/recent-media?type=animation');
(recent || []).forEach(g => items.push({ file_id: g.file_id, kind: 'animation' }));
}
grid.innerHTML = '';
if (!items.length) {
grid.innerHTML = '<div class="sk-empty">' + (stickerTab === 'sticker'
? 'موردی نیست — یا یه استیکر از منوی پیوست بفرست تا اینجا ذخیره بشه، یا توی تنظیمات یه پک استیکر معرفی کن.'
: 'موردی نیست — یه GIF از منوی پیوست بفرست تا اینجا ذخیره بشه.') + '</div>';
return;
}
items.forEach(it => {
const tile = document.createElement('div'); tile.className = 'sk-tile';
if (it.kind === 'sticker') {
// same byte-sniffing renderer the chat bubbles use — this is
// what actually makes animated (.tgs) stickers animate in the
// picker instead of just showing their assigned emoji.
renderSticker(tile, it.file_id, it.emoji);
} else {
const v = document.createElement('video'); v.src = murl(it.file_id);
v.autoplay = true; v.loop = true; v.muted = true; v.playsInline = true;
tile.appendChild(v);
}
tile.onclick = () => sendStickerFromPanel(it.file_id, it.kind, it.emoji);
grid.appendChild(tile);
});
}
async function sendStickerFromPanel(fileId, kind, emoji) {
if (!curChatId) return;
const selR = document.querySelector('input[name="sender"]:checked');
const senderMode = (selR && selR.value === 'me') ? 'me' : 'bot';
const body = { chatId: curChatId, fileId, type: kind, senderMode, businessConnectionId: curBizId, emoji };
if (replyTarget) body.replyToMsgId = replyTarget.msgId;
const res = await apiFetch('/api/send-sticker', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
cancelReply();
if (res?.error) showToast(res.error, 'error'); else { await fetchHistory(curChatId, false); scrollBot(true); }
}
|