From 49ad9812a40ed722dfd8e8f548d102500a3ce5c7 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 12 Jul 2024 13:45:31 -0300 Subject: [PATCH] fix: Correction in response returns from buttons, lists and templates Fixes the display of unnecessary logs in the webhook controller and updates the way context data is handled in the Whatsapp Business service. Adds private methods to handle different types of messages, such as buttons, reactions and contacts. Modified files: - webhook.controller.ts - whatsapp.business.service.ts --- src/api/controllers/webhook.controller.ts | 1 - .../channels/whatsapp.business.service.ts | 28 ++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/api/controllers/webhook.controller.ts b/src/api/controllers/webhook.controller.ts index 4eea4497..0e79b2c9 100644 --- a/src/api/controllers/webhook.controller.ts +++ b/src/api/controllers/webhook.controller.ts @@ -57,7 +57,6 @@ export class WebhookController { } public async receiveWebhook(data: any) { - console.log('webhook/meta', data); this.webhookService.receiveWebhook(data); return { diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index f0343ee9..da61b72a 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -174,6 +174,13 @@ export class BusinessStartupService extends ChannelStartupService { return content; } + private messageButtonJson(received: any) { + const message = received.messages[0]; + let content: any = { conversation: received.messages[0].button?.text }; + message.context ? (content = { ...content, contextInfo: { stanzaId: message.context.id } }) : content; + return content; + } + private messageReactionJson(received: any) { const message = received.messages[0]; let content: any = { @@ -315,7 +322,7 @@ export class BusinessStartupService extends ChannelStartupService { ...this.messageMediaJson(received), base64: buffer ? buffer.toString('base64') : undefined, }, - contextInfo: this.messageTextJson(received)?.contextInfo, + contextInfo: this.messageMediaJson(received)?.contextInfo, messageType: this.renderMessageType(received.messages[0].type), messageTimestamp: parseInt(received.messages[0].timestamp) as number, source: 'unknown', @@ -328,7 +335,20 @@ export class BusinessStartupService extends ChannelStartupService { message: { ...this.messageInteractiveJson(received), }, - contextInfo: this.messageTextJson(received)?.contextInfo, + contextInfo: this.messageInteractiveJson(received)?.contextInfo, + messageType: 'conversation', + messageTimestamp: parseInt(received.messages[0].timestamp) as number, + source: 'unknown', + instanceId: this.instanceId, + }; + } else if (received?.messages[0].button) { + messageRaw = { + key, + pushName, + message: { + ...this.messageButtonJson(received), + }, + contextInfo: this.messageButtonJson(received)?.contextInfo, messageType: 'conversation', messageTimestamp: parseInt(received.messages[0].timestamp) as number, source: 'unknown', @@ -341,7 +361,7 @@ export class BusinessStartupService extends ChannelStartupService { message: { ...this.messageReactionJson(received), }, - contextInfo: this.messageTextJson(received)?.contextInfo, + contextInfo: this.messageReactionJson(received)?.contextInfo, messageType: 'reactionMessage', messageTimestamp: parseInt(received.messages[0].timestamp) as number, source: 'unknown', @@ -354,7 +374,7 @@ export class BusinessStartupService extends ChannelStartupService { message: { ...this.messageContactsJson(received), }, - contextInfo: this.messageTextJson(received)?.contextInfo, + contextInfo: this.messageContactsJson(received)?.contextInfo, messageType: 'conversation', messageTimestamp: parseInt(received.messages[0].timestamp) as number, source: 'unknown',