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 = [];
|
const parts = [];
|
||||||
for (const [group, msgs] of Object.entries(groups)) {
|
for (const [group, msgs] of Object.entries(groups)) {
|
||||||
parts.push(`[👥 ${group} 👥]`);
|
parts.push(`[👥 ${group} 👥]`);
|
||||||
|
let prevSender = '';
|
||||||
for (const m of msgs) {
|
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}`);
|
parts.push(`${m.sender}: ${m.text}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return parts.join('\n');
|
return parts.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,13 +102,15 @@ async function flushQueue() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function enqueue(group, sender, text) {
|
function enqueue(group, sender, text, showSender = true) {
|
||||||
msgCounter++;
|
msgCounter++;
|
||||||
messageQueue.push({ group, sender, text });
|
messageQueue.push({ group, sender, text, showSender });
|
||||||
|
|
||||||
scheduleFlush();
|
scheduleFlush();
|
||||||
log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`);
|
log('QUEUE', `Queue #${msgCounter} - Message from ${sender}, flushed at ${flushTime()}`);
|
||||||
|
|
||||||
|
console.log(formatBatch(messageQueue))
|
||||||
|
|
||||||
if (queueSize() >= config.batch.maxChars) {
|
if (queueSize() >= config.batch.maxChars) {
|
||||||
clearTimeout(flushTimer);
|
clearTimeout(flushTimer);
|
||||||
flushTimer = null;
|
flushTimer = null;
|
||||||
@ -252,22 +262,48 @@ async function startClient() {
|
|||||||
? config.ownName
|
? config.ownName
|
||||||
: (contact.name || contact.pushname || contact.shortName || contact.number || 'Unknown');
|
: (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 || '';
|
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 {
|
try {
|
||||||
const media = await message.downloadMedia();
|
const media = await message.downloadMedia();
|
||||||
if (media) {
|
if (media) {
|
||||||
const { uploadMedia } = require('./uploader');
|
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);
|
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) {
|
} catch (err) {
|
||||||
log('ERROR', `Media upload: ${err.message}`);
|
log('ERROR', `Media upload: ${err.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!body && mediaLabel) body = mediaLabel;
|
if (!body && typeLabel) body = typeLabel;
|
||||||
if (!body) return;
|
if (!body) return;
|
||||||
|
|
||||||
if (message.mentionedIds && message.mentionedIds.length > 0) {
|
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) {
|
} catch (err) {
|
||||||
log('ERROR', `Message handler: ${err.message}`);
|
log('ERROR', `Message handler: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|||||||
18
uploader.js
18
uploader.js
@ -5,21 +5,11 @@ function pad(n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function buildFileName(originalName, ext) {
|
function buildFileName(originalName, ext) {
|
||||||
|
if (originalName) return originalName;
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
const base = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_` +
|
||||||
// Format: YYYY-MM-DD
|
`${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
||||||
const dateStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
return ext ? `${base}.${ext}` : base;
|
||||||
|
|
||||||
// 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'}`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadMedia(base64Data, mimeType, ext, originalName) {
|
async function uploadMedia(base64Data, mimeType, ext, originalName) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user