Skip to content

Ignore events that are not messages (like EPHEMERAL_SYNC_RESPONSE) #1732

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

rafwell
Copy link
Contributor

@rafwell rafwell commented Jul 17, 2025

In some cases the body.key.id doesn't exists. That cause an error.

Look my log for debug purposes.

body.key.id is null or undefined in messages.edit. Full body object: {"key":{"remoteJid":"[email protected]","fromMe":true},"type":"EPHEMERAL_SYNC_RESPONSE","ephemeralExpiration":7776000,"ephemeralSettingTimestamp":"1752775691","disappearingMode":{"initiator":"INITIATED_BY_ME","trigger":"ACCOUNT_SETTING","initiatedByMe":true}} 

Summary by Sourcery

Add guard clauses to prevent errors from non-message events and missing body.key or key.id across ChatwootService methods

Bug Fixes:

  • Ignore events with types other than 'message' or 'conversation' in eventWhatsapp and messages.edit handlers to prevent unintended processing
  • Add checks in createConversation, messages.upsert, and messages.edit to handle null or undefined body.key and key.id, logging warnings and returning early instead of throwing errors

Copy link
Contributor

sourcery-ai bot commented Jul 17, 2025

Reviewer's Guide

Enhances ChatwootService to gracefully handle non-message events and missing key properties by adding type-based filtering, null-checks with logging, and standardizing method calls to prevent runtime errors.

Sequence diagram for handling non-message events in eventWhatsapp

sequenceDiagram
    participant Client
    participant ChatwootService
    participant Logger
    participant waMonitor

    Client->>ChatwootService: eventWhatsapp(event, instance, body)
    alt body.type is not 'message' or 'conversation'
        ChatwootService->>Logger: verbose("Ignoring non-message event type: ...")
        ChatwootService-->>Client: return
    else body.type is 'message' or 'conversation'
        ChatwootService->>waMonitor: access waInstances
        Note over ChatwootService,waMonitor: Continue normal processing
    end
Loading

Sequence diagram for handling missing key in createConversation and messages.upsert

sequenceDiagram
    participant Client
    participant ChatwootService
    participant Logger

    Client->>ChatwootService: createConversation(instance, body)
    alt body.key is null or undefined
        ChatwootService->>Logger: warn("body.key is null or undefined in createConversation...")
        ChatwootService-->>Client: return null
    else body.key exists
        Note over ChatwootService: Continue normal processing
    end

    Client->>ChatwootService: eventWhatsapp('messages.upsert', instance, body)
    alt body.key is null or undefined
        ChatwootService->>Logger: warn("body.key is null or undefined...")
        ChatwootService-->>Client: return
    else body.key exists
        Note over ChatwootService: Continue normal processing
    end
Loading

Sequence diagram for handling messages.edit with missing key.id or non-message type

sequenceDiagram
    participant Client
    participant ChatwootService
    participant Logger

    Client->>ChatwootService: eventWhatsapp('messages.edit', instance, body)
    alt body.type is not 'message'
        ChatwootService->>Logger: verbose("Ignoring non-message event type: ...")
        ChatwootService-->>Client: return
    else body.key.id is null or undefined
        ChatwootService->>Logger: warn("body.key.id is null or undefined in messages.edit...")
        ChatwootService-->>Client: return
    else body.type is 'message' and body.key.id exists
        Note over ChatwootService: Continue normal processing
    end
Loading

File-Level Changes

Change Details Files
Add type-based filtering for non-message events
  • Filter out non-message types in eventWhatsapp handler
  • Filter out non-message types in messages.edit handler
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Add null-checks and logging for missing key data
  • Guard and log when body.key is missing in createConversation
  • Guard and log when body.key is missing in messages.upsert and send.message handlers
  • Guard and log when body.key.id is missing in messages.edit
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts
Standardize getMessageByKeyId invocation
  • Replace optional-chained body?.key?.id with body.key.id for clarity
src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Possibly linked issues

  • #-1: The PR fixes TypeError: Cannot read properties of undefined by adding checks for body.key and body.key.id in message processing, directly addressing the issue's error type.

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

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