-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feature: handle with interactive button message for pix #2220
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
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1666,6 +1666,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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1984,8 +1988,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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2118,6 +2123,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') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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): Accessing paymentSettings[0] assumes paymentSettings is a non-empty array. Add a check to ensure paymentSettings exists and is not empty before accessing paymentSettings[0]. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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})`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2148
to
+2149
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. suggestion: Replacing '+55' may not handle all phone formats. This approach may fail if the phone key does not start with '+55'. Please ensure the phone format is validated or normalized before performing the replacement.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const send = await this.createMessage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| instance, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getConversation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| messageType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'WAID:' + body.key.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| quotedMsg, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!send) this.logger.warn('message not sent'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2151
to
+2162
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. suggestion (bug_risk): The return value of createMessage is checked only for falsy values. If createMessage throws an error, it will bypass the warning. Wrap the call in try/catch to ensure errors are properly handled.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } 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' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.