Chatwoot/app/services/twilio/send_on_twilio_service.rb
Sojan Jose 2c73df4292
Chore: Provide fixed attachment URLs for Channels (#4507)
Prior to this change, The attachment URL sent from Chatwoot to 3rd party integrations like Whatsapp and Facebook
involved a 301 redirect before the original content is served. This causes intermittent breakages for the sent attachments.

fixes: #3632
ref: https://blog.saeloun.com/2021/09/14/rails-7-adds-expiring-urls-to-active-storage.html
2022-04-20 22:42:13 +05:30

46 lines
942 B
Ruby

class Twilio::SendOnTwilioService < Base::SendOnChannelService
private
def channel_class
Channel::TwilioSms
end
def perform_reply
begin
twilio_message = client.messages.create(**message_params)
rescue Twilio::REST::TwilioError => e
Sentry.capture_exception(e)
end
message.update!(source_id: twilio_message.sid) if twilio_message
end
def message_params
params = {
body: message.content,
from: channel.phone_number,
to: contact_inbox.source_id
}
params[:media_url] = attachments if message.attachments.present?
params
end
def attachments
message.attachments.map(&:download_url)
end
def inbox
@inbox ||= message.inbox
end
def channel
@channel ||= inbox.channel
end
def outgoing_message?
message.outgoing? || message.template?
end
def client
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
end
end