mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-30 02:32:29 +00:00 
			
		
		
		
	Fix: Consider bot message on slack integration (#6793)
This commit is contained in:
		| @@ -18,6 +18,7 @@ class HookJob < ApplicationJob | ||||
|     return unless ['message.created'].include?(event_name) | ||||
|  | ||||
|     message = event_data[:message] | ||||
|  | ||||
|     Integrations::Slack::SendOnSlackService.new(message: message, hook: hook).perform | ||||
|   end | ||||
|  | ||||
|   | ||||
| @@ -1,14 +1,12 @@ | ||||
| class HookListener < BaseListener | ||||
|   def message_created(event) | ||||
|     message = extract_message_and_account(event)[0] | ||||
|     return unless message.webhook_sendable? | ||||
|  | ||||
|     execute_hooks(event, message) | ||||
|   end | ||||
|  | ||||
|   def message_updated(event) | ||||
|     message = extract_message_and_account(event)[0] | ||||
|     return unless message.webhook_sendable? | ||||
|  | ||||
|     execute_hooks(event, message) | ||||
|   end | ||||
|   | ||||
| @@ -9,6 +9,10 @@ module MessageFilterHelpers | ||||
|     incoming? || outgoing? | ||||
|   end | ||||
|  | ||||
|   def slack_hook_sendable? | ||||
|     incoming? || outgoing? || template? | ||||
|   end | ||||
|  | ||||
|   def notifiable? | ||||
|     incoming? || outgoing? | ||||
|   end | ||||
|   | ||||
| @@ -9,7 +9,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService | ||||
|     # we don't want message loop in slack | ||||
|     return if message.external_source_id_slack.present? | ||||
|     # we don't want to start slack thread from agent conversation as of now | ||||
|     return if message.outgoing? && conversation.identifier.blank? | ||||
|     return if invalid_message? | ||||
|  | ||||
|     perform_reply | ||||
|   end | ||||
| @@ -23,6 +23,10 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService | ||||
|     true | ||||
|   end | ||||
|  | ||||
|   def invalid_message? | ||||
|     (message.outgoing? && conversation.identifier.blank?) || !message.slack_hook_sendable? | ||||
|   end | ||||
|  | ||||
|   def perform_reply | ||||
|     send_message | ||||
|  | ||||
|   | ||||
| @@ -29,6 +29,14 @@ RSpec.describe HookJob, type: :job do | ||||
|       described_class.perform_now(hook, event_name, event_data) | ||||
|     end | ||||
|  | ||||
|     it 'calls Integrations::Slack::SendOnSlackService when its a slack hook for template message' do | ||||
|       event_data = { message: create(:message, account: account, message_type: :template) } | ||||
|       hook = create(:integrations_hook, app_id: 'slack', account: account) | ||||
|       allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service) | ||||
|       expect(Integrations::Slack::SendOnSlackService).to receive(:new) | ||||
|       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) | ||||
|   | ||||
| @@ -6,6 +6,7 @@ describe Integrations::Dialogflow::ProcessorService do | ||||
|   let(:hook) { create(:integrations_hook, :dialogflow, inbox: inbox, account: account) } | ||||
|   let(:conversation) { create(:conversation, account: account, status: :pending) } | ||||
|   let(:message) { create(:message, account: account, conversation: conversation) } | ||||
|   let(:template_message) { create(:message, account: account, conversation: conversation, message_type: :template, content: 'Bot message') } | ||||
|   let(:event_name) { 'message.created' } | ||||
|   let(:event_data) { { message: message } } | ||||
|   let(:dialogflow_text_double) { double } | ||||
| @@ -35,6 +36,15 @@ describe Integrations::Dialogflow::ProcessorService do | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     context 'when invalid message and dialogflow returns empty block' do | ||||
|       it 'will not create the response message' do | ||||
|         event_data = { message: template_message } | ||||
|         processor = described_class.new(event_name: event_name, hook: hook, event_data: event_data) | ||||
|         processor.perform | ||||
|         expect(conversation.reload.messages.last.content).not_to eql('hello payload') | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     context 'when dilogflow raises exception' do | ||||
|       it 'tracks hook into exception tracked' do | ||||
|         last_message = conversation.reload.messages.last.content | ||||
|   | ||||
| @@ -9,6 +9,9 @@ describe Integrations::Slack::SendOnSlackService do | ||||
|   let!(:message) do | ||||
|     create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation) | ||||
|   end | ||||
|   let!(:template_message) do | ||||
|     create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation, message_type: :template) | ||||
|   end | ||||
|  | ||||
|   let(:slack_message) { double } | ||||
|   let(:file_attachment) { double } | ||||
| @@ -119,6 +122,23 @@ describe Integrations::Slack::SendOnSlackService do | ||||
|         expect(message.attachments).to be_any | ||||
|       end | ||||
|  | ||||
|       it 'sent a template message on slack' do | ||||
|         builder = described_class.new(message: template_message, hook: hook) | ||||
|         allow(builder).to receive(:slack_client).and_return(slack_client) | ||||
|  | ||||
|         expect(slack_client).to receive(:chat_postMessage).with( | ||||
|           channel: hook.reference_id, | ||||
|           text: template_message.content, | ||||
|           username: "#{template_message.sender.name} (Contact)", | ||||
|           thread_ts: conversation.identifier, | ||||
|           icon_url: anything | ||||
|         ).and_return(slack_message) | ||||
|  | ||||
|         builder.perform | ||||
|  | ||||
|         expect(template_message.external_source_id_slack).to eq 'cw-origin-6789.12345' | ||||
|       end | ||||
|  | ||||
|       it 'disables hook on Slack AccountInactive error' do | ||||
|         expect(slack_client).to receive(:chat_postMessage).with( | ||||
|           channel: hook.reference_id, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Tejaswini Chile
					Tejaswini Chile