Repository navigation
fix(baileys): make the link preview User-Agent configurable - #2792
Open
patrickreisx wants to merge 2 commits into
Open
patrickreisx wants to merge 2 commits into
patrickreisx wants to merge 2 commits into
Conversation
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.
Contributor
Reviewer's GuideImplementationRead the preview-fetch logic and its Baileys import together: the fetch now uses the configured User-Agent, falling back to googlebot.
ConfigurationReview the Baileys configuration type, its environment-variable mapping and default, and the example environment file together.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
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.
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.
6 of 20 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
BaileysStartupService.generateLinkPreviewfetches the page of the first link in a text message with a hardcodeduser-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 withlink-preview-js@3.2.0, using the exact options ofgenerateLinkPreview:user-agent: googlebot(current)fetch failed(the short link answers with a 301 to another host)googlebot+followRedirects: 'follow'user-agent: WhatsApp/2.23.20.0 AThis PR adds
CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENTto choose the User-Agent used for this fetch. The default staysgooglebot, so behaviour is unchanged unless the variable is set.Design notes for reviewers:
options.headers: Baileys also usesconfig.options.headersfor the WebSocket handshake with WhatsApp (lib/Socket/Client/websocket.js), so a User-Agent set there would leak into that connection.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
🧪 Testing
What I did
getLinkPreview(link-preview-js@3.2.0, the versiondevelopdepends on) with the options before and after this change, against a real Shopee short link (table above).npm run build(tsc --noEmit && tsup) on this branch: passes.BaileysStartupService.generateLinkPreviewofdevelop(throughtsx, with this change applied) against the same short link:googlebot): no preview, and the logger recordsError generating link preview: TypeError: fetch failed;CONFIG_BAILEYS_LINK_PREVIEW_USER_AGENT=WhatsApp/2.23.20.0 A: anexternalAdReplywith the product title and a thumbnail URL.WhatsApp/…agent.What I did not do
Send a message through a running
developinstance: 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 theexternalAdReplycontext 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
📝 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:
Bug Fixes:
Documentation: