Fixes https://github.com/chatwoot/chatwoot/issues/13097
### Problem
The PR #12176 removed the `before_save :setup_webhooks` callback to fix
a race condition where Meta's webhook verification request arrived
before the channel was saved to the database. This change broke manual
WhatsApp Cloud channel setup. While embedded signup explicitly calls
`channel.setup_webhooks` in `EmbeddedSignupService`, manual setup had no
equivalent call - meaning the `subscribed_apps` endpoint was never
invoked and Meta never sent webhook events to Chatwoot.
### Solution
Added an `after_commit` callback that triggers webhook setup for manual
WhatsApp Cloud channels
## Reference
https://github.com/chatwoot/chatwoot/pull/12149#issuecomment-3178108388
## Description
setup_webhook was done before the save, and hence the meta webhook
validation might fail because of a race condition where the facebook
validation is done before we saving the entry to the database.
## Type of change
Please delete options that are not relevant.
- [ ] Bug fix (non-breaking change which fixes an issue)
## How Has This Been Tested?
- New inbox creation, webhook validation
- Existing inbox update, webhook validation
-
<img width="614" height="674" alt="image"
src="https://github.com/user-attachments/assets/be223945-deed-475a-82e5-3ae9c54a13fa"
/>
## Checklist:
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
---------
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
# Pull Request Template
## Description
Please include a summary of the change and issue(s) fixed. Also, mention
relevant motivation, context, and any dependencies that this change
requires.
Fixes # (issue)
## Type of change
Please delete options that are not relevant.
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality not to work as expected)
- [ ] This change requires a documentation update
## How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.
## Checklist:
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
---------
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
## Description
This PR introduces WhatsApp Embedded Signup functionality, enabling
users to connect their WhatsApp Business accounts through Meta's
streamlined OAuth flow without manual webhook configuration. This
significantly improves the user experience by automating the entire
setup process.
**Key Features:**
- Embedded signup flow using Facebook SDK and Meta's OAuth 2.0
- Automatic webhook registration and phone number configuration
- Enhanced provider selection UI with card-based design
- Real-time progress tracking during signup process
- Comprehensive error handling and user feedback
## Required Configuration
The following environment variables must be configured by administrators
before this feature can be used:
Super Admin Configuration (via
super_admin/app_config?config=whatsapp_embedded)
- `WHATSAPP_APP_ID`: The Facebook App ID for WhatsApp Business API
integration
- `WHATSAPP_CONFIGURATION_ID`: The Configuration ID for WhatsApp
Embedded Signup flow (obtained from Meta Developer Portal)
- `WHATSAPP_APP_SECRET`: The App Secret for WhatsApp Embedded Signup
flow (required for token exchange)

## How Has This Been Tested?
#### Backend Tests (RSpec):
- Authentication validation for embedded signup endpoints
- Authorization code validation and error handling
- Missing business parameter validation
- Proper response format for configuration endpoint
- Unauthorized access prevention
#### Manual Test Cases:
- Complete embedded signup flow (happy path)
- Provider selection UI navigation
- Facebook authentication popup handling
- Error scenarios (cancelled auth, invalid business data, API failures)
- Configuration presence/absence behavior
## Related Screenshots:





