Files
chatwoot/spec/jobs/hook_job_spec.rb
f7bbd40816 fix(slack): Sync bot interactive responses (#14076)
When a customer responds to a bot's interactive prompt (input_select,
input_csat, form, input_email) from the widget, the response shows up in
the Chatwoot agent UI but is not reflected in the linked Slack channel —
Slack only ever shows the original question. This happens because the
widget submits the answer as an UPDATE to the original message (writing
`content_attributes.submitted_values` or `submitted_email`), but the
Slack hook only listened to `message.created`, so updates were ignored.

Closes https://linear.app/chatwoot/issue/PLA-147

### Preview

<img width="1290" height="1106" alt="CleanShot 2026-04-21 at 13 19
19@2x"
src="https://github.com/user-attachments/assets/cd2a9d3f-89d3-4e81-9230-5b078e1b7b44"
/>

### How to test

  1. Connect a web widget inbox to a Slack channel.
2. Trigger each bot message type (input_select, form, input_csat,
input_email) in a conversation.
  3. Submit responses from the widget.
4. Verify each response now appears in the Slack thread, appended to the
original bot question.

---------

Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-28 10:29:03 +04:00

214 lines
9.6 KiB
Ruby

require 'rails_helper'
RSpec.describe HookJob do
subject(:job) { described_class.perform_later(hook, event_name, event_data) }
let(:account) { create(:account) }
let(:hook) { create(:integrations_hook, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:event_name) { 'message.created' }
let(:event_data) { { message: create(:message, account: account, content: 'muchas muchas gracias', message_type: :incoming) } }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(hook, event_name, event_data)
.on_queue('medium')
end
context 'when the hook is disabled' do
it 'does not execute the job' do
hook = create(:integrations_hook, status: 'disabled', account: account)
allow(SendOnSlackJob).to receive(:perform_later)
allow(Integrations::Dialogflow::ProcessorService).to receive(:new)
allow(Integrations::GoogleTranslate::DetectLanguageService).to receive(:new)
expect(SendOnSlackJob).not_to receive(:perform_later)
expect(Integrations::GoogleTranslate::DetectLanguageService).not_to receive(:new)
expect(Integrations::Dialogflow::ProcessorService).not_to receive(:new)
described_class.perform_now(hook, event_name, event_data)
end
end
context 'when handleable events like message.created' do
let(:process_service) { double }
before do
allow(process_service).to receive(:perform)
end
it 'calls SendOnSlackJob when its a slack hook' do
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(SendOnSlackJob).to receive(:perform_later).and_return(process_service)
expect(SendOnSlackJob).to receive(:perform_later).with(event_data[:message], hook)
described_class.perform_now(hook, event_name, event_data)
end
it 'calls SendOnSlackJob when its a slack hook for message with attachments' do
event_data = { message: create(:message, :with_attachment, account: account) }
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(SendOnSlackJob).to receive(:set).with(wait: 2.seconds).and_return(SendOnSlackJob)
allow(SendOnSlackJob).to receive(:perform_later).and_return(process_service)
expect(SendOnSlackJob).to receive(:perform_later).with(event_data[:message], hook)
described_class.perform_now(hook, event_name, event_data)
end
it 'calls Integrations::Dialogflow::ProcessorService when its a dialogflow intergation' do
hook = create(:integrations_hook, :dialogflow, inbox: inbox, account: account)
allow(Integrations::Dialogflow::ProcessorService).to receive(:new).and_return(process_service)
expect(Integrations::Dialogflow::ProcessorService).to receive(:new).with(event_name: event_name, hook: hook, event_data: event_data)
described_class.perform_now(hook, event_name, event_data)
end
it 'calls Conversations::DetectLanguageJob when its a google_translate intergation' do
hook = create(:integrations_hook, :google_translate, account: account)
allow(Integrations::GoogleTranslate::DetectLanguageService).to receive(:new).and_return(process_service)
expect(Integrations::GoogleTranslate::DetectLanguageService).to receive(:new).with(hook: hook, message: event_data[:message])
described_class.perform_now(hook, event_name, event_data)
end
end
context 'when handleable events like message.updated for slack' do
let(:process_service) { double }
before do
allow(process_service).to receive(:perform)
end
it 'calls UpdateSlackMessageJob when content_attributes changed' do
message = create(:message, account: account, content: 'Pick one', message_type: :outgoing,
content_type: :input_select, content_attributes: { items: [{ title: 'A', value: 'a' }] })
hook = create(:integrations_hook, app_id: 'slack', account: account)
event_data = { message: message, previous_changes: { 'content_attributes' => [{}, { 'submitted_values' => [{ 'title' => 'A' }] }] } }
allow(UpdateSlackMessageJob).to receive(:perform_later).and_return(process_service)
expect(UpdateSlackMessageJob).to receive(:perform_later).with(message, hook)
described_class.perform_now(hook, 'message.updated', event_data)
end
it 'does not call UpdateSlackMessageJob when content_attributes did not change' do
message = create(:message, account: account, content: 'Pick one', message_type: :outgoing,
content_type: :input_select, content_attributes: { items: [{ title: 'A', value: 'a' }] })
hook = create(:integrations_hook, app_id: 'slack', account: account)
event_data = { message: message, previous_changes: { 'status' => %w[sent delivered] } }
expect(UpdateSlackMessageJob).not_to receive(:perform_later)
described_class.perform_now(hook, 'message.updated', event_data)
end
it 'does not call UpdateSlackMessageJob for unsupported content types' do
message = create(:message, account: account, content: 'Hello', message_type: :outgoing, content_type: :text)
hook = create(:integrations_hook, app_id: 'slack', account: account)
event_data = { message: message, previous_changes: { 'content_attributes' => [{}, {}] } }
expect(UpdateSlackMessageJob).not_to receive(:perform_later)
described_class.perform_now(hook, 'message.updated', event_data)
end
end
context 'when processing leadsquared integration' do
let(:contact) { create(:contact, account: account) }
let(:conversation) { create(:conversation, account: account, contact: contact) }
let(:processor_service) { instance_double(Crm::Leadsquared::ProcessorService) }
let(:leadsquared_hook) { instance_double(Integrations::Hook, id: 123, app_id: 'leadsquared', account: account) }
before do
allow(Crm::Leadsquared::ProcessorService).to receive(:new).with(leadsquared_hook).and_return(processor_service)
end
context 'when processing contact.updated event' do
let(:event_name) { 'contact.updated' }
let(:event_data) { { contact: contact } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_contact).with(contact)
# Mock the with_lock method directly on the job instance
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
it 'does not process when feature is not allowed' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(false)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock)
expect(job_instance).not_to receive(:with_lock)
expect(processor_service).not_to receive(:handle_contact)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing conversation.created event' do
let(:event_name) { 'conversation.created' }
let(:event_data) { { conversation: conversation } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_conversation_created).with(conversation)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing conversation.resolved event' do
let(:event_name) { 'conversation.resolved' }
let(:event_data) { { conversation: conversation } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_conversation_resolved).with(conversation)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing invalid event' do
let(:event_name) { 'invalid.event' }
let(:event_data) { { contact: contact } }
it 'does not process for invalid event names' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock)
expect(job_instance).not_to receive(:with_lock)
expect(processor_service).not_to receive(:handle_contact)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
end
end