chore: Add slack external_source_id for outgoing messages (#1503)

This commit is contained in:
Pranav Raj S 2020-12-10 22:53:49 +05:30 committed by GitHub
parent 1f02c09a41
commit 88c4b63eec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 43 deletions

View file

@ -2,13 +2,15 @@ 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
builder = Integrations::Slack::HookBuilder.new( ActiveRecord::Base.transaction do
account: Current.account, builder = Integrations::Slack::HookBuilder.new(
code: params[:code], account: Current.account,
inbox_id: params[:inbox_id] code: params[:code],
) inbox_id: params[:inbox_id]
@hook = builder.perform )
create_chatwoot_slack_channel @hook = builder.perform
create_chatwoot_slack_channel
end
end end
def update def update

View file

@ -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

View file

@ -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

View file

@ -1,50 +1,78 @@
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( expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id, channel: hook.reference_id,
text: message.content, text: "*Inbox: #{inbox.name} [#{inbox.inbox_type}]* \n\n #{message.content}",
username: "Contact: #{message.sender.name}", username: "Contact: #{message.sender.name}",
thread_ts: conversation.identifier, thread_ts: nil,
icon_url: anything icon_url: anything
) ).and_return(slack_message)
builder.perform builder.perform
expect(conversation.reload.identifier).to eq '12345.6789'
end
end end
it 'disables hook on Slack AccountInactive error' do context 'with identifier' do
builder = described_class.new(message: message, hook: hook) before do
slack_client = double conversation.update!(identifier: 'random_slack_thread_ts')
expect(builder).to receive(:slack_client).and_return(slack_client) end
expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id,
text: message.content,
username: "Contact: #{message.sender.name}",
thread_ts: conversation.identifier,
icon_url: anything
).and_raise(Slack::Web::Api::Errors::AccountInactive.new('Account disconnected'))
allow(hook).to receive(:authorization_error!) it 'sent message to slack' do
expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id,
text: message.content,
username: "Contact: #{message.sender.name}",
thread_ts: conversation.identifier,
icon_url: anything
).and_return(slack_message)
builder.perform builder.perform
expect(hook).to be_disabled
expect(hook).to have_received(:authorization_error!) expect(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,
text: message.content,
username: "Contact: #{message.sender.name}",
thread_ts: conversation.identifier,
icon_url: anything
).and_raise(Slack::Web::Api::Errors::AccountInactive.new('Account disconnected'))
allow(hook).to receive(:authorization_error!)
builder.perform
expect(hook).to be_disabled
expect(hook).to have_received(:authorization_error!)
end
end end
end end
end end