Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

fix(voice): accept incoming twilio calls with withheld caller IDs (#16328)

Muhsin Keloth39314c979aa4
diff --git a/app/javascript/dashboard/components-next/message/bubbles/VoiceCall.vue b/app/javascript/dashboard/components-next/message/bubbles/VoiceCall.vue
index f0d1304b410..b1c00c4a336 100644
--- a/app/javascript/dashboard/components-next/message/bubbles/VoiceCall.vue
+++ b/app/javascript/dashboard/components-next/message/bubbles/VoiceCall.vue
@@ -263,6 +263,7 @@ const handleJoinCall = async () => {
const canCallBack = computed(
() =>
isMissedInbound.value &&
+ (isWhatsapp.value || !!sender.value?.phoneNumber) &&
!!inboxId.value &&
!!conversationId.value &&
!hasActiveCall.value &&
diff --git a/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue b/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue
index 903cbc1a458..f4025a06d2f 100644
--- a/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue
+++ b/app/javascript/dashboard/components/widgets/conversation/MessagesView.vue
@@ -551,7 +551,13 @@ export default {
:href-link-text="$t('CONVERSATION.INSTAGRAM_RESTRICTION_STATUS_LINK')"
/>
<Banner
- v-if="!currentChat.can_reply"
+ v-if="currentChat.anonymous_caller"
+ color-scheme="alert"
+ class="mx-2 mt-2 overflow-hidden rounded-lg"
+ :banner-message="$t('CONVERSATION.ANONYMOUS_CALLER_CANNOT_REPLY')"
+ />
+ <Banner
+ v-else-if="!currentChat.can_reply"
color-scheme="alert"
class="mx-2 mt-2 overflow-hidden rounded-lg"
:banner-message="replyWindowBannerMessage"
diff --git a/app/javascript/dashboard/i18n/locale/en/conversation.json b/app/javascript/dashboard/i18n/locale/en/conversation.json
index d26d82a8325..3e2c3abc234 100644
--- a/app/javascript/dashboard/i18n/locale/en/conversation.json
+++ b/app/javascript/dashboard/i18n/locale/en/conversation.json
@@ -52,6 +52,7 @@
"CLICK_HERE": "Click here",
"LOADING_INBOXES": "Loading inboxes",
"LOADING_CONVERSATIONS": "Loading Conversations",
+ "ANONYMOUS_CALLER_CANNOT_REPLY": "Messages cannot be sent because this caller’s phone number is unavailable.",
"CANNOT_REPLY": "You cannot reply due to",
"24_HOURS_WINDOW": "24 hour message window restriction",
"48_HOURS_WINDOW": "48 hour message window restriction",
diff --git a/app/models/contact_inbox.rb b/app/models/contact_inbox.rb
index 893ab87f3c5..349956cd5ed 100644
--- a/app/models/contact_inbox.rb
+++ b/app/models/contact_inbox.rb
@@ -51,6 +51,10 @@ class ContactInbox < ApplicationRecord
}
end
+ def anonymous_caller?
+ false
+ end
+
def current_conversation
conversations.last
end
@@ -77,3 +81,5 @@ class ContactInbox < ApplicationRecord
validate_whatsapp_source_id if inbox.channel_type == 'Channel::Whatsapp'
end
end
+
+ContactInbox.prepend_mod_with('ContactInbox')
diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
index fb1fdf390ce..fd66a0419c1 100644
--- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
+++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder
@@ -48,6 +48,7 @@ json.additional_attributes conversation.additional_attributes
json.agent_last_seen_at conversation.agent_last_seen_at.to_i
json.assignee_last_seen_at conversation.assignee_last_seen_at.to_i
json.can_reply conversation.can_reply?
+json.anonymous_caller conversation.contact_inbox.anonymous_caller?
json.contact_last_seen_at conversation.contact_last_seen_at.to_i
json.custom_attributes conversation.custom_attributes
json.inbox_id conversation.inbox_id
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 4805f361524..68d7cb184db 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -124,6 +124,7 @@ en:
sso_not_enabled: SAML SSO is not enabled for this installation
multi_account_invitation_not_allowed: Users associated with another account cannot be added while SAML is enabled
conversations:
+ anonymous_caller_reply: 'Messages cannot be sent because this caller’s phone number is unavailable.'
resolved: Conversation is resolved and does not accept new messages
unread_counts:
feature_not_enabled: Conversation unread counts feature not enabled for this account
@@ -357,6 +358,7 @@ en:
request_contact_info: 'Would you like to share your phone number with us?'
unsupported_message: 'This message is unavailable.'
voice_call:
+ unknown_caller: 'Unknown caller'
twilio: 'Voice Call'
whatsapp: 'WhatsApp Call'
delivery_status:
diff --git a/enterprise/app/controllers/api/v1/accounts/contacts/calls_controller.rb b/enterprise/app/controllers/api/v1/accounts/contacts/calls_controller.rb
index 740a2fda449..15276c37b75 100644
--- a/enterprise/app/controllers/api/v1/accounts/contacts/calls_controller.rb
+++ b/enterprise/app/controllers/api/v1/accounts/contacts/calls_controller.rb
@@ -5,6 +5,7 @@ class Api::V1::Accounts::Contacts::CallsController < Api::V1::Accounts::BaseCont
def create
authorize contact, :show?
authorize voice_inbox, :show?
+ return render_could_not_create_error(I18n.t('errors.whatsapp.calls.contact_phone_required')) if contact.phone_number.blank?
call = Voice::OutboundCallBuilder.perform!(
account: Current.account,
diff --git a/enterprise/app/controllers/twilio/voice_controller.rb b/enterprise/app/controllers/twilio/voice_controller.rb
index 9b7b44c3df3..bf5f3e78766 100644
--- a/enterprise/app/controllers/twilio/voice_controller.rb
+++ b/enterprise/app/controllers/twilio/voice_controller.rb
@@ -115,7 +115,7 @@ class Twilio::VoiceController < ApplicationController
Voice::InboundCallBuilder.perform!(
inbox: inbox,
call_sid: twilio_call_sid,
- caller: { source_ids: [twilio_from], contact_attributes: { name: twilio_from, phone_number: twilio_from } }
+ caller: Voice::Provider::Twilio::InboundCaller.new(from: twilio_from, call_sid: twilio_call_sid, account: current_account).build
)
when 'outbound-api', 'outbound-dial'
sync_outbound_leg(call_sid: twilio_call_sid, direction: twilio_direction)
diff --git a/enterprise/app/models/enterprise/contact_inbox.rb b/enterprise/app/models/enterprise/contact_inbox.rb
new file mode 100644
index 00000000000..40606ef28f0
--- /dev/null
+++ b/enterprise/app/models/enterprise/contact_inbox.rb
@@ -0,0 +1,15 @@
+module Enterprise::ContactInbox
+ ANONYMOUS_CALL_SOURCE_ID = /\Aanonymous:CA[0-9a-f]{32}\z/i
+
+ def anonymous_caller?
+ inbox.channel_type == 'Channel::TwilioSms' && ANONYMOUS_CALL_SOURCE_ID.match?(source_id.to_s)
+ end
+
+ private
+
+ def validate_twilio_source_id
+ return if inbox.channel.voice_enabled? && anonymous_caller?
+
+ super
+ end
+end
diff --git a/enterprise/app/models/enterprise/conversation.rb b/enterprise/app/models/enterprise/conversation.rb
index c2647c8aa1f..9a7bd04a205 100644
--- a/enterprise/app/models/enterprise/conversation.rb
+++ b/enterprise/app/models/enterprise/conversation.rb
@@ -1,6 +1,12 @@
module Enterprise::Conversation
attr_accessor :captain_activity_reason, :captain_activity_reason_type
+ def can_reply?
+ return false if contact_inbox.anonymous_caller?
+
+ super
+ end
+
def list_of_keys
super + %w[sla_policy_id]
end
diff --git a/enterprise/app/models/enterprise/message.rb b/enterprise/app/models/enterprise/message.rb
index 5e4a1600069..fbf8bf0cc0b 100644
--- a/enterprise/app/models/enterprise/message.rb
+++ b/enterprise/app/models/enterprise/message.rb
@@ -3,6 +3,8 @@ module Enterprise::Message
base.class_eval do
has_one :call, class_name: 'Call', foreign_key: :message_id, dependent: :nullify, inverse_of: :message
+ validate :validate_anonymous_caller_reply, on: :create
+
scope :with_call, -> { includes(call: [:contact, { inbox: :channel }]) }
# Scheduling and freshness checks must share this scope so an email auto reply cannot cancel a pending response.
scope :captain_response_triggering, lambda {
@@ -29,6 +31,13 @@ module Enterprise::Message
private
+ def validate_anonymous_caller_reply
+ return if private? || voice_call? || (!outgoing? && !template?)
+ return unless conversation.contact_inbox.anonymous_caller?
+
+ errors.add(:base, I18n.t('errors.conversations.anonymous_caller_reply'))
+ end
+
def reopen_resolved_conversation
assistant = conversation.inbox.captain_assistant
diff --git a/enterprise/app/services/voice/provider/twilio/inbound_caller.rb b/enterprise/app/services/voice/provider/twilio/inbound_caller.rb
new file mode 100644
index 00000000000..62c2326dbb6
--- /dev/null
+++ b/enterprise/app/services/voice/provider/twilio/inbound_caller.rb
@@ -0,0 +1,21 @@
+class Voice::Provider::Twilio::InboundCaller
+ # Twilio can encode withheld caller-ID labels as digits, e.g. ANONYMOUS becomes 266696687.
+ # These are caller-ID placeholders, not callable numbers; 464 represents an empty or missing caller ID.
+ # https://help.twilio.com/articles/223179988-Why-am-I-getting-calls-from-these-strange-numbers-
+ WITHHELD_NUMBERS = %w[266696687 86282452253 8656696 2562533 7378742833 464].freeze
+
+ pattr_initialize [:from!, :call_sid!, :account!]
+
+ def build
+ # Match E.164-style numbers: '+' followed by 2-15 digits, with no leading zero (e.g. +14155550101).
+ # Exclude Twilio's numeric withheld-ID placeholders even when they match this format.
+ if from.match?(/\A\+[1-9]\d{1,14}\z/) && WITHHELD_NUMBERS.exclude?(from.delete_prefix('+'))
+ return { source_ids: [from], contact_attributes: { name: from, phone_number: from } }
+ end
+
+ {
+ source_ids: ["anonymous:#{call_sid}"],
+ contact_attributes: { name: I18n.t('conversations.messages.voice_call.unknown_caller', locale: account.locale) }
+ }
+ end
+end
diff --git a/spec/enterprise/controllers/twilio/voice_controller_spec.rb b/spec/enterprise/controllers/twilio/voice_controller_spec.rb
index ce12598511a..2c6e88515a7 100644
--- a/spec/enterprise/controllers/twilio/voice_controller_spec.rb
+++ b/spec/enterprise/controllers/twilio/voice_controller_spec.rb
@@ -51,6 +51,43 @@ RSpec.describe 'Twilio::VoiceController', type: :request do
expect(response.body).to include(call.conference_sid)
end
+ context 'when the caller withholds their number' do
+ let(:call_sid) { "CA#{SecureRandom.hex(16)}" }
+ let(:from_number) { 'anonymous' }
+
+ it 'answers with conference TwiML and a phone-less contact' do
+ post "/twilio/voice/call/#{digits}", params: inbound_call_params
+
+ call = Call.find_by!(provider_call_id: call_sid)
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include('<Dial>', call.conference_sid)
+ expect(call.contact).to have_attributes(name: 'Unknown caller', phone_number: nil)
+ expect(call.conversation.can_reply?).to be false
+ end
+
+ it 'creates separate contacts and conversations for separate calls' do
+ post "/twilio/voice/call/#{digits}", params: inbound_call_params
+ first = Call.find_by!(provider_call_id: call_sid)
+ second_sid = "CA#{SecureRandom.hex(16)}"
+
+ post "/twilio/voice/call/#{digits}", params: inbound_call_params.merge('CallSid' => second_sid)
+ second = Call.find_by!(provider_call_id: second_sid)
+
+ expect(response).to have_http_status(:ok)
+ expect(second.contact).not_to eq(first.contact)
+ expect(second.conversation).not_to eq(first.conversation)
+ end
+
+ it 'reuses the call, contact and conversation when Twilio retries' do
+ post "/twilio/voice/call/#{digits}", params: inbound_call_params
+
+ expect do
+ post "/twilio/voice/call/#{digits}", params: inbound_call_params
+ end.not_to(change { [Call.count, Contact.count, Conversation.count] })
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
# An inbox that predates the setting carries no key in provider_config and must keep recording.
it 'records the conference from the start when the call was created with recording on' do
conversation = create(:conversation, account: account, inbox: inbox)
diff --git a/spec/enterprise/models/message_spec.rb b/spec/enterprise/models/message_spec.rb
index 5a0e815fcc3..68ca5c89082 100644
--- a/spec/enterprise/models/message_spec.rb
+++ b/spec/enterprise/models/message_spec.rb
@@ -24,6 +24,44 @@ RSpec.describe Message do
expect(conversation.waiting_since).to be_nil
end
+ describe 'anonymous caller replies' do
+ let(:channel) { create(:channel_twilio_sms, :with_voice) }
+ let(:contact_inbox) do
+ create(:contact_inbox, inbox: channel.inbox, source_id: "anonymous:CA#{SecureRandom.hex(16)}")
+ end
+ let(:conversation) do
+ create(:conversation, account: channel.account, inbox: channel.inbox, contact: contact_inbox.contact, contact_inbox: contact_inbox)
+ end
+
+ [:outgoing, :template].each do |message_type|
+ it "rejects public #{message_type} messages" do
+ message = build(:message, conversation: conversation, message_type: message_type)
+
+ expect(message).not_to be_valid
+ expect(message.errors[:base]).to include(I18n.t('errors.conversations.anonymous_caller_reply'))
+ end
+ end
+
+ it 'allows private notes' do
+ expect(build(:message, conversation: conversation, message_type: :outgoing, private: true)).to be_valid
+ end
+
+ it 'allows voice call messages' do
+ expect(build(:message, conversation: conversation, message_type: :outgoing, content_type: :voice_call)).to be_valid
+ end
+
+ it 'allows incoming messages' do
+ expect(build(:message, conversation: conversation, message_type: :incoming)).to be_valid
+ end
+
+ it 'allows public replies to numbered callers' do
+ contact_inbox.update!(source_id: '+14157047320')
+
+ expect(build(:message, conversation: conversation, message_type: :outgoing)).to be_valid
+ expect(conversation.can_reply?).to be true
+ end
+ end
+
describe '#mark_pending_conversation_as_open_for_human_response' do
let(:conversation) { create(:conversation, status: :pending) }
let(:captain_assistant) { create(:captain_assistant, account: conversation.account) }
diff --git a/spec/enterprise/services/voice/provider/twilio/inbound_caller_spec.rb b/spec/enterprise/services/voice/provider/twilio/inbound_caller_spec.rb
new file mode 100644
index 00000000000..724c73f77d4
--- /dev/null
+++ b/spec/enterprise/services/voice/provider/twilio/inbound_caller_spec.rb
@@ -0,0 +1,23 @@
+require 'rails_helper'
+
+RSpec.describe Voice::Provider::Twilio::InboundCaller do
+ let(:account) { create(:account) }
+ let(:call_sid) { "CA#{SecureRandom.hex(16)}" }
+
+ describe '#build' do
+ ['anonymous', '', 'restricted', 'unknown', '+266696687', '+86282452253', '+8656696', '+2562533', '+7378742833', '+464'].each do |from|
+ it "creates a phone-less caller for #{from.inspect}" do
+ caller = described_class.new(from: from, call_sid: call_sid, account: account).build
+
+ expect(caller).to eq(source_ids: ["anonymous:#{call_sid}"], contact_attributes: { name: 'Unknown caller' })
+ end
+ end
+
+ it 'preserves a normal caller phone number' do
+ from = '+14157047320'
+ caller = described_class.new(from: from, call_sid: call_sid, account: account).build
+
+ expect(caller).to eq(source_ids: [from], contact_attributes: { name: from, phone_number: from })
+ end
+ end
+end