Skip to content

fix(baileys): make the link preview User-Agent configurable - #2792

Open
patrickreisx wants to merge 2 commits into
evolution-foundation:developfrom
patrickreisx:fix/link-preview-user-agent
Open

patrickreisx wants to merge 2 commits into
evolution-foundation:developfrom
patrickreisx:fix/link-preview-user-agent

Conversation

@patrickreisx

@patrickreisx patrickreisx commented Nov 15, 2026 •

Copy link
Copy Markdown

📋 Description

BaileysStartupService.generateLinkPreview fetches the page of the first link in a text message with a hardcoded user-agent: 'googlebot'. Some sites only serve Open Graph tags to specific crawlers and answer any other agent with a redirect or an empty page. When that happens no preview is attached and the message goes out as plain text.

Example: Shopee short links (https://s.shopee.com.br/<code>). Measured with link-preview-js@3.2.0, using the exact options of generateLinkPreview:

Options Result
user-agent: googlebot (current) fetch failed (the short link answers with a 301 to another host)
googlebot + followRedirects: 'follow' 200, but empty title (the product page is rendered client-side, no OG tags)
user-agent: WhatsApp/2.23.20.0 A title + image, no redirect

This PR adds CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT to choose the User-Agent used for this fetch. The default stays googlebot, so behaviour is unchanged unless the variable is set.

Design notes for reviewers:

  • It is a dedicated setting rather than Baileys' socket options.headers: Baileys also uses config.options.headers for the WebSocket handshake with WhatsApp (lib/Socket/Client/websocket.js), so a User-Agent set there would leak into that connection.
  • It does not enable followRedirects, to avoid widening the SSRF surface of this fetch.

🔗 Related Issue

Related to #2262 and #1859 (both closed; a comment on #2262 describes the same root cause: the preview fetch is blocked or gets no content). Not a Closes.

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality) — a new optional setting
  • 💥 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

What I did

  • Called getLinkPreview (link-preview-js@3.2.0, the version develop depends on) with the options before and after this change, against a real Shopee short link (table above).
  • Ran npm run build (tsc --noEmit && tsup) on this branch: passes.
  • Ran the project's ESLint on the two changed TypeScript files: no errors.
  • Called the real, private BaileysStartupService.generateLinkPreview of develop (through tsx, with this change applied) against the same short link:
    • variable not set (googlebot): no preview, and the logger records Error generating link preview: TypeError: fetch failed;
    • CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT=WhatsApp/2.23.20.0 A: an externalAdReply with the product title and a thumbnail URL.
  • Confirmed, with a Baileys-level patch of the 2.3.7 image (a different code path), that the preview shows up in a real WhatsApp group when the fetch uses a WhatsApp/… agent.

What I did not do

  • Send a message through a running develop instance: 2.4.0 returns HTTP 503 until the instance is license-activated, which I did not do. So I did not see how WhatsApp renders the externalAdReply context built by this method.

  • There is no automated test suite for this area.

  • Manual testing completed

  • Functionality verified in development environment

  • No breaking changes introduced

  • Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

Not 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

Happy to rename the variable or turn it into a per-instance setting if you prefer. No real affiliate links or personal data are included.

Summary by Sourcery

Make the Baileys link-preview User-Agent configurable while retaining googlebot as the default.

New Features:

  • Add an optional configuration setting for customizing the User-Agent used when fetching WhatsApp link previews.

Bug Fixes:

  • Allow link previews for sites that require a specific crawler User-Agent while preserving the existing default behavior.

Documentation:

  • Expose the new link-preview User-Agent setting in the example environment configuration.

The preview of a link in a text message is built by fetching the page
with a hardcoded googlebot User-Agent. Some sites only serve Open Graph
tags to specific crawlers and answer others with a redirect or an empty
page, so no preview is attached.

Add CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT to choose the User-Agent used
for that fetch. The default stays googlebot, so behaviour is unchanged
unless it is set.
@sourcery-ai

sourcery-ai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Read the preview-fetch logic and its Baileys import together: the fetch now uses the configured User-Agent, falling back to googlebot.

  • src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts L70, L2397-2404

Configuration

Review the Baileys configuration type, its environment-variable mapping and default, and the example environment file together.

  • env.example L207-210
  • src/config/env.config.ts L329, L835

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/config/env.config.ts" line_range="835" />
<code_context>
       },
       BAILEYS: {
         VERSION: process.env?.CONFIG_BAILEYS_VERSION,
+        LINK_PREVIEW_USER_AGENT: process.env?.CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT || 'googlebot',
       },
       QRCODE: {
</code_context>
<issue_to_address>
**Preview User-Agent option stays hidden**

When operators copy `.env.example` as the README instructs and want to configure a custom preview User-Agent, `.env.example` omits `CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT`, so operators do not see the new option and previews continue using the `googlebot` default unless they discover and add the variable manually.

Add `CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT` to `.env.example` with its default value.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. A configured User-Agent can make a site return different Open Graph data, which may be attached to a WhatsApp message sent to recipients. Reverting restores the default for future previews but cannot recall previews already sent.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/config/env.config.ts Resolved
The README tells operators to copy .env.example, which did not list
CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT. Add it with its default value,
as env.example already has it.

This branch has not been deployed

No deployments
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