mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	chore: Add slack external_source_id for outgoing messages (#1503)
This commit is contained in:
		| @@ -2,6 +2,7 @@ class Api::V1::Accounts::Integrations::SlackController < Api::V1::Accounts::Base | |||||||
|   before_action :fetch_hook, only: [:update, :destroy] |   before_action :fetch_hook, only: [:update, :destroy] | ||||||
|  |  | ||||||
|   def create |   def create | ||||||
|  |     ActiveRecord::Base.transaction do | ||||||
|       builder = Integrations::Slack::HookBuilder.new( |       builder = Integrations::Slack::HookBuilder.new( | ||||||
|         account: Current.account, |         account: Current.account, | ||||||
|         code: params[:code], |         code: params[:code], | ||||||
| @@ -10,6 +11,7 @@ class Api::V1::Accounts::Integrations::SlackController < Api::V1::Accounts::Base | |||||||
|       @hook = builder.perform |       @hook = builder.perform | ||||||
|       create_chatwoot_slack_channel |       create_chatwoot_slack_channel | ||||||
|     end |     end | ||||||
|  |   end | ||||||
|  |  | ||||||
|   def update |   def update | ||||||
|     create_chatwoot_slack_channel |     create_chatwoot_slack_channel | ||||||
|   | |||||||
| @@ -21,8 +21,8 @@ class Integrations::Slack::ChannelBuilder | |||||||
|   end |   end | ||||||
|  |  | ||||||
|   def find_or_create_channel |   def find_or_create_channel | ||||||
|     exisiting_channel = slack_client.conversations_list.channels.find { |channel| channel['name'] == params[:channel] } |     existing_channel = slack_client.conversations_list.channels.find { |channel| channel['name'] == params[:channel] } | ||||||
|     @channel = exisiting_channel || slack_client.conversations_create(name: params[:channel])['channel'] |     @channel = existing_channel || slack_client.conversations_create(name: params[:channel])['channel'] | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def update_reference_id |   def update_reference_id | ||||||
|   | |||||||
| @@ -24,7 +24,11 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService | |||||||
|  |  | ||||||
|   def perform_reply |   def perform_reply | ||||||
|     send_message |     send_message | ||||||
|  |  | ||||||
|  |     return unless @slack_message | ||||||
|  |  | ||||||
|     update_reference_id |     update_reference_id | ||||||
|  |     update_external_source_id_slack | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def message_content |   def message_content | ||||||
| @@ -68,6 +72,12 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService | |||||||
|     conversation.update!(identifier: @slack_message['ts']) |     conversation.update!(identifier: @slack_message['ts']) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   def update_external_source_id_slack | ||||||
|  |     return unless @slack_message['message'] | ||||||
|  |  | ||||||
|  |     message.update!(external_source_id_slack: "cw-origin-#{@slack_message['message']['ts']}") | ||||||
|  |   end | ||||||
|  |  | ||||||
|   def slack_client |   def slack_client | ||||||
|     @slack_client ||= Slack::Web::Client.new(token: hook.access_token) |     @slack_client ||= Slack::Web::Client.new(token: hook.access_token) | ||||||
|   end |   end | ||||||
|   | |||||||
| @@ -1,37 +1,64 @@ | |||||||
| require 'rails_helper' | require 'rails_helper' | ||||||
|  |  | ||||||
| describe Integrations::Slack::SendOnSlackService do | describe Integrations::Slack::SendOnSlackService do | ||||||
|   let(:account) { create(:account) } |  | ||||||
|   let!(:inbox) { create(:inbox, account: account) } |  | ||||||
|   let!(:contact) { create(:contact) } |   let!(:contact) { create(:contact) } | ||||||
|  |   let!(:conversation) { create(:conversation, contact: contact, identifier: nil) } | ||||||
|  |   let(:account) { conversation.account } | ||||||
|   let!(:hook) { create(:integrations_hook, account: account) } |   let!(:hook) { create(:integrations_hook, account: account) } | ||||||
|   let!(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact) } |   let!(:message) do | ||||||
|   let!(:message) { create(:message, account: account, inbox: inbox, conversation: conversation) } |     create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation) | ||||||
|  |   end | ||||||
|  |   let(:slack_message) { double } | ||||||
|  |   let(:slack_message_content) { double } | ||||||
|  |   let(:slack_client) { double } | ||||||
|  |   let(:builder) { described_class.new(message: message, hook: hook) } | ||||||
|  |  | ||||||
|  |   before do | ||||||
|  |     allow(builder).to receive(:slack_client).and_return(slack_client) | ||||||
|  |     allow(slack_message).to receive(:[]).with('ts').and_return('12345.6789') | ||||||
|  |     allow(slack_message).to receive(:[]).with('message').and_return(slack_message_content) | ||||||
|  |     allow(slack_message_content).to receive(:[]).with('ts').and_return('6789.12345') | ||||||
|  |   end | ||||||
|  |  | ||||||
|   describe '#perform' do |   describe '#perform' do | ||||||
|     it 'sent message to slack' do |     context 'without identifier' do | ||||||
|       builder = described_class.new(message: message, hook: hook) |       it 'updates slack thread id in conversation' do | ||||||
|       stub_request(:post, 'https://slack.com/api/chat.postMessage') |         inbox = conversation.inbox | ||||||
|         .to_return(status: 200, body: '', headers: {}) |  | ||||||
|       slack_client = double |  | ||||||
|       expect(builder).to receive(:slack_client).and_return(slack_client) |  | ||||||
|  |  | ||||||
|  |         expect(slack_client).to receive(:chat_postMessage).with( | ||||||
|  |           channel: hook.reference_id, | ||||||
|  |           text: "*Inbox: #{inbox.name} [#{inbox.inbox_type}]* \n\n #{message.content}", | ||||||
|  |           username: "Contact: #{message.sender.name}", | ||||||
|  |           thread_ts: nil, | ||||||
|  |           icon_url: anything | ||||||
|  |         ).and_return(slack_message) | ||||||
|  |  | ||||||
|  |         builder.perform | ||||||
|  |  | ||||||
|  |         expect(conversation.reload.identifier).to eq '12345.6789' | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     context 'with identifier' do | ||||||
|  |       before do | ||||||
|  |         conversation.update!(identifier: 'random_slack_thread_ts') | ||||||
|  |       end | ||||||
|  |  | ||||||
|  |       it 'sent message to slack' do | ||||||
|         expect(slack_client).to receive(:chat_postMessage).with( |         expect(slack_client).to receive(:chat_postMessage).with( | ||||||
|           channel: hook.reference_id, |           channel: hook.reference_id, | ||||||
|           text: message.content, |           text: message.content, | ||||||
|           username: "Contact: #{message.sender.name}", |           username: "Contact: #{message.sender.name}", | ||||||
|           thread_ts: conversation.identifier, |           thread_ts: conversation.identifier, | ||||||
|           icon_url: anything |           icon_url: anything | ||||||
|       ) |         ).and_return(slack_message) | ||||||
|  |  | ||||||
|         builder.perform |         builder.perform | ||||||
|  |  | ||||||
|  |         expect(message.external_source_id_slack).to eq 'cw-origin-6789.12345' | ||||||
|       end |       end | ||||||
|  |  | ||||||
|       it 'disables hook on Slack AccountInactive error' do |       it 'disables hook on Slack AccountInactive error' do | ||||||
|       builder = described_class.new(message: message, hook: hook) |  | ||||||
|       slack_client = double |  | ||||||
|       expect(builder).to receive(:slack_client).and_return(slack_client) |  | ||||||
|         expect(slack_client).to receive(:chat_postMessage).with( |         expect(slack_client).to receive(:chat_postMessage).with( | ||||||
|           channel: hook.reference_id, |           channel: hook.reference_id, | ||||||
|           text: message.content, |           text: message.content, | ||||||
| @@ -48,3 +75,4 @@ describe Integrations::Slack::SendOnSlackService do | |||||||
|       end |       end | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Pranav Raj S
					Pranav Raj S