Skip to content

Commit a82c2c4

Browse files
committed
fix(meta): handle message_echoes and guard missing contact fields
1 parent cd800f2 commit a82c2c4

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,38 @@ export class BusinessStartupService extends ChannelStartupService {
127127
if (!data) return;
128128

129129
const content = data.entry[0].changes[0].value;
130+
const firstMessage =
131+
content?.messages?.[0] ?? content?.message_echoes?.[0] ?? content?.smb_message_echoes?.[0] ?? undefined;
132+
const recipient = content?.statuses?.[0]?.recipient_id;
133+
const remoteId = firstMessage?.to ?? firstMessage?.from ?? recipient;
130134

131135
try {
132136
this.loadChatwoot();
133137

134-
this.eventHandler(content);
138+
this.eventHandler(this.normalizeWebhookContent(content));
135139

136-
this.phoneNumber = createJid(content.messages ? content.messages[0].from : content.statuses[0]?.recipient_id);
140+
if (remoteId) {
141+
this.phoneNumber = createJid(remoteId);
142+
}
137143
} catch (error) {
138144
this.logger.error(error);
139145
throw new InternalServerErrorException(error?.toString());
140146
}
141147
}
142148

149+
private normalizeWebhookContent(content: any) {
150+
if (!content || typeof content !== 'object') return content;
151+
152+
const normalized = { ...content };
153+
const echoes = normalized?.message_echoes ?? normalized?.smb_message_echoes;
154+
155+
if (!Array.isArray(normalized.messages) && Array.isArray(echoes) && echoes.length > 0) {
156+
normalized.messages = echoes;
157+
}
158+
159+
return normalized;
160+
}
161+
143162
private async downloadMediaMessage(message: any) {
144163
try {
145164
const id = message[message.type].id;
@@ -386,15 +405,19 @@ export class BusinessStartupService extends ChannelStartupService {
386405
try {
387406
let messageRaw: any;
388407
let pushName: any;
408+
const incomingContact = received?.contacts?.[0];
389409

390-
if (received.contacts) pushName = received.contacts[0].profile.name;
410+
if (incomingContact) {
411+
pushName = incomingContact?.profile?.name ?? incomingContact?.name ?? incomingContact?.wa_id ?? undefined;
412+
}
391413

392414
if (received.messages) {
393415
const message = received.messages[0]; // Añadir esta línea para definir message
416+
const remoteJid = createJid(message?.to ?? message?.from);
394417

395418
const key = {
396419
id: message.id,
397-
remoteJid: this.phoneNumber,
420+
remoteJid,
398421
fromMe: message.from === received.metadata.phone_number_id,
399422
};
400423

@@ -701,8 +724,11 @@ export class BusinessStartupService extends ChannelStartupService {
701724
where: { instanceId: this.instanceId, remoteJid: key.remoteJid },
702725
});
703726

727+
const contactPhone = incomingContact?.profile?.phone ?? incomingContact?.wa_id ?? message?.to ?? message?.from;
728+
if (!contactPhone) return;
729+
704730
const contactRaw: any = {
705-
remoteJid: received.contacts[0].profile.phone,
731+
remoteJid: createJid(contactPhone),
706732
pushName,
707733
// profilePicUrl: '',
708734
instanceId: this.instanceId,
@@ -714,7 +740,7 @@ export class BusinessStartupService extends ChannelStartupService {
714740

715741
if (contact) {
716742
const contactRaw: any = {
717-
remoteJid: received.contacts[0].profile.phone,
743+
remoteJid: createJid(contactPhone),
718744
pushName,
719745
// profilePicUrl: '',
720746
instanceId: this.instanceId,
@@ -745,10 +771,11 @@ export class BusinessStartupService extends ChannelStartupService {
745771
}
746772
if (received.statuses) {
747773
for await (const item of received.statuses) {
774+
const remoteJid = createJid(item?.recipient_id ?? this.phoneNumber);
748775
const key = {
749776
id: item.id,
750-
remoteJid: this.phoneNumber,
751-
fromMe: this.phoneNumber === received.metadata.phone_number_id,
777+
remoteJid,
778+
fromMe: item?.recipient_id ? item.recipient_id !== received.metadata.phone_number_id : true,
752779
};
753780
if (settings?.groups_ignore && key.remoteJid.includes('@g.us')) {
754781
return;

0 commit comments

Comments
 (0)