feat(chatwoot): capture ad-referral metadata from WhatsApp ads - #2683
Open
edneymatias wants to merge 2 commits into
Open
feat(chatwoot): capture ad-referral metadata from WhatsApp ads#2683edneymatias wants to merge 2 commits into
edneymatias wants to merge 2 commits into
Conversation
…d webp thumbnail fallback Extracts sourceId, sourceType, mediaType and mediaUrl from externalAdReply alongside the existing title/body/thumbnailUrl/sourceUrl fields, and exposes them as a normalized referral object under the outgoing message's content_attributes. Also hardens the ads-thumbnail pipeline: a failed webp decode in Jimp (Jimp core has no webp codec) previously threw an unhandled exception that aborted message delivery after the Chatwoot conversation shell had already been created, leaving an empty conversation behind. Both the thumbnail download and the Jimp resize now fall back gracefully (raw, unresized image) instead of failing the whole send.
Contributor
Reviewer's GuideExtends Chatwoot WhatsApp integration to capture richer ad referral metadata from WhatsApp externalAdReply payloads and attach it as normalized referral content_attributes on outgoing messages, while hardening the ads-thumbnail download and image processing pipeline to fail gracefully instead of aborting message delivery. Flow diagram for building referral content_attributes from externalAdReplyflowchart TD
A[Receive WhatsApp message with externalAdReply] --> B[Extract externalAdReply from msg and msg.message variants]
B --> C[Build adsMessage with title, body, thumbnailUrl, sourceUrl, sourceId, sourceType, mediaType, mediaUrl]
C --> D[Call buildReferralAttributes to map adsMessage to referral]
D --> E[Initialize contentAttributes]
E --> F[Add replyToIds from getReplyToIds if present]
F --> G[Add referral to contentAttributes if referral exists]
G --> H{contentAttributes not empty?}
H -->|Yes| I[Append JSON stringified content_attributes to outgoing Chatwoot message]
H -->|No| J[Send Chatwoot message without content_attributes]
Flow diagram for resilient ads thumbnail download and processingflowchart TD
A[Detect adsMessage with thumbnailUrl] --> B[Attempt thumbnail download with axios.get]
B --> C{Download succeeds?}
C -->|No| D[Log warning Failed to download ads thumbnail]
D --> E[Skip thumbnail upload and continue message delivery]
C -->|Yes| F[Determine mimeType and generate file name]
F --> G[Create fileData buffer from response]
G --> H[Set processedBuffer = fileData]
H --> I[Try to read and resize image with Jimp.read and img.cover]
I --> J{Jimp processing succeeds?}
J -->|No| K[Log warning Failed to process ads thumbnail with Jimp, sending raw image]
K --> L[Use processedBuffer = original fileData]
J -->|Yes| M[Set processedBuffer from img.getBuffer]
L --> N[Create Readable stream from processedBuffer]
M --> N[Create Readable stream from processedBuffer]
N --> O[Attach thumbnail file to Chatwoot message and send]
File-Level Changes
Possibly linked issues
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/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts" line_range="2258-2262" />
<code_context>
if (isAdsMessage) {
- const imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
+ let imgBuffer;
+ try {
+ imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
+ } catch (error) {
+ this.logger.warn(`Failed to download ads thumbnail: ${error?.message || error}`);
+ return;
+ }
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid skipping message delivery when thumbnail download fails
With the early `return` in the catch block, any failure to fetch the ads thumbnail prevents the message (and its referral metadata) from being sent. This makes message delivery depend on thumbnail availability and can reduce reliability. Instead, log the error and proceed with sending the message without the thumbnail/attachment when the image fetch fails.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
… fails Previously, a failed thumbnail download or unresolved mimetype aborted the entire ad message send, silently dropping both the text content and the referral metadata. Now falls back to a text-only message via createMessage(), which gains an optional referral parameter to carry the attribution data through that path.
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.
What changed
Extracts
sourceId,sourceType,mediaTypeandmediaUrlfromexternalAdReply(WhatsApp click-to-WhatsApp ad replies) alongside the existingtitle/body/thumbnailUrl/sourceUrlfields, and exposes them as a normalizedreferralobject under the outgoing message'scontent_attributeswhen relaying to Chatwoot.Also hardens the ads-thumbnail pipeline: Chatwoot's ads-thumbnail image is frequently served as webp by Facebook, and Jimp's core package ships no webp codec. A failed decode previously threw an unhandled exception that aborted message delivery after the Chatwoot conversation shell had already been created — leaving an empty, orphaned conversation behind. Both the thumbnail download and the Jimp resize now fall back gracefully (sending the raw, unresized image) instead of failing the whole send.
How to test
externalAdReplycontext.content_attributes.referralincludessource_id,source_type,source_url,headline,body,media_type, andimage_urlwhere available in the source ad payload.Summary by Sourcery
Capture WhatsApp ad referral metadata and propagate it to Chatwoot while hardening ad thumbnail handling to avoid message delivery failures.
New Features:
Bug Fixes:
Enhancements: