fix(agent-bot): dispatch webhook event on agent bot assignment

When an AgentBot is assigned to a conversation after the first message,
the bot receives no event and cannot respond. This adds ASSIGNEE_CHANGED
event dispatch for agent bot assignment changes and handles it in the
AgentBotListener to notify the bot via webhook.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Muhsin
2026-04-02 10:52:33 +04:00
co-authored by Claude Opus 4.6
parent 211fb1102d
commit 40d00d895d
3 changed files with 34 additions and 1 deletions
+9
View File
@@ -15,6 +15,15 @@ class AgentBotListener < BaseListener
agent_bots_for(inbox, conversation).each { |agent_bot| process_webhook_bot_event(agent_bot, payload) }
end
def assignee_changed(event)
conversation = extract_conversation_and_account(event)[0]
return unless conversation.assignee_agent_bot.present?
conversation.inbox
payload = conversation.webhook_data.merge(event: __method__.to_s)
process_webhook_bot_event(conversation.assignee_agent_bot, payload)
end
def message_created(event)
message = extract_message_and_account(event)[0]
inbox = message.inbox
+1 -1
View File
@@ -29,7 +29,7 @@ module AssignmentHandler
def notify_assignment_change
{
ASSIGNEE_CHANGED => -> { saved_change_to_assignee_id? },
ASSIGNEE_CHANGED => -> { saved_change_to_assignee_id? || saved_change_to_assignee_agent_bot_id? },
TEAM_CHANGED => -> { saved_change_to_team_id? }
}.each do |event, condition|
condition.call && dispatcher_dispatch(event, previous_changes)
+24
View File
@@ -57,6 +57,30 @@ describe AgentBotListener do
end
end
describe '#assignee_changed' do
let(:event_name) { 'assignee.changed' }
let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation) }
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: 'assignee_changed')).once
listener.assignee_changed(event)
end
end
context 'when conversation is not assigned to an agent bot' do
it 'does not send webhook' do
expect(AgentBots::WebhookJob).not_to receive(:perform_later)
listener.assignee_changed(event)
end
end
end
describe '#webwidget_triggered' do
let(:event_name) { 'webwidget.triggered' }