Module: Twilio::Rails::Models::Response

Extended by:
ActiveSupport::Concern
Included in:
Response
Defined in:
lib/twilio/rails/models/response.rb

Overview

A response object is created for every prompt in a phone call. It is associated to a PhoneCall in order, and contains transcriptions, digits, recordings, timestamps, and all other metadata.

Instance Method Summary collapse

Instance Method Details

#answer_no?true, false

Returns true if the transcription matches any of the configured "no". Will return false if the transcription is blank. See Configuration#yes_responses for the default values. It is possible for #answer_yes? and #answer_no? to both be false.

Returns:

  • (true, false)

    true if the transcription matches any of the configured "no" responses.



114
115
116
# File 'lib/twilio/rails/models/response.rb', line 114

def answer_no?
  transcription_matches?(Twilio::Rails.config.no_responses)
end

#answer_yes?true, false

Returns true if the transcription matches any of the configured "yes". Will return false if the transcription is blank. See Configuration#yes_responses for the default values. It is possible for #answer_yes? and #answer_no? to both be false.

Returns:

  • (true, false)

    true if the transcription matches any of the configured "yes" responses.



105
106
107
# File 'lib/twilio/rails/models/response.rb', line 105

def answer_yes?
  transcription_matches?(Twilio::Rails.config.yes_responses)
end

#first_for_phone_call?(include_timeouts: true) ⇒ true, false

Returns true if this response is the first time the caller has encountered the given prompt for this phone call. The parameter include_timeouts defaults to true and flags whether or not to include responses that are timeouts. If the response is unsaved it will always return false.

Parameters:

  • include_timeouts (true, false) (defaults to: true)

    Whether or not to include timeouts responses.

Returns:

  • (true, false)

    if this is the first time the caller has encountered this prompt in this phone call.



124
125
126
127
128
129
# File 'lib/twilio/rails/models/response.rb', line 124

def first_for_phone_call?(include_timeouts: true)
  return false unless id
  finder = phone_call.responses.prompt(prompt_handle).order(id: :asc)
  finder = finder.where(timeout: false) if !include_timeouts
  finder.first&.id == id
end

#first_for_phone_caller?(include_timeouts: true) ⇒ true, false

Returns true if this response is the first time the caller has encountered the given prompt across any phone call. The parameter include_timeouts defaults to true and flags whether or not to include responses that are timeouts. If the response is unsaved it will always return false.

Parameters:

  • include_timeouts (true, false) (defaults to: true)

    Whether or not to include timeouts responses.

Returns:

  • (true, false)

    if this is the first time the caller has encountered this prompt in any phone call.



137
138
139
140
141
142
# File 'lib/twilio/rails/models/response.rb', line 137

def first_for_phone_caller?(include_timeouts: true)
  return false unless id
  finder = phone_caller.responses.prompt(prompt_handle).tree(phone_call.tree_name).order(id: :asc)
  finder = finder.where(timeout: false) if !include_timeouts
  finder.first&.id == id
end

#from?(tree:) ⇒ true, false

Checks if the response is for a given tree or trees or tree names.

Parameters:

Returns:

  • (true, false)

    true if the response is for the given tree.



49
50
51
52
53
# File 'lib/twilio/rails/models/response.rb', line 49

def from?(tree:)
  trees = Array(tree).map { |t| t.is_a?(Twilio::Rails::Phone::Tree) ? t.name : t.to_s }

  trees.include?(self.phone_call.tree_name)
end

#integer_digitsInteger?

Returns the digits as an Integer entered through the keypad during a phone call as gather:. Returns nil if the response has no digits, or if the response contains * or # characters. Useful for doing branching logic within a phone tree, such as "Press 2 for sales..." etc..

Returns:

  • (Integer, nil)

    The digits as entered by the caller or nil if not found or not present.



60
61
62
63
64
# File 'lib/twilio/rails/models/response.rb', line 60

def integer_digits
  return nil unless digits.present?
  return nil unless digits =~ /\A[0-9]+\Z/
  digits.to_i
end

#is?(tree:, prompt:) ⇒ true, false

Checks if the response is for a given prompt or promts, and given tree or trees or tree names.

Parameters:

  • tree (Twilio::Rails::Phone::Tree, String, Symbol, Array)

    The tree or tree name or an array of them.

  • prompt (String, Symbol, Array)

    The prompt handle or an array of them.

Returns:

  • (true, false)

    true if the response is for the given prompt and tree.



39
40
41
42
43
# File 'lib/twilio/rails/models/response.rb', line 39

def is?(tree:, prompt:)
  trees = Array(tree).map { |t| t.is_a?(Twilio::Rails::Phone::Tree) ? t.name : t.to_s }

  from?(tree: tree) && Array(prompt).map(&:to_s).reject(&:blank?).include?(self.prompt_handle)
end

#pound_star?true, false Also known as: star_pound?

Returns true if the digits entered through the keypad during a phone call as gather: contain only * or #

Returns:

  • (true, false)

    true if the digits are only * or #.



69
70
71
# File 'lib/twilio/rails/models/response.rb', line 69

def pound_star?
  !!(digits =~ /\A[#*]+\Z/)
end

#transcription_matches?(*patterns) ⇒ true, false

Checks if any of the passed in patterns match the transcription. Will always return false if the transcription is blank. Patterns can be a String, Symbol, Regexp, or an Array of any of those. Will raise ArgumentError if no transcriptions are passed in.

Parameters:

  • patterns (String, Symbol, Regexp, Array)

    The patterns to match against.

Returns:

  • (true, false)

    true if any of the patterns match the transcription.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/twilio/rails/models/response.rb', line 80

def transcription_matches?(*patterns)
  patterns = Array(patterns).flatten
  raise ArgumentError, "transcription must match against at least one pattern" if patterns.blank?

  return false if transcription.blank?

  patterns.each do |pattern|
    case pattern
    when Regexp
      return true if pattern.match?(transcription)
    when String, Symbol
      return true if transcription.downcase.include?(pattern.to_s.downcase)
    else
      raise ArgumentError, "can only match a String or Regexp"
    end
  end

  false
end