Class: Twilio::Rails::Phone::BaseTree
- Inherits:
-
Object
- Object
- Twilio::Rails::Phone::BaseTree
- Defined in:
- lib/twilio/rails/phone/base_tree.rb
Overview
Base class for all phone trees which provides the DSL to define a tree. To define a phone tree start by generating a sublcass.
rails generate twilio:rails:phone_tree LeaveFeedback
This will create a new class in app/phone_trees/leave_feedback_tree.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.phone_trees.register { LeaveFeedbackTree }
Then define the tree using the DSL methods provided by this class. For example:
class LeaveFeedbackTree < Twilio::Rails::Phone::BaseTree
voice "Polly.Matthew-Neural"
"Sorry, you don't appear to be there. Goodbye."
unanswered_call ->(phone_call) { MyMailer.send_followup(phone_call).deliver_later }
finished_call ->(phone_call) { MyMailer.send_followup(phone_call).deliver_later }
invalid_phone_number "Sorry, we can only accept calls from North America. Goodbye."
greeting message: "Hello, and thank you for calling.",
prompt: :leave_feedback
prompt :leave_feedback,
message: "Please leave your feedback after the tone, and press pound when you are finished.",
gather: {
type: :voice,
timeout: 30,
transcribe: true,
},
after: ->(response) {
if MyServiceObject.new(response.phone_caller).
{ prompt: :followup_message }
else
{
message: "Thank you for your feedback. Have a nice day.",
hangup: true,
}
end
}
prompt :followup_message,
message: ->(response) {
[
{ play: "http://example.com/followup_message_sound.mp3" },
{ say: MyServiceObject.new(response.phone_caller). },
]
},
after: {
message: "Thank you. Have a nice day.",
hangup: true,
}
end
Class Method Summary collapse
-
.final_timeout_message(message) ⇒ Object
The
message:
object that is played to the caller if the phone tree is expecting input but none is received. -
.finished_call(proc) ⇒ nil
Accepts a proc which will be called when a call is completed, unanswered, or any state not in progress.
-
.greeting(message: nil, prompt:) ⇒ nil
The entrypoint and first call for any incoming or outgoing phone call.
-
.invalid_phone_number(message) ⇒ Object
The
message:
object that played to the caller if a call from an invalid phone number is received. -
.macros ⇒ Twilio::Rails::Phone::TreeMacros
A module of convenience macros used in prompts to prevent repetition or wordy tasks.
-
.prompt(prompt_name, message: nil, gather: nil, after:) ⇒ Object
Defines a prompt in the phone tree.
-
.tree ⇒ Twilio::Rails::Phone::Tree
The instance of Tree built from the DSL.
-
.tree_name ⇒ String
The string name of the tree used to look it up and identify it in the registry and used in the routes.
-
.unanswered_call(proc) ⇒ nil
Accepts a proc which will be called when a call goes unanswered, or is answered by an answering machine.
-
.voice(voice_name) ⇒ nil
Accepts a string with the voice parameter to be used throughout the phone tree, unless overridden for a given message.
Class Method Details
.final_timeout_message(message) ⇒ Object
The message:
object that is played to the caller if the phone tree is expecting input but none is
received. The default number of attempts before this is called is 3, configured in final_timeout_attempts
.
The default value is a simple "Goodbye."
75 76 77 78 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 75 def () tree.config[:final_timeout_message] = nil end |
.finished_call(proc) ⇒ nil
Accepts a proc which will be called when a call is completed, unanswered, or any state not in progress.
The proc will be called asynchronously in a job. The proc will be passed the
Models::PhoneCall instance for the call. It is called after the call has been completed
so cannot control the flow of the call. It is intended to be used as a hook to handle application logic for
when a call finishes and is no longer in progress. The default is nil
and no action is taken.
182 183 184 185 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 182 def finished_call(proc) tree.finished_call = proc nil end |
.greeting(message: nil, prompt:) ⇒ nil
The entrypoint and first call for any incoming or outgoing phone call. It should only be called once as
there is only one greeting. Subsequent calls will overwrite the earlier ones. It accepts an optional
message:
object which will be played to the caller. See the documentation for prompt for what a message
can contain. It then accepts a required prompt:
which is the next prompt in the flow of the call.
88 89 90 91 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 88 def greeting(message: nil, prompt:) tree.greeting = Twilio::Rails::Phone::Tree::After.new(message: , prompt: prompt) nil end |
.invalid_phone_number(message) ⇒ Object
The message:
object that played to the caller if a call from an invalid phone number is received. This can
be an empty or "Unknown" number, but the important case here is a number from outside of North America. This
is currently a limitation of theframework. The default is nil
and no action is taken. See the
documentation for prompt for what a message object can contain.
193 194 195 196 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 193 def invalid_phone_number() tree.config[:invalid_phone_number] = nil end |
.macros ⇒ Twilio::Rails::Phone::TreeMacros
A module of convenience macros used in prompts to prevent repetition or wordy tasks. See TreeMacros for the available methods. The macros do not have access to any instance information and must be passed any context they require.
Additional macros can be added through the application config. See Configuration#include_phone_macros.
221 222 223 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 221 def macros Twilio::Rails::Phone::TreeMacros end |
.prompt(prompt_name, message: nil, gather: nil, after:) ⇒ Object
Defines a prompt in the phone tree. It accepts a required name:
which must be unique within the tree.
It accepts an optional message:
object which will be played to the caller. A message must be one of:
nil
: No message will be played.String
: A string that will be read to the caller using text-to-speech. This is the equivalent of{ say: "a string" }
.Hash
: A hash that contain only the following keys:{ say: "hello" }
: A string that will be read to the caller using text-to-speech. Optionally also accepts avoice:
key which will override the default voice for this message.{ play: "https://example.com/sound.mp3" }
: A URL to a "wav" or "mp3" file that will be played to the caller via Twilio.{ pause: 1 }
: Pause in silence for the given number of seconds.
Array
: An array that contains any number of the above.Proc
: A proc that will be called when the prompt is reached. The prompt will receive the previous Models::Response instance as an argument. The proc must return one of the above.
It accepts an optional gather:
object which, if present, will be used to gather input from the caller.
After the optional message completes, the gather will collect the input. The gather object must be a hash
with one of the following types:
{ type: :digits }
: Collects one or more integer digits from the caller's keypad. Those digits will be stored in thedigits
field of the Models::Response instance. Digits accepts the following configuration keys::timeout
: The number of seconds to wait for input before timing out and falling through to theafter:
. The default is 5.:number
: The number of digits to collect. The default is 1.:interrupt
: Weather pressing a key will interrupt the message, or if the gather will not start until the message is complete. The default isfalse
.
{ type: :voice }
: Records and collects the phone caller's voice as audio. The framework handles updating theurl
and fetching the audio file as a Models::Recording attached to the response instance. However, this all happens asynchronously with no guarantee of time or success. Voice accepts the following configuration keys::length
: The number of seconds to record. The default is 10.:transcribe
: A boolean if Twilio should attempt to transcribe the audio and send it back as text. The framework handles this all asynchronously and will update thetranscription
field. Default isfalse
.:profanity_filter
: Replaces any profanity in the transcription with ***. Default isfalse
.
{ type: :speech }
: Collects speech from the caller as text using a specialzed model designed to better identify utterances of digits, commands, conversations, etc.. This does not collect audio files, and is more expensive, but returns theresponse.transcription
in realtime which can be immediately used in the call flow. Speech accepts the following configuration keys::language
: The language of the caller. The default is "en-US".:speech_model
: The model to use for the speech recognition. Accepts "default", "numbers_and_commands", "phone_call", "experimental_conversations", and "experimental_utterances". The default is "default". See the Twilio documentation for details. https://www.twilio.com/docs/voice/twiml/gather#speechmodel:enhanced
: A boolean if the enhanced model should be used. Results are better but the cost is higher. The default isfalse
.:timeout
: The number of seconds to wait for input before timing out and falling through to theafter:
. The default is 5.:speech_timeout
: Accepts an interger or "auto". If both this andtimeout
is set, Twilio will usetimeout
for digits and this value for voice or speech.:profanity_filter
: Replaces any profanity in the transcription with ***. Default isfalse
.
It accepts an required after:
object which, will be used to determine the next prompt in the call flow.
The after object must be one of:
Symbol
: The name of the next prompt in the flow.Hash
: A hash that contains::message
: An optional message to play before the next prompt. See the above documentation for what a message can contain.:prompt
: The name of the next prompt in the flow.:hangup
: A boolean if the call should be hung up after the message. Only one of prompt and hangup can be present.
Proc
: A proc that will be called after the message and gather have been called. The proc will receive the current Models::Response instance as an argument. The proc must return one of the above.
156 157 158 159 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 156 def prompt(prompt_name, message: nil, gather: nil, after:) tree.prompts[prompt_name] = Twilio::Rails::Phone::Tree::Prompt.new(name: prompt_name, message: , gather: gather, after: after) nil end |
.tree ⇒ Twilio::Rails::Phone::Tree
210 211 212 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 210 def tree @tree ||= Twilio::Rails::Phone::Tree.new(tree_name) end |
.tree_name ⇒ String
The string name of the tree used to look it up and identify it in the registry and used in the routes. It must be unique and use URL safe characters. It defaults to the class name but can be overridden here.
202 203 204 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 202 def tree_name self.name.demodulize.underscore.sub(/_tree\z/, "") end |
.unanswered_call(proc) ⇒ nil
Accepts a proc which will be called when a call goes unanswered, or is answered by an answering machine.
The proc will be called asynchronously in a job. The proc will be passed the
Models::PhoneCall instance for the call. It is called after the call has been completed
so cannot control the flow of the call. It is intended to be used as a hook to handle application logic for
unanswered calls. The default is nil
and no action is taken.
169 170 171 172 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 169 def unanswered_call(proc) tree.unanswered_call = proc nil end |
.voice(voice_name) ⇒ nil
Accepts a string with the voice parameter to be used throughout the phone tree, unless overridden for a given message. See the Twilio documentation for a list of available voices. The voices are dependent on locale, and can also accept Amazon Polly voices. The default is "male". https://www.twilio.com/docs/voice/twiml/say/text-speech
67 68 69 70 |
# File 'lib/twilio/rails/phone/base_tree.rb', line 67 def voice(voice_name) tree.config[:voice] = voice_name nil end |