perf: 在消息更新时也加上 externalId

dev/supplier-email-drawer
LiaoYijun 9 months ago
parent ae9217b833
commit 7da8586cbc

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

Loading…
Cancel
Save