diff --git a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts index 77d591bd..3b156c31 100644 --- a/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts +++ b/src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts @@ -1654,6 +1654,10 @@ export class ChatwootService { return result; } + private isInteractiveButtonMessage(messageType: string, message: any) { + return messageType === 'interactiveMessage' && message.interactiveMessage?.nativeFlowMessage?.buttons?.length > 0; + } + private getAdsMessage(msg: any) { interface AdsMessage { title: string; @@ -1972,8 +1976,9 @@ export class ChatwootService { const adsMessage = this.getAdsMessage(body); const reactionMessage = this.getReactionMessage(body.message); + const isInteractiveButtonMessage = this.isInteractiveButtonMessage(body.messageType, body.message); - if (!bodyMessage && !isMedia && !reactionMessage) { + if (!bodyMessage && !isMedia && !reactionMessage && !isInteractiveButtonMessage) { this.logger.warn('no body message found'); return; } @@ -2098,6 +2103,50 @@ export class ChatwootService { return; } + if (isInteractiveButtonMessage) { + const buttons = body.message.interactiveMessage.nativeFlowMessage.buttons; + this.logger.info('is Interactive Button Message: ' + JSON.stringify(buttons)); + + for (const button of buttons) { + const buttonParams = JSON.parse(button.buttonParamsJson); + const paymentSettings = buttonParams.payment_settings; + + if (button.name === 'payment_info' && paymentSettings[0].type === 'pix_static_code') { + const pixSettings = paymentSettings[0].pix_static_code; + const pixKeyType = (() => { + switch (pixSettings.key_type) { + case 'EVP': + return 'Chave Aleatória'; + case 'EMAIL': + return 'E-mail'; + case 'PHONE': + return 'Telefone'; + default: + return pixSettings.key_type; + } + })(); + const pixKey = pixSettings.key_type === 'PHONE' ? pixSettings.key.replace('+55', '') : pixSettings.key; + const content = `*${pixSettings.merchant_name}*\nChave PIX: ${pixKey} (${pixKeyType})`; + + const send = await this.createMessage( + instance, + getConversation, + content, + messageType, + false, + [], + body, + 'WAID:' + body.key.id, + quotedMsg, + ); + if (!send) this.logger.warn('message not sent'); + } else { + this.logger.warn('Interactive Button Message not mapped'); + } + } + return; + } + const isAdsMessage = (adsMessage && adsMessage.title) || adsMessage.body || adsMessage.thumbnailUrl; if (isAdsMessage) { const imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });