Skip to content

Conversation

@splusoficial
Copy link

@splusoficial splusoficial commented Jun 15, 2025

Summary by Sourcery

Refine bot matching to include both 'all' and 'none' triggers, add an early exit for paused sessions, and standardize return behavior in chatbot controllers.

New Features:

  • Skip processing incoming messages for sessions with status 'paused'.

Bug Fixes:

  • Include triggerType 'none' alongside 'all' for catch-all bot matching.
  • Return null instead of undefined when no matching bot is found in ChatbotController.

Enhancements:

  • Initialize findBot with an explicit type and unify return logic in bot lookup and controllers.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 15, 2025

Reviewer's Guide

This PR enhances bot trigger validation to treat 'none' triggers as wildcards, adds logic to skip processing of paused sessions, and standardizes the findBot return type in controllers.

Sequence Diagram: Paused Session Message Handling in BaseChatbotController

sequenceDiagram
    actor User
    participant BCC as BaseChatbotController

    User->>BCC: Sends message (remoteJid, content)
    BCC->>BCC: Retrieve session for remoteJid
    alt session exists AND session.status == "paused"
        BCC->>BCC: Log warning "Session for ${remoteJid} is paused"
        Note right of BCC: Skip message processing
    else session not paused or does not exist
        BCC->>BCC: Continue message processing (existing logic)
        %% Potentially interact with ChatbotController / findBotByTrigger etc.
    end
Loading

Sequence Diagram: Updated Bot Search Logic in findBotByTrigger

sequenceDiagram
    participant Caller as e.g. ChatbotController
    participant FBT as findBotByTrigger
    participant BR as BotRepository

    Caller->>FBT: findBotByTrigger(content, instanceId)
    FBT->>BR: findFirst({ where: { triggerType: { in: ['all', 'none'] }, enabled: true, instanceId: instanceId } })
    BR-->>FBT: bot (e.g., findTriggerAllOrNone)
    alt Bot found with 'all' or 'none' trigger
        FBT-->>Caller: Returns found bot
    else No bot found with 'all' or 'none' trigger
        FBT->>FBT: Check for 'advanced' triggers (calling BR)
        alt Bot found with 'advanced' trigger
            FBT-->>Caller: Returns found bot
        else No bot found with 'advanced' trigger
            FBT->>FBT: Check for 'equals' triggers (calling BR)
            alt Bot found with 'equals' trigger
                FBT-->>Caller: Returns found bot
            else No bot found with 'equals' trigger
                FBT->>FBT: Check for 'regex' triggers (calling BR)
                alt Bot found with 'regex' trigger
                    FBT-->>Caller: Returns found bot
                else No bot found with any trigger
                    FBT-->>Caller: Returns null (or undefined)
                end
            end
        end
    end
Loading

Class Diagram: Updates to Chatbot Controller Methods

classDiagram
    class BaseChatbotController {
        +handleMessage(messageData: any) void
    }
    class ChatbotController {
        +findBotAndTriggerAction(content: string, botRepository: any, instance: InstanceDto, session?: IntegrationSession) : Promise<any | null>
    }

    %% BaseChatbotController.handleMessage() is the method where the paused session check is added.
    %% ChatbotController.findBotAndTriggerAction() is updated to handle the new return possibility (null) from bot finding logic.
Loading

File-Level Changes

Change Details Files
Enhanced trigger matching in findBotByTrigger to include 'none' as wildcard
  • Use 'in' operator to match both 'all' and 'none' trigger types
  • Rename findTriggerAll variable to findTriggerAllOrNone
  • Enclose early-return conditions in braces for consistency
src/utils/findBotByTrigger.ts
Add session pause handling to skip message processing
  • Check for session.status === 'paused' before processing requests
  • Log a warning when a paused session causes skipping
src/api/integrations/chatbot/base-chatbot.controller.ts
Standardize findBot return behavior in ChatbotController
  • Declare findBot variable explicitly as any
  • Return null instead of undefined when no bot is found
src/api/integrations/chatbot/chatbot.controller.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

@DavidsonGomes DavidsonGomes merged commit 07cccb7 into EvolutionAPI:develop Jun 17, 2025
1 check passed
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.

2 participants