feat: Support sending and receiving attachments in Slack Integration (#3022)

- Process incoming slack attachments
- Send attachments from chatwoot to slack
This commit is contained in:
Tejaswini Chile
2021-09-18 00:49:01 +05:30
committed by GitHub
parent 794a56d4cc
commit b1b0268705
6 changed files with 139 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ require 'rails_helper'
describe Integrations::Slack::IncomingMessageBuilder do
let(:account) { create(:account) }
let(:message_params) { slack_message_stub }
let(:message_with_attachments) { slack_attachment_stub }
let(:message_without_thread_ts) { slack_message_stub_without_thread_ts }
let(:verification_params) { slack_url_verification_stub }
@@ -51,6 +52,17 @@ describe Integrations::Slack::IncomingMessageBuilder do
builder.perform
expect(conversation.messages.count).to eql(messages_count)
end
it 'saves attachment if params files present' do
expect(hook).not_to eq nil
messages_count = conversation.messages.count
builder = described_class.new(message_with_attachments)
allow(builder).to receive(:sender).and_return(nil)
builder.perform
expect(conversation.messages.count).to eql(messages_count + 1)
expect(conversation.messages.last.content).to eql('this is test https://chatwoot.com Hey @Sojan Test again')
expect(conversation.messages.last.attachments).to be_any
end
end
end
end

View File

@@ -9,6 +9,7 @@ describe Integrations::Slack::SendOnSlackService do
create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation)
end
let(:slack_message) { double }
let(:file_attachment) { double }
let(:slack_message_content) { double }
let(:slack_client) { double }
let(:builder) { described_class.new(message: message, hook: hook) }
@@ -58,6 +59,36 @@ describe Integrations::Slack::SendOnSlackService do
expect(message.external_source_id_slack).to eq 'cw-origin-6789.12345'
end
it 'sent attachment on 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)
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
attachment.file.attach(io: File.open(Rails.root.join('spec/assets/avatar.png')), filename: 'avatar.png', content_type: 'image/png')
expect(slack_client).to receive(:files_upload).with(
channels: hook.reference_id,
initial_comment: 'Attached File!',
content: anything,
filename: attachment.file.filename,
filetype: 'png',
thread_ts: conversation.identifier,
title: anything
).and_return(file_attachment)
message.save!
builder.perform
expect(message.external_source_id_slack).to eq 'cw-origin-6789.12345'
expect(message.attachments).to be_any
end
it 'disables hook on Slack AccountInactive error' do
expect(slack_client).to receive(:chat_postMessage).with(
channel: hook.reference_id,

View File

@@ -21,6 +21,20 @@ module SlackStubs
}
end
def slack_attachment_stub
{
token: '[FILTERED]',
team_id: 'TLST3048H',
api_app_id: 'A012S5UETV4',
event: message_event,
type: 'event_callback',
event_id: 'Ev013QUX3WV6',
event_time: 1_588_623_033,
authed_users: '[FILTERED]',
webhook: {}
}
end
def slack_message_stub_without_thread_ts
{
token: '[FILTERED]',
@@ -52,6 +66,7 @@ module SlackStubs
ts: '1588623033.006000',
team: 'TLST3048H',
blocks: message_blocks,
files: file_stub,
thread_ts: '1588623023.005900',
channel: 'G01354F6A6Q',
event_ts: '1588623033.006000',
@@ -59,6 +74,19 @@ module SlackStubs
}
end
def file_stub
[
{
mimetype: 'image/png',
url_private: 'https://via.placeholder.com/250x250.png',
name: 'name_of_the_file',
title: 'title_of_the_file',
filetype: 'png',
url_private_download: 'https://via.placeholder.com/250x250.png'
}
]
end
def message_blocks
[
{