Skip to content

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

fix: prevent multiple bot providers on an inbox (#16092)

Sojan Jose98a9ed710ad7
diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/components/BotConfiguration.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/components/BotConfiguration.vue
index a5ec9df37f2..318e0042b18 100644
--- a/app/javascript/dashboard/routes/dashboard/settings/inbox/components/BotConfiguration.vue
+++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/components/BotConfiguration.vue
@@ -61,7 +61,9 @@ export default {
});
useAlert(this.$t('AGENT_BOTS.BOT_CONFIGURATION.SUCCESS_MESSAGE'));
} catch (error) {
- useAlert(this.$t('AGENT_BOTS.BOT_CONFIGURATION.ERROR_MESSAGE'));
+ useAlert(
+ error.message || this.$t('AGENT_BOTS.BOT_CONFIGURATION.ERROR_MESSAGE')
+ );
}
},
async disconnectBot() {
diff --git a/app/models/agent_bot_inbox.rb b/app/models/agent_bot_inbox.rb
index ffcf483b4c7..82c69b98332 100644
--- a/app/models/agent_bot_inbox.rb
+++ b/app/models/agent_bot_inbox.rb
@@ -14,6 +14,7 @@
class AgentBotInbox < ApplicationRecord
validates :inbox_id, presence: true
validates :agent_bot_id, presence: true
+ validate :ensure_exclusive_bot_provider, if: :active?
before_validation :ensure_account_id
belongs_to :inbox
@@ -23,6 +24,11 @@ class AgentBotInbox < ApplicationRecord
private
+ def ensure_exclusive_bot_provider
+ provider = inbox&.conflicting_bot_provider(:agent_bot)
+ errors.add(:base, I18n.t('errors.inboxes.bot_provider_conflict', current_provider: provider, requested_provider: 'Agent Bot')) if provider
+ end
+
def ensure_account_id
self.account_id = inbox&.account_id
end
diff --git a/app/models/concerns/inbox_bot_status.rb b/app/models/concerns/inbox_bot_status.rb
index ba130bfcb57..7afe87af482 100644
--- a/app/models/concerns/inbox_bot_status.rb
+++ b/app/models/concerns/inbox_bot_status.rb
@@ -6,11 +6,23 @@ module InboxBotStatus
end
def external_bot_active?
- agent_bot_inbox&.active? || dialogflow_active?
+ agent_bot_active? || dialogflow_active?
+ end
+
+ def conflicting_bot_provider(provider)
+ return 'Agent Bot' if provider != :agent_bot && agent_bot_active?
+ return 'Dialogflow' if provider != :dialogflow && dialogflow_active?
+
+ nil
end
private
+ # Bot links can outlive their bot until async cleanup runs.
+ def agent_bot_active?
+ AgentBotInbox.active.joins(:agent_bot).exists?(inbox_id: id)
+ end
+
def dialogflow_active?
hooks.exists?(app_id: %w[dialogflow], status: 'enabled')
end
diff --git a/app/models/integrations/hook.rb b/app/models/integrations/hook.rb
index 54b6a08af1b..e8f2e423193 100644
--- a/app/models/integrations/hook.rb
+++ b/app/models/integrations/hook.rb
@@ -35,6 +35,7 @@ class Integrations::Hook < ApplicationRecord
if: :shopify?
validate :validate_settings_json_schema
validate :ensure_feature_enabled
+ validate :ensure_exclusive_bot_provider, if: -> { dialogflow? && enabled? }
validate :validate_openai_api_key, if: :validate_openai_api_key?
validate :validate_cloudflare_realtimekit_credentials, if: :validate_cloudflare_realtimekit_credentials?
validate :validate_google_translate_credentials, on: :create, if: :google_translate?
@@ -112,6 +113,11 @@ class Integrations::Hook < ApplicationRecord
private
+ def ensure_exclusive_bot_provider
+ provider = inbox&.conflicting_bot_provider(:dialogflow)
+ errors.add(:base, I18n.t('errors.inboxes.bot_provider_conflict', current_provider: provider, requested_provider: 'Dialogflow')) if provider
+ end
+
def ensure_feature_enabled
return if shopify? && disabled?
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 5fac6801896..134eeec581b 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -219,6 +219,7 @@ en:
openai:
invalid_api_key: 'OpenAI API key is invalid or revoked. Please check your key in your OpenAI dashboard.'
inboxes:
+ bot_provider_conflict: 'Disconnect %{current_provider} before connecting %{requested_provider}'
imap:
socket_error: Please check the network connection, IMAP address and try again.
no_response_error: Please check the IMAP credentials and try again.
diff --git a/enterprise/app/models/captain_inbox.rb b/enterprise/app/models/captain_inbox.rb
index 0b57b2d02d8..944027cb1b5 100644
--- a/enterprise/app/models/captain_inbox.rb
+++ b/enterprise/app/models/captain_inbox.rb
@@ -19,4 +19,12 @@ class CaptainInbox < ApplicationRecord
belongs_to :inbox
validates :inbox_id, uniqueness: true
+ validate :ensure_exclusive_bot_provider
+
+ private
+
+ def ensure_exclusive_bot_provider
+ provider = inbox&.conflicting_bot_provider(:captain)
+ errors.add(:base, I18n.t('errors.inboxes.bot_provider_conflict', current_provider: provider, requested_provider: 'Captain')) if provider
+ end
end
diff --git a/enterprise/app/models/enterprise/inbox.rb b/enterprise/app/models/enterprise/inbox.rb
index 3e156b31604..4817a2c21a2 100644
--- a/enterprise/app/models/enterprise/inbox.rb
+++ b/enterprise/app/models/enterprise/inbox.rb
@@ -16,6 +16,10 @@ module Enterprise::Inbox
captain_assistant.present? && more_responses?
end
+ def conflicting_bot_provider(provider)
+ super || ('Captain' if provider != :captain && CaptainInbox.joins(:captain_assistant).exists?(inbox_id: id))
+ end
+
private
def more_responses?
diff --git a/spec/enterprise/controllers/api/v1/accounts/captain/inboxes_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/captain/inboxes_controller_spec.rb
index 85d5617553b..e001db8c2ea 100644
--- a/spec/enterprise/controllers/api/v1/accounts/captain/inboxes_controller_spec.rb
+++ b/spec/enterprise/controllers/api/v1/accounts/captain/inboxes_controller_spec.rb
@@ -63,6 +63,17 @@ RSpec.describe 'Api::V1::Accounts::Captain::Inboxes', type: :request do
expect(json_response[:id]).to eq(inbox2.id)
end
+ it 'returns a conflict message when another inbox bot is connected' do
+ create(:agent_bot_inbox, inbox: inbox2)
+
+ post "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}/inboxes",
+ params: valid_params,
+ headers: admin.create_new_auth_token
+
+ expect(response).to have_http_status(:unprocessable_entity)
+ expect(json_response[:message]).to include('Disconnect Agent Bot before connecting Captain')
+ end
+
context 'when inbox does not exist' do
it 'returns not found status' do
post "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}/inboxes",
diff --git a/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb b/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb
index 52bd1bc2308..9b63209484c 100644
--- a/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb
+++ b/spec/enterprise/jobs/enterprise/account/conversations_resolution_scheduler_job_spec.rb
@@ -66,7 +66,9 @@ RSpec.describe Account::ConversationsResolutionSchedulerJob, type: :job do
it 'does not enqueue resolution jobs for inboxes with an external bot' do
regular_inbox = create(:inbox, account: account)
create(:captain_inbox, captain_assistant: assistant, inbox: regular_inbox)
- create(:agent_bot_inbox, inbox: regular_inbox, agent_bot: create(:agent_bot, account: account))
+ connection = create(:agent_bot_inbox, inbox: regular_inbox, agent_bot: create(:agent_bot, account: account), status: :inactive)
+ # Keep coverage for existing duplicate connections until they are eliminated.
+ connection.update_column(:status, AgentBotInbox.statuses[:active]) # rubocop:disable Rails/SkipsModelValidations
expect do
described_class.perform_now
diff --git a/spec/enterprise/models/inbox_spec.rb b/spec/enterprise/models/inbox_spec.rb
index a7a83de6b67..a14e9582e6c 100644
--- a/spec/enterprise/models/inbox_spec.rb
+++ b/spec/enterprise/models/inbox_spec.rb
@@ -157,6 +157,111 @@ RSpec.describe Inbox do
end
end
+ describe 'bot provider exclusivity' do
+ let(:account) { create(:account) }
+ let(:inbox) { create(:inbox, account: account) }
+ let(:agent_bot) { create(:agent_bot, account: account) }
+ let(:assistant) { create(:captain_assistant, account: account) }
+
+ context 'when an Agent Bot is connected' do
+ let!(:connection) { create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot) }
+
+ it 'rejects Captain and Dialogflow' do
+ captain_connection = build(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+ dialogflow_hook = build(:integrations_hook, :dialogflow, inbox: inbox, account: account)
+
+ expect(captain_connection).not_to be_valid
+ expect(captain_connection.errors[:base]).to include('Disconnect Agent Bot before connecting Captain')
+ expect(dialogflow_hook).not_to be_valid
+ expect(dialogflow_hook.errors[:base]).to include('Disconnect Agent Bot before connecting Dialogflow')
+ end
+
+ it 'allows Captain after the Agent Bot is disabled' do
+ connection.inactive!
+
+ expect(build(:captain_inbox, inbox: inbox, captain_assistant: assistant)).to be_valid
+ end
+
+ it 'ignores an Agent Bot connection whose bot was deleted' do
+ agent_bot.destroy!
+
+ expect(AgentBotInbox.exists?(inbox_id: inbox.id)).to be(true)
+ expect(inbox.reload.external_bot_active?).to be(false)
+ expect(build(:captain_inbox, inbox: inbox, captain_assistant: assistant)).to be_valid
+ end
+ end
+
+ context 'when Captain is connected' do
+ let!(:connection) { create(:captain_inbox, inbox: inbox, captain_assistant: assistant) }
+
+ it 'rejects Agent Bot and Dialogflow' do
+ agent_bot_connection = build(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
+ dialogflow_hook = build(:integrations_hook, :dialogflow, inbox: inbox, account: account)
+
+ expect(agent_bot_connection).not_to be_valid
+ expect(agent_bot_connection.errors[:base]).to include('Disconnect Captain before connecting Agent Bot')
+ expect(dialogflow_hook).not_to be_valid
+ expect(dialogflow_hook.errors[:base]).to include('Disconnect Captain before connecting Dialogflow')
+ end
+
+ it 'allows Dialogflow after Captain is disconnected' do
+ connection.destroy!
+
+ expect(build(:integrations_hook, :dialogflow, inbox: inbox, account: account)).to be_valid
+ end
+ end
+
+ context 'when Dialogflow is enabled' do
+ let!(:hook) { create(:integrations_hook, :dialogflow, inbox: inbox, account: account) }
+
+ it 'rejects Agent Bot and Captain' do
+ agent_bot_connection = build(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
+ captain_connection = build(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+
+ expect(agent_bot_connection).not_to be_valid
+ expect(agent_bot_connection.errors[:base]).to include('Disconnect Dialogflow before connecting Agent Bot')
+ expect(captain_connection).not_to be_valid
+ expect(captain_connection.errors[:base]).to include('Disconnect Dialogflow before connecting Captain')
+ end
+
+ it 'allows Agent Bot after Dialogflow is disabled' do
+ hook.disabled!
+
+ expect(build(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)).to be_valid
+ end
+ end
+
+ it 'rejects enabling an inactive Agent Bot while Captain is connected' do
+ connection = create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot, status: :inactive)
+ create(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+
+ expect { connection.active! }.to raise_error(ActiveRecord::RecordInvalid, /Disconnect Captain before connecting Agent Bot/)
+ end
+
+ it 'ignores a Captain connection whose assistant was deleted' do
+ create(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+ assistant.destroy!
+
+ expect(build(:agent_bot_inbox, inbox: inbox.reload, agent_bot: agent_bot)).to be_valid
+ end
+
+ it 'rejects enabling a disabled Dialogflow hook while Captain is connected' do
+ hook = create(:integrations_hook, :dialogflow, inbox: inbox, account: account, status: :disabled)
+ create(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+
+ expect { hook.enabled! }.to raise_error(ActiveRecord::RecordInvalid, /Disconnect Captain before connecting Dialogflow/)
+ end
+
+ it 'allows manual Agent Bot assignment on a Captain-connected inbox' do
+ create(:captain_inbox, inbox: inbox, captain_assistant: assistant)
+ conversation = create(:conversation, account: account, inbox: inbox).reload
+
+ Conversations::AssignmentService.new(conversation: conversation, assignee_id: agent_bot.id, assignee_type: 'AgentBot').perform
+
+ expect(conversation.reload.ai_assignee).to eq(agent_bot)
+ end
+ end
+
describe 'audit log' do
context 'when inbox is created' do
it 'has associated audit log created' do
diff --git a/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb b/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb
index d2cf30fc6fc..73c9294a2b2 100644
--- a/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb
+++ b/spec/enterprise/services/enterprise/message_templates/hook_execution_service_spec.rb
@@ -191,11 +191,14 @@ RSpec.describe MessageTemplates::HookExecutionService do
it 'does not schedule Captain for inbox bot integrations' do
expect(Captain::Conversation::ResponseBuilderJob).not_to receive(:perform_later)
- agent_bot_inbox = create(:agent_bot_inbox, inbox: inbox, agent_bot: create(:agent_bot, account: account))
+ agent_bot_inbox = create(:agent_bot_inbox, inbox: inbox, agent_bot: create(:agent_bot, account: account), status: :inactive)
+ # Keep coverage for existing duplicate connections until they are eliminated.
+ agent_bot_inbox.update_column(:status, AgentBotInbox.statuses[:active]) # rubocop:disable Rails/SkipsModelValidations
create(:message, conversation: conversation, message_type: :incoming, account: account)
agent_bot_inbox.destroy!
- create(:integrations_hook, :dialogflow, inbox: inbox, account: account)
+ hook = create(:integrations_hook, :dialogflow, inbox: inbox, account: account, status: :disabled)
+ hook.update_column(:status, Integrations::Hook.statuses[:enabled]) # rubocop:disable Rails/SkipsModelValidations
create(:message, conversation: conversation, message_type: :incoming, account: account)
end