Module: Twilio::Rails::Phone::TreeMacros
Overview
This module is available as ‘macros` in context of generating phone trees. It provides a set of shortcuts for common or verboase actions. It can be extended with custom macros using the config option Configuration#include_phone_macros
Instance Method Summary collapse
-
#answer_no?(string) ⇒ true, false
Matches if the entire passed in string is included in the Configuration#no_responses and is considered a “no”.
-
#answer_yes?(string) ⇒ true, false
Matches if the entire passed in string is included in the Configuration#yes_responses and is considered a “yes”.
-
#answers_no ⇒ Array<String>
The list of configured answers that are considered “no” from Configuration#no_responses.
-
#answers_yes ⇒ Array<String>
The list of configured answers that are considered “yes” from Configuration#yes_responses.
-
#digit_gather_interruptable(timeout: 6) ⇒ Hash
Gather one digit, allowing the current speech to be interrupted and stopped when a digit is pressed, with a configurable timeout that defaults to 6 seconds.
-
#digits(num) ⇒ String
Split a number into its digits and join them with commas, in order for it to be read out as a list of digits.
-
#numbered_choice_response_includes?(choices, response:) ⇒ true, false
Validates if the response object includes a digit that is within the range of the choices array.
-
#numbered_choices(choices, prefix: nil) ⇒ Array<String>
Format a list of choices such that they are a numbered list for a phone tree menu, and can be passed directly into a ‘say:`.
-
#pause(seconds = nil) ⇒ Hash
Pause for a number of seconds, defaults to 1 second.
-
#play_public_file(filename) ⇒ Hash
Wraps the result of #public_file in a hash that can be used directly as a ‘message:`.
-
#public_file(filename) ⇒ String
Finds and validates the existence of a file in the ‘public` folder.
-
#say(&block) ⇒ Twilio::Rails::Phone::Tree::Message
Expose a TwiML::Say node to be used in a ‘message:` block.
Instance Method Details
#answer_no?(string) ⇒ true, false
Matches if the entire passed in string is included in the Configuration#no_responses and is considered a “no”.
114 115 116 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 114 def answer_no?(string) answers_no.include?((string || "").downcase.strip.gsub(/[.,!?]/, "")) end |
#answer_yes?(string) ⇒ true, false
Matches if the entire passed in string is included in the Configuration#yes_responses and is considered a “yes”.
98 99 100 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 98 def answer_yes?(string) answers_yes.include?((string || "").downcase.strip.gsub(/[.,!?]/, "")) end |
#answers_no ⇒ Array<String>
The list of configured answers that are considered “no” from Configuration#no_responses.
105 106 107 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 105 def answers_no Twilio::Rails.config.no_responses end |
#answers_yes ⇒ Array<String>
The list of configured answers that are considered “yes” from Configuration#yes_responses.
89 90 91 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 89 def answers_yes Twilio::Rails.config.yes_responses end |
#digit_gather_interruptable(timeout: 6) ⇒ Hash
Gather one digit, allowing the current speech to be interrupted and stopped when a digit is pressed, with a configurable timeout that defaults to 6 seconds.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 15 def digit_gather_interruptable(timeout: 6) timeout = timeout.to_i.presence || 6 timeout = 6 if timeout < 1 { type: :digits, timeout: timeout.to_i.presence || 6, number: 1, interrupt: true, finish_on_key: "", } end |
#digits(num) ⇒ String
Split a number into its digits and join them with commas, in order for it to be read out as a list of digits.
35 36 37 38 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 35 def digits(num) return "" if num.blank? num.to_s.split("").join(", ") end |
#numbered_choice_response_includes?(choices, response:) ⇒ true, false
Validates if the response object includes a digit that is within the range of the choices array. This pairs directly with #numbered_choices for creating menus and validating the input. The array of choices must be larger than 1 and less than 10, otherwise a Error will be raised.
false also if there are no digits or the digit is out of range.
80 81 82 83 84 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 80 def numbered_choice_response_includes?(choices, response:) raise Twilio::Rails::Phone::Error, "`numbered_choice_response_includes?` macro got an empty array" if choices.empty? raise Twilio::Rails::Phone::Error, "`numbered_choice_response_includes?` macro cannot be more than 9" if choices.length > 9 !!(response.integer_digits && response.integer_digits > 0 && response.integer_digits <= choices.length) end |
#numbered_choices(choices, prefix: nil) ⇒ Array<String>
Format a list of choices such that they are a numbered list for a phone tree menu, and can be passed directly into a ‘say:`. This pairs perfectly with #numbered_choice_response_includes? for creating menus. The array of choices must be larger than 1 and less than 10, otherwise a Error will be raised.
65 66 67 68 69 70 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 65 def numbered_choices(choices, prefix: nil) raise Twilio::Rails::Phone::Error, "`numbered_choices` macro got an empty array" if choices.empty? raise Twilio::Rails::Phone::Error, "`numbered_choices` macro cannot be more than 9" if choices.length > 9 prefix ||= "For" choices.each_with_index.map { |choice, index| "#{ prefix } #{ choice }, press #{ index + 1 }." }.join(" ") end |
#pause(seconds = nil) ⇒ Hash
Pause for a number of seconds, defaults to 1 second. Useful when putting space between segments of speech.
44 45 46 47 48 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 44 def pause(seconds=nil) { pause: (seconds.presence || 1), } end |
#play_public_file(filename) ⇒ Hash
Wraps the result of #public_file in a hash that can be used directly as a ‘message:`.
141 142 143 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 141 def play_public_file(filename) { play: public_file(filename) } end |
#public_file(filename) ⇒ String
Finds and validates the existence of a file in the ‘public` folder. Formats that link to include the configured hose from Configuration#host, and returns a fully qualified URL to the file. This is useful for playing audio files in a `message:` block. If the file is not found Error is raised.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 126 def public_file(filename) filename = filename.gsub(/^\//, "") local_path = ::Rails.public_path.join(filename) if File.exist?(local_path) "#{ ::Twilio::Rails.config.host }/#{ filename }" else raise Twilio::Rails::Phone::Error, "Cannot find public file '#{ filename }' at #{ local_path }" end end |
#say(&block) ⇒ Twilio::Rails::Phone::Tree::Message
Expose a TwiML::Say node to be used in a ‘message:` block. This can be used to form Speech Synthesis Markup Language (SSML) to be used with Amazon Polly. Note that SSML is only available with some Polly voices, and only some tags are supported. Twilio will return server errors if used incorrectly. See the Twilio documentation for more information: www.twilio.com/docs/voice/twiml/say/text-speech#ssml-with-amazon-polly
160 161 162 |
# File 'lib/twilio/rails/phone/tree_macros.rb', line 160 def say(&block) Twilio::Rails::Phone::Tree::Message.new(&block) end |