diff --git a/app/listeners/agent_bot_listener.rb b/app/listeners/agent_bot_listener.rb index ccac8005f..8fdf964fb 100644 --- a/app/listeners/agent_bot_listener.rb +++ b/app/listeners/agent_bot_listener.rb @@ -15,6 +15,14 @@ class AgentBotListener < BaseListener agent_bots_for(inbox, conversation).each { |agent_bot| process_webhook_bot_event(agent_bot, payload) } end + def conversation_updated(event) + conversation = extract_conversation_and_account(event)[0] + inbox = conversation.inbox + event_name = __method__.to_s + payload = conversation.webhook_data.merge(event: event_name) + agent_bots_for(inbox, conversation).each { |agent_bot| process_webhook_bot_event(agent_bot, payload) } + end + def message_created(event) message = extract_message_and_account(event)[0] inbox = message.inbox diff --git a/spec/listeners/agent_bot_listener_spec.rb b/spec/listeners/agent_bot_listener_spec.rb index 56c478a1b..24af37383 100644 --- a/spec/listeners/agent_bot_listener_spec.rb +++ b/spec/listeners/agent_bot_listener_spec.rb @@ -57,6 +57,39 @@ describe AgentBotListener do end end + describe '#conversation_updated' do + let(:event_name) { 'conversation.updated' } + let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation) } + + context 'when agent bot is not configured' do + it 'does not send webhook' do + expect(AgentBots::WebhookJob).not_to receive(:perform_later) + listener.conversation_updated(event) + end + end + + context 'when agent bot is configured on inbox' do + it 'sends webhook to the inbox agent bot' do + create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot) + expect(AgentBots::WebhookJob).to receive(:perform_later).with(agent_bot.outgoing_url, + conversation.webhook_data.merge(event: 'conversation_updated')).once + listener.conversation_updated(event) + end + end + + context 'when conversation is assigned to an agent bot' do + before do + conversation.update!(assignee_agent_bot: agent_bot, assignee: nil) + end + + it 'sends webhook to the assigned agent bot' do + expect(AgentBots::WebhookJob).to receive(:perform_later).with(agent_bot.outgoing_url, + conversation.webhook_data.merge(event: 'conversation_updated')).once + listener.conversation_updated(event) + end + end + end + describe '#webwidget_triggered' do let(:event_name) { 'webwidget.triggered' }