Class: Twilio::Rails::SMS::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio/rails/sms/responder.rb

Overview

The class responsible for pattern matching and delegating how to handle an incoming SMS. Called by Twiml::MessageOperation to generate the body of the response. For a given message it iterates over all registered sms_responders and replies with the first one that handles, or raises if none are found to handle the message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Responder

Returns a new instance of Responder.



13
14
15
16
# File 'lib/twilio/rails/sms/responder.rb', line 13

def initialize(message)
  @message = message
  @sms_conversation = message.sms_conversation
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/twilio/rails/sms/responder.rb', line 11

def message
  @message
end

#sms_conversationObject (readonly)

Returns the value of attribute sms_conversation.



11
12
13
# File 'lib/twilio/rails/sms/responder.rb', line 11

def sms_conversation
  @sms_conversation
end

Instance Method Details

#respondString

Iterates over all registered sms_responders and replies with the first one that handles, or raises if none are found to handle the message.

Returns:

  • (String)

    the body of the response.

Raises:



22
23
24
25
26
27
28
29
30
# File 'lib/twilio/rails/sms/responder.rb', line 22

def respond
  Twilio::Rails.config.sms_responders.all.each do |name, responder_class|
    responder = responder_class.new(message)
    return responder.reply if responder.handle?
  end

  raise Twilio::Rails::SMS::InvalidResponderError, "No responder found for SMS. message_id=#{ message.id } "\
    "phone_caller_id=#{ sms_conversation.phone_caller&.id } from_number=\"#{ sms_conversation.from_number }\" body=\"#{ message.body }\""
end