Module: Twilio::Rails::Models::PhoneCaller

Extended by:
ActiveSupport::Concern
Included in:
PhoneCaller
Defined in:
lib/twilio/rails/models/phone_caller.rb

Overview

The core identity object, uniquely identifying an individual by their phone number. All ingoing or outgoing phone calls or SMS messages are associated to a phone caller. A phone caller is automatically created when any phone call or SMS message is sent or received.

Instance Method Summary collapse

Instance Method Details

#inbound_calls_for(tree) ⇒ Array<Twilio::Rails::Models::PhoneCall>

Returns All inbound phone calls for the given phone tree or tree name.

Returns:



35
36
37
38
# File 'lib/twilio/rails/models/phone_caller.rb', line 35

def inbound_calls_for(tree)
  tree = tree.is_a?(Twilio::Rails::Phone::Tree) ? tree.name : tree
  phone_calls.inbound.tree(tree)
end

#locationString

Returns A well formatted string with the city/state/country of the phone caller, if available.

Returns:

  • (String)

    A well formatted string with the city/state/country of the phone caller, if available.



30
31
32
# File 'lib/twilio/rails/models/phone_caller.rb', line 30

def location
  phone_calls.inbound.last&.location
end

#outbound_calls_for(tree) ⇒ Array<Twilio::Rails::Models::PhoneCall>

Returns All outbound phone calls for the given phone tree or tree name.

Returns:



41
42
43
44
# File 'lib/twilio/rails/models/phone_caller.rb', line 41

def outbound_calls_for(tree)
  tree = tree.is_a?(Twilio::Rails::Phone::Tree) ? tree.name : tree
  phone_calls.outbound.tree(tree)
end

#response_digits(prompt:, tree:) ⇒ String?

Returns the digits as a ‘String` as entered through the keypad during a phone call as `gather:`. Returns `nil` if the response is not found, if the response has no digits, or if the response was a timeout. Can include both `*` and `#` characters if the caller pressed them.

Parameters:

  • prompt (String, Symbol)

    The prompt handle to query.

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

    The tree or name of the tree to query.

Returns:

  • (String, nil)

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



58
59
60
61
62
# File 'lib/twilio/rails/models/phone_caller.rb', line 58

def response_digits(prompt:, tree:)
  response = responses.tree(tree).where(prompt_handle: prompt, timeout: false).last
  return nil unless response
  response.digits
end

#response_for(prompt:, tree:) ⇒ Twilio::Rails::Models::Response?

Finds the most recent Response for the given prompt and tree. This is useful for building phone trees and finding previous responses to prompts. Returns ‘nil` if no response is found.

Parameters:

  • prompt (String, Symbol)

    The prompt handle to find the response for.

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

    The tree or name of the tree in which to find the response.

Returns:



94
95
96
# File 'lib/twilio/rails/models/phone_caller.rb', line 94

def response_for(prompt:, tree:)
  responses.tree(tree).where(prompt_handle: prompt).last
end

#response_integer_digits(prompt:, tree:) ⇒ Integer?

Returns the digits as an ‘Integer` entered through the keypad during a phone call as `gather:`. Returns `nil` if the response is not found, if the response has no digits, if the response was a timeout, or if the response contains `*` or `#` characters. Useful for doing branching logic within a phone tree, such as “Press 2 for sales…” etc..

Parameters:

  • prompt (String, Symbol)

    The prompt handle to query.

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

    The tree or name of the tree to query.

Returns:

  • (Integer, nil)

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



72
73
74
75
76
# File 'lib/twilio/rails/models/phone_caller.rb', line 72

def response_integer_digits(prompt:, tree:)
  response = responses.tree(tree).where(prompt_handle: prompt, timeout: false).last
  return nil unless response
  response.integer_digits
end

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

Checks if this phone caller has ever reached a response in a given phone tree. This is useful for building phone trees and determining if a phone caller has reached a certain point in the tree before or not.

Parameters:

  • prompt (String, Symbol)

    The prompt handle to query.

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

    The tree or name of the tree to query.

Returns:

  • (true, false)

    If the response has been reached or not.



84
85
86
# File 'lib/twilio/rails/models/phone_caller.rb', line 84

def response_reached?(prompt:, tree:)
  response_for(prompt: prompt, tree: tree).present?
end

#sms_conversationsArray<Twilio::Rails::Models::SmsConversation>

Returns All SMS conversations for the phone caller.

Returns:

  • (Array<Twilio::Rails::Models::SmsConversation>)

    All SMS conversations for the phone caller.



47
48
49
# File 'lib/twilio/rails/models/phone_caller.rb', line 47

def sms_conversations
  Twilio::Rails.config.sms_conversation_class.phone_number(self.phone_number)
end