diff --git a/wai-server/core/baileys/index.js b/wai-server/core/baileys/index.js index 69b890c..b63ec27 100644 --- a/wai-server/core/baileys/index.js +++ b/wai-server/core/baileys/index.js @@ -35,6 +35,7 @@ const createWhatsApp = async phone => { let qrCode = null; const channelId = generateId(); const whatsAppNo = phone; + const msgIdMap = new Map(); const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs-' + phone + '_' + channelId + '.txt')); @@ -58,16 +59,17 @@ const msgRetryCounterCache = new NodeCache(); return new Promise(() => { waSocket.sendMessage(jid, { text: content }) .then(msg => { - waEmitter.emit('message:updated', { - id: msg.key.id, - externalId, - status: 'saved', - direction: 'outbound', - from: whatsAppNo, - to: number, - eventSource: 'sendMessage.promise.then', - updateTime: formatTimestamp(new Date().getTime() / 1000), - }) + msgIdMap.set(msg.key.id, externalId); + waEmitter.emit('message:updated', { + id: msg.key.id, + externalId, + status: 'saved', + direction: 'outbound', + from: whatsAppNo, + to: number, + eventSource: 'sendMessage.promise.then', + updateTime: formatTimestamp(new Date().getTime() / 1000), + }) }) .catch(ex => { waEmitter.emit('message:updated', { @@ -138,9 +140,11 @@ const msgRetryCounterCache = new NodeCache(); sendTextMessage(fromWhatsAppNo, '这是自测消息:' + new Date().toString()); } + const externalId = msgIdMap.get(msg.key.id); if (msg.key.fromMe) { waEmitter.emit('message:updated', { id: msg.key.id, + externalId, status: formatStatus(msg.status), direction: 'outbound', from: whatsAppNo, @@ -162,6 +166,7 @@ const msgRetryCounterCache = new NodeCache(); } else { waEmitter.emit('message:received', { id: msg.key.id, + externalId, status: '', direction: 'inbound', from: fromWhatsAppNo, @@ -188,9 +193,12 @@ const msgRetryCounterCache = new NodeCache(); if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) { const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text; const fromWhatsAppNo = parsePhoneNumber(msg.key.remoteJid); + + const externalId = msgIdMap.get(msg.key.id); if (msg.key.fromMe) { waEmitter.emit('message:updated', { id: msg.key.id, + externalId, status: formatStatus(msg.status), direction: 'outbound', from: whatsAppNo, @@ -219,8 +227,11 @@ const msgRetryCounterCache = new NodeCache(); console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2)); for (const msg of messageUpdate) { + + const externalId = msgIdMap.get(msg.key.id); waEmitter.emit('message:updated', { id: msg.key.id, + externalId, status: formatStatus(msg.update?.status), direction: msg.key.fromMe ? 'outbound' : 'inbound', from: msg.key.fromMe ? whatsAppNo : parsePhoneNumber(msg.key.remoteJid),