Fixed formatting of photos and stuff
This commit is contained in:
parent
1e55b07892
commit
d739b7edcb
52
index.js
52
index.js
@ -45,10 +45,18 @@ function formatBatch(queue) {
|
||||
const parts = [];
|
||||
for (const [group, msgs] of Object.entries(groups)) {
|
||||
parts.push(`[👥 ${group} 👥]`);
|
||||
let prevSender = '';
|
||||
for (const m of msgs) {
|
||||
if (m.showSender === false) {
|
||||
parts.push('');
|
||||
parts.push(m.text);
|
||||
} else {
|
||||
if (prevSender && prevSender !== m.sender) parts.push('');
|
||||
prevSender = m.sender;
|
||||
parts.push(`${m.sender}: ${m.text}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return parts.join('\n');
|
||||
}
|
||||
|
||||
@ -94,13 +102,15 @@ async function flushQueue() {
|
||||
}
|
||||
}
|
||||
|
||||
function enqueue(group, sender, text) {
|
||||
function enqueue(group, sender, text, showSender = true) {
|
||||
msgCounter++;
|
||||
messageQueue.push({ group, sender, text });
|
||||
messageQueue.push({ group, sender, text, showSender });
|
||||
|
||||
scheduleFlush();
|
||||
log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`);
|
||||
|
||||
console.log(formatBatch(messageQueue))
|
||||
|
||||
if (queueSize() >= config.batch.maxChars) {
|
||||
clearTimeout(flushTimer);
|
||||
flushTimer = null;
|
||||
@ -252,22 +262,48 @@ async function startClient() {
|
||||
? config.ownName
|
||||
: (contact.name || contact.pushname || contact.shortName || contact.number || 'Unknown');
|
||||
|
||||
const mediaLabel = { image: '📷 Image', video: '🎥 Video', ptt: '🎤 Voice', audio: '🎵 Audio', document: '📄 Document', sticker: '🖼️ Sticker' }[message.type];
|
||||
const typeLabel = { image: '📷 Image', video: '🎥 Video', ptt: '🎤 Voice', audio: '🎵 Audio', document: '📄 Document', sticker: '🖼️ Sticker' }[message.type];
|
||||
let body = message.body || '';
|
||||
if (message.hasMedia && config.appsScriptUrl) {
|
||||
let isReply = false;
|
||||
|
||||
if (message.hasQuotedMsg) {
|
||||
try {
|
||||
const quoted = await message.getQuotedMessage();
|
||||
if (quoted) {
|
||||
const qContact = await quoted.getContact();
|
||||
const qName = qContact.name || qContact.pushname || qContact.shortName || 'Unknown';
|
||||
let qText = quoted.body || ({ image: '📷 Image', video: '🎥 Video', ptt: '🎤 Voice', audio: '🎵 Audio', document: '📄 Document', sticker: '🖼️ Sticker' })[quoted.type] || '[media]';
|
||||
if (qText.length > 50) qText = qText.slice(0, 50) + '...';
|
||||
body = `↩️ ${qName} אמר: ${qText} ↩️\n${sender}: ${body}`;
|
||||
isReply = true;
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
if (message.isForwarded) {
|
||||
let fwdFrom = '';
|
||||
if (message.author) {
|
||||
try {
|
||||
const fwdContact = await client.getContactById(message.author);
|
||||
fwdFrom = fwdContact.name || fwdContact.pushname || fwdContact.shortName || '';
|
||||
} catch (_) {}
|
||||
}
|
||||
body = `⏩ הועבר${fwdFrom ? ' מ' + fwdFrom : ''}⏩\n` + body;
|
||||
}
|
||||
if (message.hasMedia && config.appsScriptUrl && message.type !== 'sticker') {
|
||||
try {
|
||||
const media = await message.downloadMedia();
|
||||
if (media) {
|
||||
const { uploadMedia } = require('./uploader');
|
||||
const ext = media.mimeType ? media.mimeType.split('/')[1] || 'bin' : 'bin';
|
||||
const ext = media.mimeType ? media.mimeType.split('/')[1] || '' : '';
|
||||
const link = await uploadMedia(media.data, media.mimeType, ext, media.filename);
|
||||
if (link) body = (body ? `${body}\n📎 ${link}` : `📎 ${link}`);
|
||||
if (link) body = (body ? `${body}\n[${typeLabel}]\n[${link}]` : `[${typeLabel}]\n[${link}]`);
|
||||
}
|
||||
} catch (err) {
|
||||
log('ERROR', `Media upload: ${err.message}`);
|
||||
}
|
||||
}
|
||||
if (!body && mediaLabel) body = mediaLabel;
|
||||
if (!body && typeLabel) body = typeLabel;
|
||||
if (!body) return;
|
||||
|
||||
if (message.mentionedIds && message.mentionedIds.length > 0) {
|
||||
@ -282,7 +318,7 @@ async function startClient() {
|
||||
}
|
||||
}
|
||||
|
||||
enqueue(chat.name, sender, body);
|
||||
enqueue(chat.name, sender, body, !isReply && !message.isForwarded);
|
||||
} catch (err) {
|
||||
log('ERROR', `Message handler: ${err.message}`);
|
||||
}
|
||||
|
||||
18
uploader.js
18
uploader.js
@ -5,21 +5,11 @@ function pad(n) {
|
||||
}
|
||||
|
||||
function buildFileName(originalName, ext) {
|
||||
if (originalName) return originalName;
|
||||
const now = new Date();
|
||||
|
||||
// Format: YYYY-MM-DD
|
||||
const dateStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
||||
|
||||
// Format: HH-MM-SS (using dashes since colons can be problematic in filenames)
|
||||
const timeStr = `${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}`;
|
||||
|
||||
if (originalName) {
|
||||
// Option A: original name
|
||||
return originalName;
|
||||
} else {
|
||||
// Option B: Fallback if no name is provided
|
||||
return `${dateStr}_${timeStr}.${ext || 'bin'}`;
|
||||
}
|
||||
const base = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_` +
|
||||
`${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
||||
return ext ? `${base}.${ext}` : base;
|
||||
}
|
||||
|
||||
async function uploadMedia(base64Data, mimeType, ext, originalName) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user