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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user