|
|
|
|
@ -153,11 +153,12 @@ const createWhatsApp = async phone => {
|
|
|
|
|
|
|
|
|
|
const parseDocumentMessage = async original => {
|
|
|
|
|
const documentMessage = original.message.documentMessage;
|
|
|
|
|
const imageBuffer = await downloadMediaMessage(
|
|
|
|
|
// original.message.documentWithCaptionMessage
|
|
|
|
|
const mediaBuffer = await downloadMediaMessage(
|
|
|
|
|
original, 'buffer', {}, { logger, reuploadRequest: waSocket.updateMediaMessage, },
|
|
|
|
|
);
|
|
|
|
|
const imageFilename = './temp/doc_' + whatsAppNo + '_' + original.key.id + '_' + documentMessage.fileName;
|
|
|
|
|
await writeFile(imageFilename, imageBuffer);
|
|
|
|
|
const documentFilename = './temp/doc_' + whatsAppNo + '_' + original.key.id + '_' + documentMessage.fileName;
|
|
|
|
|
await writeFile(documentFilename, mediaBuffer);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
type: 'document',
|
|
|
|
|
@ -165,7 +166,7 @@ const createWhatsApp = async phone => {
|
|
|
|
|
mimetype: documentMessage.mimetype,
|
|
|
|
|
sha256: uint8ArrayToBase64(documentMessage.fileSha256),
|
|
|
|
|
caption: documentMessage.caption,
|
|
|
|
|
filePath: imageFilename,
|
|
|
|
|
filePath: documentFilename,
|
|
|
|
|
link_original: documentMessage.url,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
@ -207,22 +208,16 @@ const createWhatsApp = async phone => {
|
|
|
|
|
if (original.message?.imageMessage) return parseImageMessage;
|
|
|
|
|
|
|
|
|
|
// if (original.message?.documentMessage) return parseDocumentMessage;
|
|
|
|
|
// documentWithCaptionMessage
|
|
|
|
|
|
|
|
|
|
if (original.message?.templateMessage) return parseTemplateMessage;
|
|
|
|
|
|
|
|
|
|
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 => {
|
|
|
|
|
console.info('messages.upsert: ', JSON.stringify(upsert, undefined, 2));
|
|
|
|
|
const msgEventSource = serverConfig.name + '.messages.upsert.' + upsert.type;
|
|
|
|
|
|
|
|
|
|
for (const msg of upsert.messages) {
|
|
|
|
|
|
|
|
|
|
@ -254,8 +249,6 @@ const createWhatsApp = async phone => {
|
|
|
|
|
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 msgFrom = msg.key.fromMe ? whatsAppNo : remoteNo;
|
|
|
|
|
const msgTo = msg.key.fromMe ? remoteNo : whatsAppNo;
|
|
|
|
|
@ -288,6 +281,8 @@ const createWhatsApp = async phone => {
|
|
|
|
|
const parsedMessage = await messageParser(msg);
|
|
|
|
|
const mergedMessage = Object.assign({}, standardMessage, parsedMessage);
|
|
|
|
|
console.info('mergedMessage: ', mergedMessage);
|
|
|
|
|
|
|
|
|
|
const emitEventName = msg.key.fromMe ? 'message:updated' : 'message:received';
|
|
|
|
|
waEmitter.emit(emitEventName, mergedMessage);
|
|
|
|
|
} else {
|
|
|
|
|
console.info('不支持该消息类型:', msg);
|
|
|
|
|
@ -295,28 +290,27 @@ const createWhatsApp = async phone => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleMessagesUpdate = async messageUpdate => {
|
|
|
|
|
console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2));
|
|
|
|
|
|
|
|
|
|
for (const msg of messageUpdate) {
|
|
|
|
|
|
|
|
|
|
// 没有明确标识状态的更新,忽略
|
|
|
|
|
const ignore = msg.update === undefined || msg.update.status === undefined;
|
|
|
|
|
|
|
|
|
|
if (ignore) continue;
|
|
|
|
|
|
|
|
|
|
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 msgStatus = formatStatus(msg.update.status);
|
|
|
|
|
const msgTimestamp = msg.messageTimestamp === undefined ? new Date().getTime() / 1000 : msg.messageTimestamp;
|
|
|
|
|
const msgStatus = msg.update === undefined ? formatStatus(msg.status) :formatStatus(msg.update.status);
|
|
|
|
|
|
|
|
|
|
waEmitter.emit('message:updated', {
|
|
|
|
|
return {
|
|
|
|
|
id: msg.key.id,
|
|
|
|
|
externalId,
|
|
|
|
|
status: msgStatus,
|
|
|
|
|
@ -325,16 +319,36 @@ const createWhatsApp = async phone => {
|
|
|
|
|
to: msgTo,
|
|
|
|
|
conversation: {
|
|
|
|
|
type: conversationType,
|
|
|
|
|
name: groupSubject,
|
|
|
|
|
},
|
|
|
|
|
customerProfile: {
|
|
|
|
|
id: decodeJid(msg.key.participant),
|
|
|
|
|
name: msg.pushName,
|
|
|
|
|
id: isGroup ? decodeJid(msg.key.participant) : decodeJid(msg.key.remoteJid),
|
|
|
|
|
// 商业号使用 verifiedBizName,个人使用 pushName
|
|
|
|
|
name: msg.verifiedBizName || msg.pushName,
|
|
|
|
|
},
|
|
|
|
|
whatsAppNo,
|
|
|
|
|
fromMe: msg.key.fromMe,
|
|
|
|
|
eventSource: serverConfig.name + '.messages.updated',
|
|
|
|
|
updateTime: formatTimestamp(new Date().getTime() / 1000),
|
|
|
|
|
updateTime: formatTimestamp(msgTimestamp),
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleMessagesUpdate = async messageUpdate => {
|
|
|
|
|
console.info('messages.update: ', JSON.stringify(messageUpdate, undefined, 2));
|
|
|
|
|
const msgEventSource = serverConfig.name + '.messages.updated'
|
|
|
|
|
|
|
|
|
|
for (const msg of messageUpdate) {
|
|
|
|
|
|
|
|
|
|
// 没有明确标识状态的更新,忽略
|
|
|
|
|
const ignore = msg.update === undefined || msg.update.status === undefined;
|
|
|
|
|
|
|
|
|
|
if (ignore) continue;
|
|
|
|
|
|
|
|
|
|
const standardMessage = await buildStandardMessage(msg)
|
|
|
|
|
const mergedMessage = Object.assign({}, standardMessage, {
|
|
|
|
|
eventSource: msgEventSource
|
|
|
|
|
});
|
|
|
|
|
//
|
|
|
|
|
waEmitter.emit('message:updated', mergedMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|