🎨refactor:messageUpdate 统一创建 Message

main
LiaoYijun 7 months ago
parent f51452c919
commit aed3a7bf8c

@ -153,11 +153,12 @@ const createWhatsApp = async phone => {
const parseDocumentMessage = async original => { const parseDocumentMessage = async original => {
const documentMessage = original.message.documentMessage; const documentMessage = original.message.documentMessage;
const imageBuffer = await downloadMediaMessage( // original.message.documentWithCaptionMessage
const mediaBuffer = await downloadMediaMessage(
original, 'buffer', {}, { logger, reuploadRequest: waSocket.updateMediaMessage, }, original, 'buffer', {}, { logger, reuploadRequest: waSocket.updateMediaMessage, },
); );
const imageFilename = './temp/doc_' + whatsAppNo + '_' + original.key.id + '_' + documentMessage.fileName; const documentFilename = './temp/doc_' + whatsAppNo + '_' + original.key.id + '_' + documentMessage.fileName;
await writeFile(imageFilename, imageBuffer); await writeFile(documentFilename, mediaBuffer);
return { return {
type: 'document', type: 'document',
@ -165,7 +166,7 @@ const createWhatsApp = async phone => {
mimetype: documentMessage.mimetype, mimetype: documentMessage.mimetype,
sha256: uint8ArrayToBase64(documentMessage.fileSha256), sha256: uint8ArrayToBase64(documentMessage.fileSha256),
caption: documentMessage.caption, caption: documentMessage.caption,
filePath: imageFilename, filePath: documentFilename,
link_original: documentMessage.url, link_original: documentMessage.url,
}, },
}; };
@ -207,22 +208,16 @@ const createWhatsApp = async phone => {
if (original.message?.imageMessage) return parseImageMessage; if (original.message?.imageMessage) return parseImageMessage;
// if (original.message?.documentMessage) return parseDocumentMessage; // if (original.message?.documentMessage) return parseDocumentMessage;
// documentWithCaptionMessage
if (original.message?.templateMessage) return parseTemplateMessage; if (original.message?.templateMessage) return parseTemplateMessage;
return null; return null;
}; };
const getUpsertSource = (upsert) => {
// source = prefix + infix + suffix
// prefix: server.name
// infix: upsert/update
// suffix: notify/append...
return serverConfig.name + '.messages.upsert.' + upsert.type;
};
const handleMessagesUpsert = async upsert => { const handleMessagesUpsert = async upsert => {
console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2)); console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2));
const msgEventSource = serverConfig.name + '.messages.upsert.' + upsert.type;
for (const msg of upsert.messages) { for (const msg of upsert.messages) {
@ -254,8 +249,6 @@ const createWhatsApp = async phone => {
groupSubjectCache.set(msg.key.remoteJid, groupMetadata.subject) groupSubjectCache.set(msg.key.remoteJid, groupMetadata.subject)
} }
const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received';
const msgEventSource = getUpsertSource(upsert);
const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound'; const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';
const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo; const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo; const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
@ -288,6 +281,8 @@ const createWhatsApp = async phone => {
const parsedMessage = await messageParser(msg); const parsedMessage = await messageParser(msg);
const mergedMessage = Object.assign({}, standardMessage, parsedMessage); const mergedMessage = Object.assign({}, standardMessage, parsedMessage);
console.info('mergedMessage: ', mergedMessage); console.info('mergedMessage: ', mergedMessage);
const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received';
waEmitter.emit(emitEventName, mergedMessage); waEmitter.emit(emitEventName, mergedMessage);
} else { } else {
console.info('不支持该消息类型:', msg); console.info('不支持该消息类型:', msg);
@ -295,8 +290,51 @@ const createWhatsApp = async phone => {
} }
} }
const buildStandardMessage = async msg => {
// 如果是群发(xxx@broadcast)participant 是发送人,不然则是 remoteJid
const remoteNo = isJidBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid);
const externalId = externalIdCache.get(msg.key.id);
const isPersonal = isJidPersonal(msg.key.remoteJid);
const conversationType = isPersonal ? 'individual' : 'group';
const isGroup = isJidGroup(msg.key.remoteJid);
let groupSubject = groupSubjectCache.get(msg.key.remoteJid);
if (isGroup && groupSubject === undefined) {
const groupMetadata = await waSocket.groupMetadata(msg.key.remoteJid);
groupSubject = groupMetadata.subject;
groupSubjectCache.set(msg.key.remoteJid, groupMetadata.subject)
}
const msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';
const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
const msgTimestamp = msg.messageTimestamp === undefined ? new Date().getTime() / 1000 : msg.messageTimestamp;
const msgStatus = msg.update === undefined ? formatStatus(msg.status) :formatStatus(msg.update.status);
return {
id: msg.key.id,
externalId,
status: msgStatus,
direction: msgDirection,
from: msgFrom,
to: msgTo,
conversation: {
type: conversationType,
name: groupSubject,
},
customerProfile: {
id: isGroup ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid),
// 商业号使用 verifiedBizName个人使用 pushName
name: msg.verifiedBizName || msg.pushName,
},
whatsAppNo,
fromMe: msg.key.fromMe,
updateTime: formatTimestamp(msgTimestamp),
}
};
const handleMessagesUpdate = async messageUpdate => { const handleMessagesUpdate = async messageUpdate => {
console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2)); console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2));
const msgEventSource = serverConfig.name + '.messages.updated'
for (const msg of messageUpdate) { for (const msg of messageUpdate) {
@ -305,36 +343,12 @@ const createWhatsApp = async phone => {
if (ignore) continue; if (ignore) continue;
// 如果是群发(xxx@broadcast)participant 是发送人,不然则是 remoteJid const standardMessage = await buildStandardMessage(msg)
const remoteNo = isJidBroadcast(msg.key.remoteJid) ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid); const mergedMessage = Object.assign({}, standardMessage, {
const externalId = externalIdCache.get(msg.key.id); eventSource: msgEventSource
const isPersonal = isJidPersonal(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 = formatStatus(msg.update.status);
waEmitter.emit('message:updated', {
id: msg.key.id,
externalId,
status: msgStatus,
direction: msgDirection,
from: msgFrom,
to: msgTo,
conversation: {
type: conversationType,
},
customerProfile: {
id: decodeJid(msg.key.participant),
name: msg.pushName,
},
whatsAppNo,
fromMe: msg.key.fromMe,
eventSource: serverConfig.name + '.messages.updated',
updateTime: formatTimestamp(new Date().getTime() / 1000),
}); });
//
waEmitter.emit('message:updated', mergedMessage);
} }
} }

Loading…
Cancel
Save