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

@@ -79,7 +79,7 @@ class Integrations::Slack::IncomingMessageBuilder
def create_message
return unless conversation
conversation.messages.create(
@message = conversation.messages.create(
message_type: :outgoing,
account_id: conversation.account_id,
inbox_id: conversation.inbox_id,
@@ -89,10 +89,46 @@ class Integrations::Slack::IncomingMessageBuilder
sender: sender
)
process_attachments(params[:event][:files]) if params[:event][:files].present?
{ status: 'success' }
end
def slack_client
@slack_client ||= Slack::Web::Client.new(token: @integration_hook.access_token)
end
# TODO: move process attachment for facebook instagram and slack in one place
# https://api.slack.com/messaging/files
def process_attachments(attachments)
attachments.each do |attachment|
tempfile = Down::NetHttp.download(attachment[:url_private], headers: { 'Authorization' => "Bearer #{integration_hook.access_token}" })
attachment_params = {
file_type: file_type(attachment),
account_id: @message.account_id,
external_url: attachment[:url_private],
file: {
io: tempfile,
filename: tempfile.original_filename,
content_type: tempfile.content_type
}
}
attachment_obj = @message.attachments.new(attachment_params)
attachment_obj.file.content_type = attachment[:mimetype]
attachment_obj.save!
end
end
def file_type(attachment)
return if attachment[:mimetype] == 'text/plain'
case attachment[:filetype]
when 'png', 'jpeg', 'gif', 'bmp', 'tiff', 'jpg'
:image
when 'pdf'
:file
end
end
end

View File

@@ -45,8 +45,15 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
end
def send_message
return if message_content.blank?
post_message if message_content.present?
upload_file if message.attachments.any?
rescue Slack::Web::Api::Errors::AccountInactive => e
Rails.logger.info e
hook.authorization_error!
hook.disable if hook.enabled?
end
def post_message
@slack_message = slack_client.chat_postMessage(
channel: hook.reference_id,
text: message_content,
@@ -54,10 +61,28 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService
thread_ts: conversation.identifier,
icon_url: avatar_url(message.sender)
)
rescue Slack::Web::Api::Errors::AccountInactive => e
Rails.logger.info e
hook.authorization_error!
hook.disable if hook.enabled?
end
def upload_file
result = slack_client.files_upload({
channels: hook.reference_id,
initial_comment: 'Attached File!',
thread_ts: conversation.identifier
}.merge(file_information))
Rails.logger.info(result)
end
def file_type
File.extname(message.attachments.first.file_url).strip.downcase[1..]
end
def file_information
{
filename: message.attachments.first.file.filename,
filetype: file_type,
content: message.attachments.first.file.download,
title: message.attachments.first.file.filename
}
end
def sender_name(sender)