Class: Twilio::Rails::SMS::Twiml::MessageOperation

Inherits:
BaseOperation show all
Defined in:
app/operations/twilio/rails/sms/twiml/message_operation.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/operations/twilio/rails/sms/twiml/message_operation.rb', line 9

def execute
  inbound_message = conversation.messages.build(
    direction: "inbound",
    body: params["Body"],
    sid: params["SmsSid"].presence || params["MessageSid"].presence
  )

  inbound_message.save!

  body = Twilio::Rails::SMS::Responder.new(inbound_message).respond

  if body.present?
    message = conversation.messages.build(
      direction: "outbound",
      body: body,
    )

    message.save!

    twiml_response = Twilio::TwiML::MessagingResponse.new do |twiml|
      twiml.message(
        body: body,
        action: ::Twilio::Rails::Engine.routes.url_helpers.sms_status_message_path(message_id: message.id)
      )
    end

    Twilio::Rails.config.logger.info("message_twiml: #{twiml_response.to_s}")
    twiml_response.to_s
  else
    Twilio::Rails.config.logger.info("resply is blank, not sending message in response")
    Twilio::Rails.config.logger.info("message_twiml: #{twiml_response.to_s}")

    twiml = Twilio::TwiML::MessagingResponse.new
    twiml.to_s
  end
end