Skip to content

Conversation

@webmastersites
Copy link

@webmastersites webmastersites commented Jan 26, 2026

πŸ“‹ Description

πŸ”— Related Issue

Closes #(issue_number)

πŸ§ͺ Type of Change

  • πŸ› Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • πŸ’₯ Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • πŸ“š Documentation update
  • πŸ”§ Refactoring (no functional changes)
  • ⚑ Performance improvement
  • 🧹 Code cleanup
  • πŸ”’ Security fix

πŸ§ͺ Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types (if applicable)

πŸ“Έ Screenshots (if applicable)

βœ… Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

πŸ“ Additional Notes

Summary by Sourcery

Refine WhatsApp interactive button message handling with clearer validation rules and unified message construction.

Bug Fixes:

  • Prevent errors when sending button messages with missing or empty button arrays.
  • Enforce correct constraints and exclusivity between reply, PIX, and CTA button types to avoid invalid WhatsApp payloads.

Enhancements:

  • Unify interactive button message construction for reply and CTA buttons while preserving a dedicated flow for PIX messages.
  • Support optional headers, descriptions, and footers when composing interactive button messages for WhatsApp.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 26, 2026

Reviewer's Guide

Refactors WhatsApp button message construction to support multiple interactive button types (reply, PIX, CTA) with stricter validation and unified message building, while preserving media header support and send-with-typing behavior.

Class diagram for updated BaileysStartupService buttonMessage handling

classDiagram
  class BaileysStartupService {
    +mapType Map~string,string~
    +buttonMessage(data SendButtonsDto) Promise~proto.IMessage~
    +prepareMediaMessage(options PrepareMediaOptions) Promise~PreparedMediaMessage~
    +sendMessageWithTyping(number string, message proto.IMessage, options SendMessageOptions) Promise~proto.IMessage~
  }

  class SendButtonsDto {
    +number string
    +title string
    +description string
    +footer string
    +thumbnailUrl string
    +buttons SendButtonDto[]
    +delay number
    +quoted any
    +mentionsEveryOne boolean
    +mentioned string[]
  }

  class SendButtonDto {
    +type string  %% reply | pix | url | call | copy
    +label string
    +value string
  }

  class PrepareMediaOptions {
    +mediatype string  %% image
    +media string
  }

  class PreparedMediaMessage {
    +message PreparedMediaPayload
  }

  class PreparedMediaPayload {
    +imageMessage any
  }

  class SendMessageOptions {
    +delay number
    +presence string
    +quoted any
    +mentionsEveryOne boolean
    +mentioned string[]
  }

  class proto_IMessage {
  }

  BaileysStartupService --> SendButtonsDto : uses
  BaileysStartupService --> SendButtonDto : validates
  BaileysStartupService --> PrepareMediaOptions : creates
  BaileysStartupService --> PreparedMediaMessage : uses
  BaileysStartupService --> SendMessageOptions : creates
  BaileysStartupService --> proto_IMessage : builds
Loading

Flow diagram for WhatsApp buttonMessage validation and construction

flowchart TD
  A[start buttonMessage] --> B[Check buttons array exists and has length > 0]
  B -->|invalid| Z1[Throw BadRequestException At least one button is required]
  B -->|valid| C[Scan buttons to detect types]
  C --> C1[Set hasReplyButtons]
  C1 --> C2[Set hasPixButton]
  C2 --> C3[Set hasCTAButtons for url, call, copy]

  C3 --> D{hasReplyButtons}
  D -->|yes| D1[Check total buttons <= 3]
  D1 -->|no| Z2[Throw BadRequestException Maximum of 3 reply buttons allowed]
  D1 -->|yes| D2{hasCTAButtons or hasPixButton}
  D2 -->|yes| Z3[Throw BadRequestException Reply buttons cannot be mixed with CTA or PIX buttons]
  D2 -->|no| E
  D -->|no| E[Proceed]

  E --> F{hasPixButton}
  F -->|yes| F1[Check total buttons == 1]
  F1 -->|>1| Z4[Throw BadRequestException Only one PIX button is allowed]
  F1 -->|1| F2{hasReplyButtons or hasCTAButtons}
  F2 -->|yes| Z5[Throw BadRequestException PIX button cannot be mixed with other button types]
  F2 -->|no| G[Build PIX nativeFlowMessage with single PIX button]
  G --> H[Call sendMessageWithTyping and return]
  F -->|no| I[Proceed]

  I --> J{hasCTAButtons}
  J -->|yes| J1[Check total buttons <= 2]
  J1 -->|no| Z6[Throw BadRequestException Maximum of 2 CTA buttons allowed]
  J1 -->|yes| J2{hasReplyButtons}
  J2 -->|yes| Z7[Throw BadRequestException CTA buttons cannot be mixed with reply buttons]
  J2 -->|no| K[Proceed]
  J -->|no| K[Proceed]

  K --> L[Optionally prepare media header from thumbnailUrl]
  L --> M[Map all buttons to nativeFlowMessage buttons]
  M --> N[Build final interactiveMessage body, footer, header, nativeFlowMessage]
  N --> O[Call sendMessageWithTyping and return]

  Z1 --> X[end]
  Z2 --> X
  Z3 --> X
  Z4 --> X
  Z5 --> X
  Z6 --> X
  Z7 --> X
  H --> X
  O --> X[end]
Loading

File-Level Changes

