fix: Twilio multiple attachment fix (#11452)
This commit is contained in:
committed by
GitHub
parent
bce1f58e86
commit
e9cda40b71
@@ -106,15 +106,22 @@ class Twilio::IncomingMessageService
|
||||
end
|
||||
|
||||
def attach_files
|
||||
return if params[:MediaUrl0].blank?
|
||||
num_media = params[:NumMedia].to_i
|
||||
return if num_media.zero?
|
||||
|
||||
attachment_file = download_attachment_file
|
||||
num_media.times do |i|
|
||||
media_url = params[:"MediaUrl#{i}"]
|
||||
attach_single_file(media_url) if media_url.present?
|
||||
end
|
||||
end
|
||||
|
||||
def attach_single_file(media_url)
|
||||
attachment_file = download_attachment_file(media_url)
|
||||
return if attachment_file.blank?
|
||||
|
||||
@message.attachments.new(
|
||||
account_id: @message.account_id,
|
||||
file_type: file_type(params[:MediaContentType0]),
|
||||
file_type: file_type(attachment_file.content_type),
|
||||
file: {
|
||||
io: attachment_file,
|
||||
filename: attachment_file.original_filename,
|
||||
@@ -123,24 +130,22 @@ class Twilio::IncomingMessageService
|
||||
)
|
||||
end
|
||||
|
||||
def download_attachment_file
|
||||
download_with_auth
|
||||
def download_attachment_file(media_url)
|
||||
download_with_auth(media_url)
|
||||
rescue Down::Error, Down::ClientError => e
|
||||
handle_download_attachment_error(e)
|
||||
handle_download_attachment_error(e, media_url)
|
||||
end
|
||||
|
||||
def download_with_auth
|
||||
def download_with_auth(media_url)
|
||||
Down.download(
|
||||
params[:MediaUrl0],
|
||||
# https://support.twilio.com/hc/en-us/articles/223183748-Protect-Media-Access-with-HTTP-Basic-Authentication-for-Programmable-Messaging
|
||||
media_url,
|
||||
http_basic_authentication: [twilio_channel.account_sid, twilio_channel.auth_token || twilio_channel.api_key_sid]
|
||||
)
|
||||
end
|
||||
|
||||
# This is just a temporary workaround since some users have not yet enabled media protection. We will remove this in the future.
|
||||
def handle_download_attachment_error(error)
|
||||
def handle_download_attachment_error(error, media_url)
|
||||
Rails.logger.info "Error downloading attachment from Twilio: #{error.message}: Retrying"
|
||||
Down.download(params[:MediaUrl0])
|
||||
Down.download(media_url)
|
||||
rescue StandardError => e
|
||||
Rails.logger.info "Error downloading attachment from Twilio: #{e.message}: Skipping"
|
||||
nil
|
||||
|
||||
Reference in New Issue
Block a user