fix(baileys): preserve Meta Ads webhook messages - #2673
Draft
cardosolucass96 wants to merge 3 commits into
Draft
Conversation
Contributor
Reviewer's GuideRefactors Baileys WhatsApp message upsert handling to preserve Meta Ads/CTWA webhook messages (even without resolved phone numbers), enrich webhook payloads with campaign metadata and LID info, and isolate per-message failures within batches while adding better error handling and media/webhook processing logic. Sequence diagram for Meta Ads message upsert and webhook preservationsequenceDiagram
participant BaileysStartupService
participant PhoneResolver as resolvePhoneNumber
participant Webhook as sendDataWebhook
participant DB as prismaRepository
participant S3 as s3Service
participant Chatbot as chatbotController
BaileysStartupService->>BaileysStartupService: prepareMessage(received)
BaileysStartupService->>BaileysStartupService: getCampaignMetadata(messageRaw.contextInfo)
BaileysStartupService->>PhoneResolver: resolvePhoneNumber(messageRaw.key)
PhoneResolver-->>BaileysStartupService: phoneNumber or null
BaileysStartupService->>BaileysStartupService: createWebhookMessage()
BaileysStartupService->>Webhook: sendDataWebhook(Events.MESSAGES_UPSERT, webhookMessage)
alt isMetaAdsMessage
Note over BaileysStartupService,Webhook: Webhook sent early, before DB/media/chatbot
else nonMetaAdsMessage
BaileysStartupService->>Webhook: sendDataWebhook(Events.MESSAGES_UPSERT, webhookMessage)
end
BaileysStartupService->>DB: message.create(messageData)
BaileysStartupService->>S3: uploadFile(fullName, buffer, size)
BaileysStartupService->>DB: media.create({ messageId, instanceId, type, fileName })
BaileysStartupService->>Chatbot: emit({ instance, remoteJid, msg, pushName })
BaileysStartupService->>Webhook: sendDataWebhook(Events.CONTACTS_UPDATE | CONTACTS_UPSERT, contactRaw)
Sequence diagram for per-message error isolation and placeholder resendsequenceDiagram
participant BaileysStartupService
participant Client as client
participant Logger as logger
loop for each received in messages
BaileysStartupService->>BaileysStartupService: [per-message try]
alt messageStubParameters indicate decryption error
BaileysStartupService->>Client: requestPlaceholderResend(received.key)
Client-->>BaileysStartupService: resendId
BaileysStartupService->>Logger: warn("requested resend", keyId, resendId)
BaileysStartupService-->>BaileysStartupService: continue to next message
else normal processing
BaileysStartupService->>BaileysStartupService: full message handling (DB, media, webhook, chatbot)
end
BaileysStartupService->>Logger: error on per-message failure
end
BaileysStartupService->>Logger: error on outer handler failure
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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
messages.upserteven when the phone number cannot be resolved.campaignobject with Meta/CTWA identifiers and ad reply metadata.Why
Meta Ads conversations can arrive through Baileys with an
@lidJID and without a phone number orremoteJidAlt. The previous processing path could discard the message before the webhook reached the CRM workflow.Validation
npm run lint:checknpm run buildgit diff --checkThis PR does not add payload persistence or a retry queue.
Summary by Sourcery
Improve WhatsApp Baileys message upsert handling to reliably surface Meta Ads conversations and harden per-message processing against failures.
New Features:
Bug Fixes:
Enhancements: