diff --git a/wai-server/core/baileys/index.js b/wai-server/core/baileys/index.js index 7144439..d14ca09 100644 --- a/wai-server/core/baileys/index.js +++ b/wai-server/core/baileys/index.js @@ -79,10 +79,13 @@ const createWhatsApp = async phone => { } 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 isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const conversationType = isPersonal ? 'individual' : 'group'; const isGroup = isJidGroup(msg.key.remoteJid); - let groupSubject = groupSubjectCache.get(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; @@ -111,7 +114,7 @@ const createWhatsApp = async phone => { body: text, }, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, name: groupSubject, }, customerProfile: { @@ -148,7 +151,7 @@ const createWhatsApp = async phone => { link_original: imageMessage.url, }, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, name: groupSubject, }, customerProfile: { @@ -166,34 +169,41 @@ const createWhatsApp = async phone => { for (const msg of upsert.messages) { if (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); - if (msg.key.fromMe) { - waEmitter.emit('message:updated', { - id: msg.key.id, - externalId, - status: formatStatus(msg.status), - direction: 'outbound', - from: whatsAppNo, - to: fromWhatsAppNo, - type: 'text', - text: { - body: text, - }, - conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', - }, - customerProfile: { - id: decodeJid(msg.participant), - name: msg.pushName, - }, - whatsAppNo, - fromMe: msg.key.fromMe, - eventSource: serverConfig.name + '.messages.upsert.append', - updateTime: formatTimestamp(msg.messageTimestamp), - }); - } + const isPersonal = isJidUser(msg.key.remoteJid) || isJidStatusBroadcast(msg.key.remoteJid); + const conversationType = isPersonal ? 'individual' : 'group'; + + 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; + const msgStatus = msg.status === undefined ? '' : formatStatus(msg.status); + + waEmitter.emit(emitEventName, { + id: msg.key.id, + externalId, + status: msgStatus, + direction: msgDirection, + from: msgFrom, + to: msgTo, + type: 'text', + text: { + body: text, + }, + conversation: { + type: conversationType, + }, + 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; + // 如果是群发(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 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', { id: msg.key.id, externalId, - status: formatStatus(msg.update?.status), - direction: msg.key.fromMe ? 'outbound' : 'inbound', - from: msg.key.fromMe ? whatsAppNo : decodeJid(msg.key.remoteJid), - to: msg.key.fromMe ? decodeJid(msg.key.remoteJid) : whatsAppNo, + status: msgStatus, + direction: msgDirection, + from: msgFrom, + to: msgTo, conversation: { - type: isJidUser(msg.key.remoteJid) ? 'individual' : 'group', + type: conversationType, }, customerProfile: { id: decodeJid(msg.key.participant),