77
app/services/twilio/incoming_message_service.rb
Normal file
77
app/services/twilio/incoming_message_service.rb
Normal file
@@ -0,0 +1,77 @@
|
||||
class Twilio::IncomingMessageService
|
||||
pattr_initialize [:params!]
|
||||
|
||||
def perform
|
||||
set_contact
|
||||
set_conversation
|
||||
@conversation.messages.create(
|
||||
content: params[:Body],
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
message_type: :incoming,
|
||||
contact_id: @contact.id,
|
||||
source_id: params[:SmsSid]
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def twilio_inbox
|
||||
@twilio_inbox ||= ::Channel::TwilioSms.find_by!(
|
||||
account_sid: params[:AccountSid],
|
||||
phone_number: params[:To]
|
||||
)
|
||||
end
|
||||
|
||||
def inbox
|
||||
@inbox ||= twilio_inbox.inbox
|
||||
end
|
||||
|
||||
def account
|
||||
@account ||= inbox.account
|
||||
end
|
||||
|
||||
def set_contact
|
||||
contact_inbox = ::ContactBuilder.new(
|
||||
source_id: params[:From],
|
||||
inbox: inbox,
|
||||
contact_attributes: contact_attributes
|
||||
).perform
|
||||
|
||||
@contact_inbox = contact_inbox
|
||||
@contact = contact_inbox.contact
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
def set_conversation
|
||||
@conversation = @contact_inbox.conversations.first
|
||||
return if @conversation
|
||||
|
||||
@conversation = ::Conversation.create!(conversation_params)
|
||||
end
|
||||
|
||||
def contact_attributes
|
||||
{
|
||||
name: params[:From],
|
||||
phone_number: params[:From],
|
||||
contact_attributes: additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
def additional_attributes
|
||||
{
|
||||
from_zip_code: params[:FromZip],
|
||||
from_country: params[:FromCountry],
|
||||
from_state: params[:FromState]
|
||||
}
|
||||
end
|
||||
end
|
||||
34
app/services/twilio/outgoing_message_service.rb
Normal file
34
app/services/twilio/outgoing_message_service.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class Twilio::OutgoingMessageService
|
||||
pattr_initialize [:message!]
|
||||
|
||||
def perform
|
||||
return if message.private
|
||||
return if message.source_id
|
||||
return if inbox.channel.class.to_s != 'Channel::TwilioSms'
|
||||
return unless message.outgoing?
|
||||
|
||||
twilio_message = client.messages.create(
|
||||
body: message.content,
|
||||
from: channel.phone_number,
|
||||
to: contact.phone_number
|
||||
)
|
||||
message.update!(source_id: twilio_message.sid)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
delegate :conversation, to: :message
|
||||
delegate :contact, to: :conversation
|
||||
|
||||
def inbox
|
||||
@inbox ||= message.inbox
|
||||
end
|
||||
|
||||
def channel
|
||||
@channel ||= inbox.channel
|
||||
end
|
||||
|
||||
def client
|
||||
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
||||
end
|
||||
end
|
||||
35
app/services/twilio/webhook_setup_service.rb
Normal file
35
app/services/twilio/webhook_setup_service.rb
Normal file
@@ -0,0 +1,35 @@
|
||||
class Twilio::WebhookSetupService
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
pattr_initialize [:inbox!]
|
||||
|
||||
def perform
|
||||
if phone_numbers.empty?
|
||||
Rails.logger.info "TWILIO_PHONE_NUMBER_NOT_FOUND: #{channel.phone_number}"
|
||||
else
|
||||
twilio_client
|
||||
.incoming_phone_numbers(phonenumber_sid)
|
||||
.update(sms_method: 'POST', sms_url: twilio_callback_index_url)
|
||||
end
|
||||
rescue Twilio::REST::TwilioError => e
|
||||
Rails.logger.info "TWILIO_FAILURE: #{e.message}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def phonenumber_sid
|
||||
phone_numbers.first.sid
|
||||
end
|
||||
|
||||
def phone_numbers
|
||||
@phone_numbers ||= twilio_client.incoming_phone_numbers.list(phone_number: channel.phone_number)
|
||||
end
|
||||
|
||||
def channel
|
||||
@channel ||= inbox.channel
|
||||
end
|
||||
|
||||
def twilio_client
|
||||
@twilio_client ||= ::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user