perf: 优化消息处理逻辑,增加接收图片事件触发

dev/supplier-email-drawer
LiaoYijun 6 months ago
parent d60b1502ee
commit d1e328f209

@ -89,63 +89,40 @@ 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 msgDirection = msg.key.fromMe ? 'outbound' : 'inbound';
const msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
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;
if (msg.key.fromMe) { waEmitter.emit(emitEventName, {
waEmitter.emit('message:updated', { id: msg.key.id,
id: msg.key.id, externalId,
externalId, status: formatStatus(msg.status),
status: formatStatus(msg.status), direction: msgDirection,
direction: 'outbound', from: msgFrom,
from: whatsAppNo, to: msgTo,
to: remoteNo, type: 'text',
type: 'text', text: {
text: { body: text,
body: text, },
}, conversation: {
conversation: { type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', name: groupSubject,
name: groupSubject, },
}, customerProfile: {
customerProfile: { id: decodeJid(msg.key.participant),
id: decodeJid(msg.key.participant), name: msg.pushName,
name: msg.pushName, },
}, whatsAppNo,
whatsAppNo, fromMe: msg.key.fromMe,
fromMe: msg.key.fromMe, eventSource: serverConfig.name + '.messages.upsert.notify',
eventSource: serverConfig.name + '.messages.upsert.notify', updateTime: formatTimestamp(msg.messageTimestamp),
updateTime: formatTimestamp(msg.messageTimestamp), });
});
} else {
waEmitter.emit('message:received', {
id: msg.key.id,
externalId,
status: '',
direction: 'inbound',
from: remoteNo,
to: whatsAppNo,
type: 'text',
text: {
body: text,
},
conversation: {
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
name: groupSubject,
},
customerProfile: {
id: decodeJid(msg.key.participant),
name: msg.pushName,
},
whatsAppNo,
fromMe: msg.key.fromMe,
eventSource: serverConfig.name + '.messages.upsert.notify',
createTime: formatTimestamp(msg.messageTimestamp),
});
}
} else if (messageType === 'imageMessage') { } else if (messageType === 'imageMessage') {
console.info('image message: ', msg)
const imageMessage = msg.message.imageMessage; const imageMessage = msg.message.imageMessage;
const fileExtension = getFileExtension(imageMessage.mimetype); const fileExtension = getFileExtension(imageMessage.mimetype);
const imageBuffer = await downloadMediaMessage( const imageBuffer = await downloadMediaMessage(
@ -153,35 +130,34 @@ const createWhatsApp = async phone => {
); );
const imageFilename = './temp/image_' + whatsAppNo + '_' + msg.key.id + fileExtension; const imageFilename = './temp/image_' + whatsAppNo + '_' + msg.key.id + fileExtension;
await writeFile(imageFilename, imageBuffer); await writeFile(imageFilename, imageBuffer);
if (msg.key.fromMe) {
waEmitter.emit('message:updated', { waEmitter.emit(emitEventName, {
id: msg.key.id, id: msg.key.id,
externalId, externalId,
status: formatStatus(msg.status), status: formatStatus(msg.status),
direction: 'outbound', direction: msgDirection,
from: whatsAppNo, from: msgFrom,
to: remoteNo, to: msgTo,
type: 'image', type: 'image',
image: { image: {
mimetype: imageMessage.mimetype, mimetype: imageMessage.mimetype,
sha256: uint8ArrayToBase64(imageMessage.fileSha256), sha256: uint8ArrayToBase64(imageMessage.fileSha256),
caption: imageMessage.caption, caption: imageMessage.caption,
filePath: imageFilename, filePath: imageFilename,
link_original: imageMessage.url, link_original: imageMessage.url,
}, },
conversation: { conversation: {
type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group',
}, },
customerProfile: { customerProfile: {
id: decodeJid(msg.key.participant), id: decodeJid(msg.key.participant),
name: msg.pushName, name: msg.pushName,
}, },
whatsAppNo, whatsAppNo,
fromMe: msg.key.fromMe, fromMe: msg.key.fromMe,
eventSource: serverConfig.name + '.messages.upsert.notify', eventSource: serverConfig.name + '.messages.upsert.notify',
updateTime: formatTimestamp(msg.messageTimestamp), updateTime: formatTimestamp(msg.messageTimestamp),
}); });
}
} }
} }
} else if (upsert.type === 'append') { } else if (upsert.type === 'append') {

Loading…
Cancel
Save