mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-13 21:01:16 +08:00
Adds storage support for WhatsApp business-scoped user identifiers received from Meta Cloud API and Twilio WhatsApp webhooks. The change keeps existing phone-based behavior intact, stores BSUID and parent BSUID values as additional `contact_inboxes.source_id` rows for the same contact, and allows BSUID-only inbound messages to create contacts, conversations, and messages without requiring a phone number. Related: https://github.com/chatwoot/chatwoot/issues/13837 **What changed** - Extended WhatsApp source ID validation to accept regular BSUID and parent BSUID formats. - For Meta Cloud API, stores phone, `user_id`, and `parent_user_id` identifiers as contact inbox source IDs when they are present. - For Twilio WhatsApp, stores phone, `ExternalUserId`, and `ParentExternalUserId` identifiers as contact inbox source IDs while preserving the existing `whatsapp:` Twilio source ID shape. - Supports BSUID-only inbound messages by creating a contact, contact inbox, conversation, and message even when the phone number is missing. - Links phone-first and later BSUID-only messages to the same contact when the first payload contains both phone and BSUID. - Stores WhatsApp usernames in contact `additional_attributes`, matching existing social channel patterns. - Keeps existing phone-based outbound and new-conversation behavior unchanged for this milestone. **How to test** 1. Send a Meta Cloud webhook payload with both `wa_id` and `user_id`. 2. Verify Chatwoot creates or finds the phone `contact_inbox` and also creates a BSUID `contact_inbox` for the same contact. 3. Send a later Meta Cloud payload for the same user with only `user_id` / `from_user_id`. 4. Verify Chatwoot finds the BSUID `contact_inbox` and creates the inbound message without requiring a phone number. 5. Send a Twilio WhatsApp webhook with `From: whatsapp:+E164`, `ExternalUserId`, and optionally `ParentExternalUserId`. 6. Verify Chatwoot stores the Twilio phone and BSUID identifiers as `whatsapp:`-prefixed source IDs for the same contact. 7. Send a Twilio WhatsApp webhook where `From` is `whatsapp:<BSUID>` and there is no phone number. 8. Verify Chatwoot creates the contact, contact inbox, conversation, and message without a phone number. --------- Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com>
133 lines
6.8 KiB
Ruby
133 lines
6.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe ContactInbox do
|
|
describe 'pubsub_token' do
|
|
let(:contact_inbox) { create(:contact_inbox) }
|
|
|
|
it 'gets created on object create' do
|
|
obj = contact_inbox
|
|
expect(obj.pubsub_token).not_to be_nil
|
|
end
|
|
|
|
it 'does not get updated on object update' do
|
|
obj = contact_inbox
|
|
old_token = obj.pubsub_token
|
|
obj.update(source_id: '234234323')
|
|
expect(obj.pubsub_token).to eq(old_token)
|
|
end
|
|
|
|
it 'backfills pubsub_token on call for older objects' do
|
|
obj = create(:contact_inbox)
|
|
# to replicate an object with out pubsub_token
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
obj.update_column(:pubsub_token, nil)
|
|
# rubocop:enable Rails/SkipsModelValidations
|
|
|
|
obj.reload
|
|
|
|
# ensure the column is nil in database
|
|
results = ActiveRecord::Base.connection.execute('Select * from contact_inboxes;')
|
|
expect(results.first['pubsub_token']).to be_nil
|
|
|
|
new_token = obj.pubsub_token
|
|
obj.update(source_id: '234234323')
|
|
# the generated token shoul be persisted in db
|
|
expect(obj.pubsub_token).to eq(new_token)
|
|
end
|
|
end
|
|
|
|
describe 'validations' do
|
|
context 'when source_id' do
|
|
it 'allows source_id longer than 255 characters for channels without format restrictions' do
|
|
long_source_id = 'a' * 300
|
|
email_inbox = create(:inbox, channel: create(:channel_email))
|
|
contact = create(:contact, account: email_inbox.account)
|
|
contact_inbox = build(:contact_inbox, contact: contact, inbox: email_inbox, source_id: long_source_id)
|
|
|
|
expect(contact_inbox.valid?).to be(true)
|
|
expect { contact_inbox.save! }.not_to raise_error
|
|
expect(contact_inbox.reload.source_id).to eq(long_source_id)
|
|
expect(contact_inbox.source_id.length).to eq(300)
|
|
end
|
|
|
|
it 'validates whatsapp channel source_id' do
|
|
whatsapp_inbox = create(:channel_whatsapp, sync_templates: false, validate_provider_config: false).inbox
|
|
contact = create(:contact)
|
|
valid_source_id = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: '1234567890')
|
|
valid_bsuid_source_id = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: 'IN.2081978709342942')
|
|
valid_parent_bsuid_source_id = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: 'IN.ENT.9081726354')
|
|
ci_character_in_source_id = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: '1234567890aaa')
|
|
ci_plus_in_source_id = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: '+1234567890')
|
|
expect(valid_source_id.valid?).to be(true)
|
|
expect(valid_bsuid_source_id.valid?).to be(true)
|
|
expect(valid_parent_bsuid_source_id.valid?).to be(true)
|
|
expect(ci_character_in_source_id.valid?).to be(false)
|
|
expect(ci_character_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for whatsapp inbox. valid Regex #{RegexHelper::WHATSAPP_CHANNEL_REGEX}"]
|
|
)
|
|
expect(ci_plus_in_source_id.valid?).to be(false)
|
|
expect(ci_plus_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for whatsapp inbox. valid Regex #{RegexHelper::WHATSAPP_CHANNEL_REGEX}"]
|
|
)
|
|
end
|
|
|
|
it 'validates twilio sms channel source_id' do
|
|
twilio_sms_inbox = create(:channel_twilio_sms).inbox
|
|
contact = create(:contact)
|
|
valid_source_id = build(:contact_inbox, contact: contact, inbox: twilio_sms_inbox, source_id: '+1234567890')
|
|
ci_character_in_source_id = build(:contact_inbox, contact: contact, inbox: twilio_sms_inbox, source_id: '+1234567890aaa')
|
|
ci_without_plus_in_source_id = build(:contact_inbox, contact: contact, inbox: twilio_sms_inbox, source_id: '1234567890')
|
|
expect(valid_source_id.valid?).to be(true)
|
|
expect(ci_character_in_source_id.valid?).to be(false)
|
|
expect(ci_character_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for twilio sms inbox. valid Regex #{RegexHelper::TWILIO_CHANNEL_SMS_REGEX}"]
|
|
)
|
|
expect(ci_without_plus_in_source_id.valid?).to be(false)
|
|
expect(ci_without_plus_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for twilio sms inbox. valid Regex #{RegexHelper::TWILIO_CHANNEL_SMS_REGEX}"]
|
|
)
|
|
end
|
|
|
|
it 'validates twilio whatsapp channel source_id' do
|
|
twilio_whatsapp_inbox = create(:channel_twilio_sms, medium: :whatsapp).inbox
|
|
contact = create(:contact)
|
|
valid_source_id = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox, source_id: 'whatsapp:+1234567890')
|
|
valid_bsuid_source_id = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox, source_id: 'whatsapp:IN.2081978709342942')
|
|
valid_parent_bsuid_source_id = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox, source_id: 'whatsapp:IN.ENT.9081726354')
|
|
ci_character_in_source_id = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox, source_id: 'whatsapp:+1234567890aaa')
|
|
ci_without_plus_in_source_id = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox, source_id: 'whatsapp:1234567890')
|
|
expect(valid_source_id.valid?).to be(true)
|
|
expect(valid_bsuid_source_id.valid?).to be(true)
|
|
expect(valid_parent_bsuid_source_id.valid?).to be(true)
|
|
expect(ci_character_in_source_id.valid?).to be(false)
|
|
expect(ci_character_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for twilio whatsapp inbox. valid Regex #{RegexHelper::TWILIO_CHANNEL_WHATSAPP_REGEX}"]
|
|
)
|
|
expect(ci_without_plus_in_source_id.valid?).to be(false)
|
|
expect(ci_without_plus_in_source_id.errors.full_messages).to eq(
|
|
["Source invalid source id for twilio whatsapp inbox. valid Regex #{RegexHelper::TWILIO_CHANNEL_WHATSAPP_REGEX}"]
|
|
)
|
|
end
|
|
|
|
it 'rejects whatsapp BSUID source_id values longer than 128 alphanumeric characters' do
|
|
whatsapp_inbox = create(:channel_whatsapp, sync_templates: false, validate_provider_config: false).inbox
|
|
contact = create(:contact)
|
|
contact_inbox = build(:contact_inbox, contact: contact, inbox: whatsapp_inbox, source_id: "IN.#{'1' * 129}")
|
|
|
|
expect(contact_inbox.valid?).to be(false)
|
|
end
|
|
|
|
it 'rejects twilio whatsapp parent BSUID source_id values longer than 128 alphanumeric characters' do
|
|
twilio_whatsapp_inbox = create(:channel_twilio_sms, medium: :whatsapp).inbox
|
|
contact = create(:contact)
|
|
contact_inbox = build(:contact_inbox, contact: contact, inbox: twilio_whatsapp_inbox,
|
|
source_id: "whatsapp:IN.ENT.#{'1' * 129}")
|
|
|
|
expect(contact_inbox.valid?).to be(false)
|
|
end
|
|
end
|
|
end
|
|
end
|