perf: 增加客人群发消息处理

dev/supplier-email-drawer
LiaoYijun 6 months ago
parent 4399a48ead
commit f5de6e8b2d

@ -79,10 +79,13 @@ const createWhatsApp = async phone => {
} }
const messageType = Object.keys(msg.message)[0]; const messageType = Object.keys(msg.message)[0];
const remoteNo = decodeJid(msg.key.remoteJid); // 如果是群发(status@broadcast)participant 是发送人,不然则是 remoteJid
const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid);
const externalId = externalIdCache.get(msg.key.id); const externalId = externalIdCache.get(msg.key.id);
const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid);
const conversationType = isPersonal ? 'individual' : 'group';
const isGroup = isJidGroup(msg.key.remoteJid); const isGroup = isJidGroup(msg.key.remoteJid);
let groupSubject = groupSubjectCache.get(msg.key.remoteJid) let groupSubject = groupSubjectCache.get(msg.key.remoteJid)
if (isGroup && groupSubject === undefined) { if (isGroup && groupSubject === undefined) {
const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid); const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid);
groupSubject = groupMetadata.subject; groupSubject = groupMetadata.subject;
@ -111,7 +114,7 @@ const createWhatsApp = async phone => {
body: text, body: text,
}, },
conversation: { conversation: {
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', type: conversationType,
name: groupSubject, name: groupSubject,
}, },
customerProfile: { customerProfile: {
@ -148,7 +151,7 @@ const createWhatsApp = async phone => {
link_original: imageMessage.url, link_original: imageMessage.url,
}, },
conversation: { conversation: {
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', type: conversationType,
name: groupSubject, name: groupSubject,
}, },
customerProfile: { customerProfile: {
@ -166,34 +169,41 @@ const createWhatsApp = async phone => {
for (const msg of upsert.messages) { for (const msg of upsert.messages) {
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 = decodeJid(msg.key.remoteJid); // 如果是群发(status@broadcast)participant 是发送人,不然则是 remoteJid
const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid);
const externalId = externalIdCache.get(msg.key.id); const externalId = externalIdCache.get(msg.key.id);
if (msg.key.fromMe) { const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid);
waEmitter.emit('message:updated', { const conversationType = isPersonal ? 'individual' : 'group';
id: msg.key.id,
externalId, const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received';
status: formatStatus(msg.status), const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';
direction: 'outbound', const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
from: whatsAppNo, const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
to: fromWhatsAppNo, const msgStatus = msg.status === undefined ? '' : formatStatus(msg.status);
type: 'text',
text: { waEmitter.emit(emitEventName, {
body: text, id: msg.key.id,
}, externalId,
conversation: { status: msgStatus,
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', direction: msgDirection,
}, from: msgFrom,
customerProfile: { to: msgTo,
id: decodeJid(msg.participant), type: 'text',
name: msg.pushName, text: {
}, body: text,
whatsAppNo, },
fromMe: msg.key.fromMe, conversation: {
eventSource: serverConfig.name + '.messages.upsert.append', type: conversationType,
updateTime: formatTimestamp(msg.messageTimestamp), },
}); customerProfile: {
} id: decodeJid(msg.participant),
name: msg.pushName,
},
whatsAppNo,
fromMe: msg.key.fromMe,
eventSource: serverConfig.name + '.messages.upsert.append',
updateTime: formatTimestamp(msg.messageTimestamp),
});
} }
} }
} }
@ -209,16 +219,26 @@ const createWhatsApp = async phone => {
if (ignore) continue; if (ignore) continue;
// 如果是群发(status@broadcast)participant 是发送人,不然则是 remoteJid
const remoteNo = isJidStatusBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid);
const externalId = externalIdCache.get(msg.key.id); const externalId = externalIdCache.get(msg.key.id);
const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid);
const conversationType = isPersonal ? 'individual' : 'group';
const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';
const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
const msgStatus = msg.status === undefined ? '' : formatStatus(msg.status);
waEmitter.emit('message:updated', { waEmitter.emit('message:updated', {
id: msg.key.id, id: msg.key.id,
externalId, externalId,
status: formatStatus(msg.update?.status), status: msgStatus,
direction: msg.key.fromMe ? 'outbound' : 'inbound', direction: msgDirection,
from: msg.key.fromMe ? whatsAppNo : decodeJid(msg.key.remoteJid), from: msgFrom,
to: msg.key.fromMe ? decodeJid(msg.key.remoteJid) : whatsAppNo, to: msgTo,
conversation: { conversation: {
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', type: conversationType,
}, },
customerProfile: { customerProfile: {
id: decodeJid(msg.key.participant), id: decodeJid(msg.key.participant),

Loading…
Cancel
Save