-
Notifications
You must be signed in to change notification settings - Fork 6.2k
fix(meta): handle message_echoes and guard missing contact fields #2514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
a82c2c4
b8e4bfa
e4cd85a
532bc1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,19 +127,38 @@ export class BusinessStartupService extends ChannelStartupService { | |
| if (!data) return; | ||
|
|
||
| const content = data.entry[0].changes[0].value; | ||
| const firstMessage = | ||
| content?.messages?.[0] ?? content?.message_echoes?.[0] ?? content?.smb_message_echoes?.[0] ?? undefined; | ||
| const recipient = content?.statuses?.[0]?.recipient_id; | ||
| const remoteId = firstMessage?.to ?? firstMessage?.from ?? recipient; | ||
|
|
||
| try { | ||
| this.loadChatwoot(); | ||
|
|
||
| this.eventHandler(content); | ||
| this.eventHandler(this.normalizeWebhookContent(content)); | ||
|
|
||
| this.phoneNumber = createJid(content.messages ? content.messages[0].from : content.statuses[0]?.recipient_id); | ||
| if (remoteId) { | ||
| this.phoneNumber = createJid(remoteId); | ||
| } | ||
| } catch (error) { | ||
| this.logger.error(error); | ||
| throw new InternalServerErrorException(error?.toString()); | ||
| } | ||
| } | ||
|
|
||
| private normalizeWebhookContent(content: any) { | ||
| if (!content || typeof content !== 'object') return content; | ||
|
|
||
| const normalized = { ...content }; | ||
| const echoes = normalized?.message_echoes ?? normalized?.smb_message_echoes; | ||
|
|
||
| if (!Array.isArray(normalized.messages) && Array.isArray(echoes) && echoes.length > 0) { | ||
| normalized.messages = echoes; | ||
| } | ||
|
|
||
| return normalized; | ||
| } | ||
|
|
||
| private async downloadMediaMessage(message: any) { | ||
| try { | ||
| const id = message[message.type].id; | ||
|
|
@@ -386,15 +405,19 @@ export class BusinessStartupService extends ChannelStartupService { | |
| try { | ||
| let messageRaw: any; | ||
| let pushName: any; | ||
| const incomingContact = received?.contacts?.[0]; | ||
|
|
||
| if (received.contacts) pushName = received.contacts[0].profile.name; | ||
| if (incomingContact) { | ||
| pushName = incomingContact?.profile?.name ?? incomingContact?.name ?? incomingContact?.wa_id ?? undefined; | ||
| } | ||
|
|
||
| if (received.messages) { | ||
| const message = received.messages[0]; // Añadir esta línea para definir message | ||
| const remoteJid = createJid(message?.to ?? message?.from); | ||
|
|
||
| const key = { | ||
| id: message.id, | ||
| remoteJid: this.phoneNumber, | ||
| remoteJid, | ||
| fromMe: message.from === received.metadata.phone_number_id, | ||
| }; | ||
|
|
||
|
|
@@ -701,8 +724,11 @@ export class BusinessStartupService extends ChannelStartupService { | |
| where: { instanceId: this.instanceId, remoteJid: key.remoteJid }, | ||
| }); | ||
|
|
||
| const contactPhone = incomingContact?.profile?.phone ?? incomingContact?.wa_id ?? message?.to ?? message?.from; | ||
| if (!contactPhone) return; | ||
|
|
||
| const contactRaw: any = { | ||
| remoteJid: received.contacts[0].profile.phone, | ||
| remoteJid: createJid(contactPhone), | ||
| pushName, | ||
| // profilePicUrl: '', | ||
| instanceId: this.instanceId, | ||
|
|
@@ -714,7 +740,7 @@ export class BusinessStartupService extends ChannelStartupService { | |
|
|
||
| if (contact) { | ||
| const contactRaw: any = { | ||
| remoteJid: received.contacts[0].profile.phone, | ||
| remoteJid: createJid(contactPhone), | ||
| pushName, | ||
| // profilePicUrl: '', | ||
| instanceId: this.instanceId, | ||
|
|
@@ -745,10 +771,11 @@ export class BusinessStartupService extends ChannelStartupService { | |
| } | ||
| if (received.statuses) { | ||
| for await (const item of received.statuses) { | ||
| const remoteJid = createJid(item?.recipient_id ?? this.phoneNumber); | ||
| const key = { | ||
| id: item.id, | ||
| remoteJid: this.phoneNumber, | ||
| fromMe: this.phoneNumber === received.metadata.phone_number_id, | ||
| remoteJid, | ||
| fromMe: item?.recipient_id ? item.recipient_id !== received.metadata.phone_number_id : true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): The new Previously |
||
| }; | ||
| if (settings?.groups_ignore && key.remoteJid.includes('@g.us')) { | ||
| return; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.