Change Details Files
Refactor button message sending to centralize validation and interactive message construction for reply, PIX, and CTA buttons.
  • Add null/empty safety check for buttons array before processing
  • Compute booleans for reply, PIX, and CTA (url/call/copy) button presence up front
  • Enforce new validation rules: reply buttons (max 3, cannot mix with CTA/PIX), PIX button (exactly one, cannot mix with other types), CTA buttons (max 2, cannot mix with reply)
  • Special-case PIX messages to send a minimal nativeFlowMessage with a single PIX button and template metadata
  • Extract optional media header preparation to a single generatedMedia call using thumbnailUrl when present
  • Build the interactive message body text from title and optional description, plus optional footer and media header
  • Map all buttons generically via mapType and toJSONString and send via existing sendMessageWithTyping helper
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • There appears to be a stray aqui? line left in the method body, which should be removed to avoid confusing future readers.
  • The PIX-specific branch builds a very similar nativeFlowMessage to the generic case; consider extracting a small helper to construct this structure so the button/params assembly is defined in one place.
  • The button-type validation rules (reply/PIX/CTA combinations and limits) are embedded inline in buttonMessage; consider encapsulating this validation into a dedicated function to make the business rules easier to read and maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- There appears to be a stray `aqui?` line left in the method body, which should be removed to avoid confusing future readers.
- The PIX-specific branch builds a very similar `nativeFlowMessage` to the generic case; consider extracting a small helper to construct this structure so the button/params assembly is defined in one place.
- The button-type validation rules (reply/PIX/CTA combinations and limits) are embedded inline in `buttonMessage`; consider encapsulating this validation into a dedicated function to make the business rules easier to read and maintain.

## Individual Comments

### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:3313` </location>
<code_context>
-    if (data.buttons.length === 0) {
-      throw new BadRequestException('At least one button is required');
-    }
+  aqui?

-    const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');
</code_context>

<issue_to_address>
**issue (bug_risk):** Remove the stray `aqui?` line, as it looks like leftover debug text and will break compilation.

This token isn’t valid TypeScript and will cause a syntax error; please delete it.
</issue_to_address>

### Comment 2
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:3322-3323` </location>
<code_context>
-    const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');
+  const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');
+  const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');
+  const hasCTAButtons = data.buttons.some(
+    (btn) => btn.type === 'url' || btn.type === 'call' || btn.type === 'copy',
+  );

</code_context>

<issue_to_address>
**suggestion (bug_risk):** The new CTA detection and validation logic no longer guards against mixing PIX/reply with any future or unknown button types.

Previously, `hasOtherButtons` treated any non-`reply`/`pix` type as β€œother”, so reply/PIX could not be mixed with *any* other button type. Now `hasCTAButtons` only covers `url`/`call`/`copy`, so any new or existing types outside this list can be mixed with reply/PIX without validation catching it. If reply/PIX must remain exclusive with all other types, either keep a generic `hasOtherButtons` check alongside the CTA check, or treat any unsupported `btn.type` as β€œother” in the validation logic.

Suggested implementation:

```typescript
  const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');
  const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');
  const hasCTAButtons = data.buttons.some(
    (btn) => btn.type === 'url' || btn.type === 'call' || btn.type === 'copy',
  );
  // Treat any non-reply/non-pix button type as "other" to guard against future/unknown types
  const hasOtherButtons = data.buttons.some(
    (btn) => btn.type !== 'reply' && btn.type !== 'pix',
  );

  if ((hasReplyButtons || hasPixButton) && hasOtherButtons) {
    throw new BadRequestException(
      'Reply and PIX buttons cannot be combined with other button types',
    );
  }

```

1. If there is existing validation later in `buttonMessage` that attempted to prevent mixing `reply`/`pix` with other types (e.g., relying on a prior `hasOtherButtons` or only on `hasCTAButtons`), remove or adjust it to avoid duplicate/conflicting checks, since this new `hasOtherButtons` guard now centralizes that rule.
2. If `hasCTAButtons` is intended to drive any CTA-specific behavior (like building different payload structures), ensure that code uses the new `hasCTAButtons` variable defined here rather than recomputing or assuming specific types elsewhere.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click πŸ‘ or πŸ‘Ž on each comment and I'll use the feedback to improve your reviews.

Comment on lines 3322 to 3323
const hasCTAButtons = data.buttons.some(
(btn) => btn.type === 'url' || btn.type === 'call' || btn.type === 'copy',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): The new CTA detection and validation logic no longer guards against mixing PIX/reply with any future or unknown button types.

Previously, hasOtherButtons treated any non-reply/pix type as β€œother”, so reply/PIX could not be mixed with any other button type. Now hasCTAButtons only covers url/call/copy, so any new or existing types outside this list can be mixed with reply/PIX without validation catching it. If reply/PIX must remain exclusive with all other types, either keep a generic hasOtherButtons check alongside the CTA check, or treat any unsupported btn.type as β€œother” in the validation logic.

Suggested implementation:

  const hasReplyButtons = data.buttons.some((btn) => btn.type === 'reply');
  const hasPixButton = data.buttons.some((btn) => btn.type === 'pix');
  const hasCTAButtons = data.buttons.some(
    (btn) => btn.type === 'url' || btn.type === 'call' || btn.type === 'copy',
  );
  // Treat any non-reply/non-pix button type as "other" to guard against future/unknown types
  const hasOtherButtons = data.buttons.some(
    (btn) => btn.type !== 'reply' && btn.type !== 'pix',
  );

  if ((hasReplyButtons || hasPixButton) && hasOtherButtons) {
    throw new BadRequestException(
      'Reply and PIX buttons cannot be combined with other button types',
    );
  }
  1. If there is existing validation later in buttonMessage that attempted to prevent mixing reply/pix with other types (e.g., relying on a prior hasOtherButtons or only on hasCTAButtons), remove or adjust it to avoid duplicate/conflicting checks, since this new hasOtherButtons guard now centralizes that rule.
  2. If hasCTAButtons is intended to drive any CTA-specific behavior (like building different payload structures), ensure that code uses the new hasCTAButtons variable defined here rather than recomputing or assuming specific types elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant