2020-06-25 18:05:16 +00:00
|
|
|
class Twilio::SendOnTwilioService < Base::SendOnChannelService
|
|
|
|
private
|
2020-04-05 16:41:27 +00:00
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
def channel_class
|
|
|
|
Channel::TwilioSms
|
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
|
2020-06-25 18:05:16 +00:00
|
|
|
def perform_reply
|
2021-09-06 18:35:14 +00:00
|
|
|
begin
|
|
|
|
twilio_message = client.messages.create(**message_params)
|
|
|
|
rescue Twilio::REST::TwilioError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e, user: message.sender, account: message.account).capture_exception
|
2021-09-06 18:35:14 +00:00
|
|
|
end
|
2021-09-21 17:32:45 +00:00
|
|
|
message.update!(source_id: twilio_message.sid) if twilio_message
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
def message_params
|
|
|
|
params = {
|
|
|
|
body: message.content,
|
|
|
|
from: channel.phone_number,
|
|
|
|
to: contact_inbox.source_id
|
|
|
|
}
|
2021-09-06 18:35:14 +00:00
|
|
|
params[:media_url] = attachments if message.attachments.present?
|
2020-04-29 20:11:13 +00:00
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
2022-04-20 17:12:13 +00:00
|
|
|
message.attachments.map(&:download_url)
|
2020-04-29 20:11:13 +00:00
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
|
|
|
|
def inbox
|
|
|
|
@inbox ||= message.inbox
|
|
|
|
end
|
|
|
|
|
|
|
|
def channel
|
|
|
|
@channel ||= inbox.channel
|
|
|
|
end
|
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
def outgoing_message?
|
|
|
|
message.outgoing? || message.template?
|
|
|
|
end
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
def client
|
|
|
|
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
|
|
|
end
|
|
|
|
end
|