Class: Twilio::Rails::SMS::DelegatedResponder

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

Overview

Base class for SMS responders. To define a responder start by generating a sublcass.

rails generate twilio:rails:sms_responder ThankYou

This will create a new class in ‘app/sms_responders/thank_you_responder.rb` which will subclass this class. It must be registered with the framework in the initializer for it to be available. The generator does this.

# config/initializers/twilio_rails.rb
config.sms_responders.register { ThankYouResponder }

Then the responder must implement the #handle? and #reply methods. If the #handle? method returns true then the #reply method will be called to generate the body of the response, and send that message back as an SMS. Only one responder will be called for a given message.

Examples:

class ThankYouResponder < ::Twilio::Rails::SMS::DelegatedResponder
  def handle?
    matches?(/thank you/)
  end

  def reply
    "Thank you too!"
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ DelegatedResponder

Returns a new instance of DelegatedResponder.



41
42
43
44
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 41

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

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



30
31
32
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 30

def message
  @message
end

#sms_conversationObject (readonly)

Returns the value of attribute sms_conversation.



30
31
32
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 30

def sms_conversation
  @sms_conversation
end

Class Method Details

.responder_nameString

Returns the name of the class, without the namespace or the ‘Responder` suffix.

Returns:

  • (String)

    the name of the responder.



36
37
38
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 36

def responder_name
  self.name.demodulize.underscore.gsub(/_responder\Z/, "")
end

Instance Method Details

#handle?true, false

Must be implemented by the subclass otherwise will raise a ‘NotImplementedError`. Returns true if this responder should handle the given message. If true then the #reply method will be called to generate the body of the response. It has access to the message and the conversation.

Returns:

  • (true, false)

    true if this responder should handle the given message.

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 51

def handle?
  raise NotImplementedError
end

#replyString?

Must be implemented by the subclass otherwise will raise a ‘NotImplementedError`. Returns the body of the message to be sent in response. Will only be called if #handle? returns true. It has access to the message and the conversation.

Returns:

  • (String, nil)

    the body of the response to be sent as SMS, or ‘nil` if no message should be sent.

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 60

def reply
  raise NotImplementedError
end