Fixes
https://linear.app/chatwoot/issue/CW-2131/spec-for-whatsapp-cloud-channels-sign-in-with-facebook
---------
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
* feat: add received messages handling in WhatsApp services
* feat: ensure received messages are processed in the conversation
* tests: add received messages specs
* tests: add spec for call provider received_messages after save incoming message
* test: add spec to prevent calling received_messages for outgoing messages
* feat: add received_messages to error handling methods
* test: add spec for sending received messages request in WhatsappBaileysService
* fix: update fromMe flag in WhatsappBaileysService
* test: add spec to verify received_messages is not called for protocol messages
* test: improve received_messages stub
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
* fix: fill whatsapp provider url with value from inbox provider config
* chore: compact blank on setup channel provider
* fix: deadlock on `reconnecting` state
* feat: add store in additional_attributes to conversation model
* feat: set default mark_as_read to true in additional_attributes for conversation params
* fix: remove mark_as_read from additional_attributes in conversation
* refactor: move message read logic to mark_messages_as_read method in ConversationsController
* feat: add 'Mark messages as read' option to WhatsApp channel settings
* feat: add 'mark_as_read' option in factorie for WhatsApp channel
* feat: integrate Checkbox component for 'mark as read' option in WhatsApp settings
* fix: ensure 'mark_as_read' option is included in channel creation payload
* feat: add 'Mark as read' settings for WhatsApp inbox, including UI and state management
* chore: remove redundant content update test from IncomingMessageBaileysService spec
* feat: update 'Mark as read' label to 'Read receipts' and replace Checkbox with Switch component in WhatsApp settings
* fix: handle potential nil value for 'mark_as_read' in provider config
* feat: refactor provider config to streamline channel creation with 'mark_as_read' option
* feat: add test for MESSAGE_READ event dispatch in update_last_seen action
* fix: update subheader for 'Mark as read' setting to read receipt behavior in WhatsApp
* feat: update mark_messages_as_read behavior to handle false value in provider config
* chore: update label for 'Mark as read' option
* feat: update update_last_seen to send WhatsApp read receipt for WhatsApp channels
* test: remove MESSAGE_READ event dispatch tests in wrong file
* feat: enhance update_last_seen behavior for WhatsApp channel to conditionally dispatch messages.read event
* feat: update update_last_seen to dispatch messages.read event
* test: refactor update_last_seen tests
* chore: refactor to ensure provider_service responds to read_messages before ensure the mark_as_read provider config
* test: enhance #read_messages with provider config mark_as_read expected behaviors
* chore: clarify test names and remove useless expect
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
* fix: correct structure of lastMessages in message content
* fix: update send_message to store external_created_at timestamp with result from request
* fix: filter incoming messages for unread conversation logic
* fix: improve extraction of message timestamp in send_message method
* feat: enhance message timestamp extraction and update logic in send_message method
* refactor: send_message to improve error handling and update external_created_at timestamp
* chore: move message timestamp extraction to helper
* fix: use message timestamp extract on incoming
* fix: update unread_conversation to handle last message correctly for Baileys provider
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
* feat: add MESSAGES_UNREAD event and implement unread action in ConversationsController
* feat: rename MESSAGES_UNREAD event to CONVERSATION_UNREAD and update unread action in ConversationsController
* feat: implement send_unread_conversation method and update conversation_unread event handling
* feat: add test for dispatching conversation.unread event in ConversationsController
* feat: add conversation_unread event handling in ChannelListener
* feat: add send_unread_conversation method and corresponding tests in Whatsapp channel
* feat: add logging for nil last_message in send_unread_conversation method
* feat: add send_unread_conversation to error handling methods
* feat: implement send_unread_conversation method with logging for nil last_message
* fix: add nil check and logging for last_message in send_unread_conversation method
* fix: update conversation_unread method to call unread_conversation instead of send_unread_conversation
* refactor: rename send_unread_conversation to unread_conversation and update method implementation
* refactor: rename send_unread_conversation to send_unread_messages and update implementation to handle multiple messages
* refactor: rename send_read_messages to read_messages and send_unread_messages to unread_message for clarity
* refactor: rename send_read_messages to read_messages and send_unread_messages to unread_message for clarity
* refactor: rename send_read_messages to read_messages for consistency
* feat: implement send_read_messages method for WhatsApp channel
* feat: implement messages_read event handling and dispatch for conversations
* feat: enhance messages_read handling to include last_seen_at and conversation context
* feat: update last_seen handling to reference agent_last_seen_at for messages read event
* chore: fix rebase
* feat: update error handling
* feat: update send_read_messages to mark received messages as not sent by the user
* test: controller spec
* test: channel listener
* test: channel and provider
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
* fix: return not_found status for missing messages in WhatsApp webhook
* feat: enhance message handling to support image attachments
* chore: Handle incoming whatsapp baileys service attachments and implement specs for images and videos
* fix: add file_content_type method to incoming baileys messages
* chore: specs for stickers, audio and files
* fix: handle media attachment errors
* chore: convert file_content_type to string when attaching media
* fix: add handling for attachment not found error in incoming message service
* chore: refactor file_content_type method and simplify filename method
* chore: update media attachment tests
* feat: baileys unsupported message alert (#27)
* feat: create alert message for unsupported message types
* chore: fail message when try send not supported type in baileys
* chore: add tests for unsupported message handling in Baileys service
* chore: correct spelling
* chore: NIT in spec name
* chore: remove unnecessary message reload in unsupported message type test
* feat: baileys support to send attachments (#28)
* feat: enhance message sending logic with support for attachments and interactive messages
* fix: update message format to use messageContent for text messages
* feat: attachment message sending
* fix: use strict encoding for attachment file download
* chore: streamline message sending logic and remove unused error classes
* chore: remove type from message sending logic
* chore: update message sending specs
* chore: raise MessageNotSentError instead of updating message status to failed
* chore: change baileys contact name preferences (#30)
* refactor: improve contact name handling and extraction from JID
* test: enhance message handling to update contact names based on pushName and verifiedBizName
* chore: update contact name condition to match phone number from JID
* chore: correct method name typo
* chore: correct variable names for phone number consistency in tests
* chore: update message payload structure and improve error handling in send_message
* test: re-add testing for error handling
* feat: enhance contact name handling and add self-message detection
* fix: ensure presence check for verified business name in contact name retrieval
* test: improve specs
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
Co-authored-by: Gabriel Jablonski <gabriel@fazer.ai>
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
Co-authored-by: Gabriel Jablonski <gabriel@fazer.ai>
* fix: try to disconnect baileys before destroy inbox (#29)
* fix: ensure proper disconnection of Baileys provider on channel destruction
* test: add callback tests for disconnecting channel provider in Whatsapp spec
* refactor: simplify condition for disconnecting Baileys provider on channel destruction
* refactor: enhance disconnect_channel_provider specs for Baileys provider
* test: verify channel destruction does not call disconnect_channel_provider
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
Co-authored-by: Gabriel Jablonski <gabriel@fazer.ai>
* feat: baileys provider and placeholder for link device modal
* chore: drop qrcode.vue in favor of just img tag
* chore: update modal props
* feat: setup channel provider connection
* chore: update .env.example with Baileys API default configuration
* feat: add support for Baileys provider in WhatsApp events processing
* chore: rename Baileys API default host variable to DEFAULT_BAILEYS_URL
* feat: add setup and disconnect methods for Baileys channel provider in inboxes controller that will be implemented
* feat: add CHANNEL_CONNECTION_UPDATE event and include it in broadcast data preparation
* refactor: simplify channel retrieval logic in WhatsappEventsJob
* refactor: revert CHANNEL_UPDATE_EVENTS constant from ActionCableBroadcastJob
* feat: add 'baileys' as a provider option in Whatsapp channel model
* feat: add provider_connection field to Whatsapp channel model and migration
* refactor: remove unnecessary CHANNEL_CONNECTION_UPDATE event type
* feat: implement channel provider connection with baileys API
* feat: add inbox association to Whatsapp channel model and update webhook URL handling
* feat: enhance Baileys service to handle webhook multiple event types
* refactor: simplify webhook verification logic in Baileys service
* feat: add setup channel provider call, and refactor some logic
* chore: adapt logic to new API
* refactor: fix typo
* refactor: fix import
* refactor: fix typo
* chore: add fixme comment about race condition
* fix: remove double disconnect call
* feat: implement message processing for incoming WhatsApp messages
* refactor: streamline message type determination and improve readability
* chore: increase cache key granularity
provider connection info might be updated multiple times within 1 second, so updates might be lost due to cache key not being updated. changing cache key to milliseconds solves this
* feat: add `is-loading` to buttons
* feat: update send_message method to use 'to' parameter and improve error handling
* refactor: simplify test setup and update API key in specs
* chore: add setup and disconnect channel provider specs
* test: fix spec after increase cache key granularity
* feat: handle reconnecting state on modal
* style: centered error text
* feat: advanced options on create inbox
* feat: handle new reconnecting on backend
* refactor: update inbox controller specs and leave a FIXME note
* test: add specs for Whatsapp::IncomingMessageBaileysService
* feat: add baileys configuration page
* feat: link device button when disconnected on conversation
* chore: refactor .env.example
* feat: add TODO for unimplemented methods in IncomingMessageBaileysService
* fix: correct method name and update environment variable references in WhatsappBaileysService
* refactor: simplify channel lookup by removing redundant method and handling phone number check directly
* chore: add TODO for unimplemented event processing methods in IncomingMessageBaileysService
* fix: update environment variable references in WhatsappBaileysService tests
* chore(webhook): add pt-BR translations
* chore: add pt-br translations
* chore: inboxname component margin
* refactor: inboxname computed prop
* feat: enhance WhatsApp provider connection handling and message processing
* test: inbox controller
* chore: improve baileys connection and messages handling
* test: incoming message service baileys
* refactor: update provider config validation and improve test setup for WhatsApp Baileys service
* fix: ensure only text messages are sent and update message source ID
* fix: create message
* fix: only update message on success
* test: fix broken specs
* chore: raise error on unsupported message content type
* feat: hide provider connection data from non-admins
* fix: update advanced options
* chore: move class definition
* fix: issue with send_message not returning id
---------
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>