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

Loading…
Cancel
Save