Skip to content

<!-- Generated by sourcery-ai[bot]: start review_guide --> #2348

@sokveasnahang-cpu

Description

@sokveasnahang-cpu

Reviewer's Guide

Adds Typebot support for sending file/embed blocks as WhatsApp media/audio and introduces active URL scraping to reliably generate WhatsApp link previews, wiring the preview context into the Baileys send flow while preserving explicit linkPreview disabling.

Sequence diagram for WhatsApp active link preview generation

sequenceDiagram
  actor User
  participant ClientApp
  participant BaileysStartupService
  participant LinkPreviewJS
  participant WhatsAppServer

  User ->> ClientApp: Send text message with URL
  ClientApp ->> BaileysStartupService: sendMessage(sender, message, options, group)
  activate BaileysStartupService

  BaileysStartupService ->> BaileysStartupService: determine linkPreview flag
  alt linkPreview explicitly disabled
    BaileysStartupService ->> WhatsAppServer: send message without previewContext
  else linkPreview enabled or undefined
    BaileysStartupService ->> BaileysStartupService: generateLinkPreview(text)
    activate BaileysStartupService
    BaileysStartupService ->> LinkPreviewJS: getLinkPreview(url, headers)
    LinkPreviewJS -->> BaileysStartupService: previewData(title, description, images)
    BaileysStartupService ->> BaileysStartupService: build externalAdReply context
    deactivate BaileysStartupService

    BaileysStartupService ->> WhatsAppServer: send message with previewContext
  end

  WhatsAppServer -->> User: Deliver message with rich link preview
Loading

Sequence diagram for Typebot file and embed message handling to WhatsApp

sequenceDiagram
  actor TypebotFlow
  participant TypebotService
  participant WhatsAppInstance
  participant WhatsAppServer

  TypebotFlow ->> TypebotService: Emit outgoing message
  TypebotService ->> TypebotService: Inspect message.type

  alt message.type is file or embed
    TypebotService ->> TypebotService: Extract content.url and content.name
    TypebotService ->> TypebotService: mediaType = getMediaType(mediaUrl)
    TypebotService ->> TypebotService: Derive fileName if missing

    alt mediaType is audio
      TypebotService ->> WhatsAppInstance: audioWhatsapp(number, delay, encoding, audioUrl)
    else other media type
      TypebotService ->> WhatsAppInstance: mediaMessage(number, delay, mediatype, mediaUrl, fileName)
    end

    TypebotService ->> TypebotService: sendTelemetry(/message/sendMedia)
  else other message types
    TypebotService ->> WhatsAppInstance: Existing handling for text, image, etc.
  end

  WhatsAppInstance ->> WhatsAppServer: Forward WhatsApp message
  WhatsAppServer -->> TypebotFlow: Message delivered to end user
Loading

Updated class diagram for BaileysStartupService and TypebotService message handling

classDiagram
  class BaileysStartupService {
    +sendMessage(sender:string, message:any, options:any, group:any, previewContext:any) Promise~any~
    -generateLinkPreview(text:string) Promise~any~
  }

  class TypebotService {
    +handleTypebotMessage(instance:any, session:any, message:any, settings:any) Promise~any~
    -getMediaType(mediaUrl:string) string
  }

  TypebotService --> BaileysStartupService : uses for WhatsApp delivery

  class LinkPreviewJS {
    +getLinkPreview(url:string, options:any) Promise~any~
  }

  BaileysStartupService --> LinkPreviewJS : generates link previews using
Loading

File-Level Changes

Change Details Files
Implement active link preview generation for WhatsApp text messages and inject result into sent message context.
  • Added generateLinkPreview helper that extracts the first URL from text, normalizes it, calls link-preview-js, and maps metadata into an externalAdReply structure while handling errors gracefully.
  • Updated sendMessage options handling so linkPreview=false explicitly disables previews, otherwise link previews are attempted for conversation messages.
  • Wired generated preview context into both group and direct-message send paths by passing it into sendMessage and spreading it into the outgoing message object.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Add Typebot support for file and embed message types by routing them through existing WhatsApp media/audio send helpers.
  • Extended TypebotService to detect file/embed message types and extract URL/name from message content, skipping send when URL is missing and emitting telemetry.
  • Derived a best-effort filename from the URL path or media type, falling back to generic names when necessary.
  • Routed audio media URLs through audioWhatsapp and all other media through mediaMessage with mediatype inferred from URL, and emitted a consolidated media telemetry event.
src/api/integrations/chatbot/typebot/services/typebot.service.ts
Minor formatting/indentation adjustments in BaileysStartupService.
  • Normalized indentation in QR code logging block.
  • Re-indented initialization of the messagesRepository Set for readability without changing logic.
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

Originally posted by @sourcery-ai in #2347 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions