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