Class: Tropo::Generator
- Includes:
- Helpers
- Defined in:
- lib/tropo-webapi-ruby/tropo-webapi-ruby.rb
Instance Attribute Summary collapse
-
#recognizer ⇒ Object
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition.
-
#text_session ⇒ Object
readonly
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition.
-
#voice ⇒ Object
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition.
-
#voice_session ⇒ Object
readonly
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition.
Class Method Summary collapse
Instance Method Summary collapse
-
#answer(params = {}, &block) ⇒ String?
Prompts initiates a new answer.
-
#ask(params = {}, &block) ⇒ String?
(also: #prompt)
Prompts the user (audio file or text to speech) and optionally waits for a response from the user.
-
#call(params = {}, &block) ⇒ String?
Prompts initiates a new call.
-
#choices(params = {}) ⇒ Object
Choices to give the user on input.
-
#conference(params = {}, &block) ⇒ String?
Creates a conference or pushes a user to an existing conference.
-
#generalLogSecurity(params = {}, &block) ⇒ String?
that can turn off all logging on the Tropo platform.
-
#getConceptByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘concept’ of action named actionName.
-
#getDispositionByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘disposition’ of action named actionName.
-
#getFieldValueByActionNameFieldName(resultactions, actionName, fieldName) ⇒ String
A String representing the value field of fieldName of action named actionName.
-
#getInterpretationByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘interpretation’ of action named actionName.
-
#getUploadStatusByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘uploadStatus’ of action named actionName.
-
#getUtteranceByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘utterance’ of action named actionName.
-
#getValueByActionName(resultactions, actionName) ⇒ String
A String representing the value field ‘value’ of action named actionName.
-
#hangup ⇒ String?
(also: #disconnect)
This function instructs Tropo to “hang-up” or disconnect the current session.
-
#initialize(params = {}, &block) ⇒ Object
constructor
Initializes the Generator class.
-
#message(params = {}, &block) ⇒ String?
Message initiates a new message to a destination and then hangs up on that destination.
-
#on(params = {}, &block) ⇒ Object
Sets event handlers to call a REST resource when a particular event occurs.
-
#parse(response) ⇒ Hash
Parses the JSON string recieved from Tropo into a Ruby Hash, or if already a Ruby Hash parses it with the nicities provided by the gem.
-
#record(params = {}, &block) ⇒ Object
Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded.
-
#redirect(params = {}) ⇒ String?
The redirect function forwards an incoming SIP call to another destination before answering it.
-
#reject ⇒ String?
Allows Tropo applications to reject incoming calls before they are answered.
-
#reset ⇒ nil
Resets the action hash if one desires to reuse the same Generator object.
-
#response ⇒ String
Renders the JSON string to be sent to Tropo to execute a set of actions.
-
#say(value = nil, params = {}) ⇒ String?
Plays a prompt (audio file, text to speech or text for IM/SMS).
-
#start_recording(params = {}) ⇒ String?
(also: #start_call_recording)
Allows Tropo applications to begin recording the current session.
-
#stop_recording ⇒ String?
(also: #stop_call_recording)
Stops the recording of the current session after startCallRecording has been called.
-
#to_hash ⇒ Hash
Returns the current hash object of the response, as opposed to JSON.
-
#transfer(params = {}, &block) ⇒ nil, String
Transfers an already answered call to another destination / phone number.
-
#wait(params = {}, &block) ⇒ String?
The wait function suspends the script’s current thread of execution for the specified length of time, or until the active session changes state (such as a user hanging up), whichever occurs first.
Constructor Details
#initialize ⇒ Object #initialize(params) ⇒ Object #initialize(params, &block) ⇒ Object
Initializes the Generator class
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 35 def initialize(params={}, &block) @response = { :tropo => Array.new } @voice = params[:voice] if params[:voice] @recognizer = params[:recognizer] if params[:recognizer] if block_given? # Lets us know were are in the midst of building a block, so we only rendor the JSON # response at the end of executing the block, rather than at each action @building = true instance_exec(&block) render_response end end |
Instance Attribute Details
#recognizer ⇒ Object
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition
10 11 12 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 10 def recognizer @recognizer end |
#text_session ⇒ Object (readonly)
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition
10 11 12 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 10 def text_session @text_session end |
#voice ⇒ Object
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition
10 11 12 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 10 def voice @voice end |
#voice_session ⇒ Object (readonly)
Set a couple of Booleans to indicate the session type as a convenience Set a default voice for speech synthesis Set a default recognizer for speech recognition
10 11 12 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 10 def voice_session @voice_session end |
Class Method Details
Instance Method Details
#answer(params) ⇒ String? #answer(params, &block) ⇒ String?
Prompts initiates a new answer.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 152 def answer(params={}, &block) if block_given? create_nested_hash('answer', params) instance_exec(&block) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('answer', params) @response[:tropo] << hash end render_response if @building.nil? end |
#ask(params) ⇒ String? #ask(params, &block) ⇒ String? Also known as: prompt
Prompts the user (audio file or text to speech) and optionally waits for a response from the user. If collected, responses may be in the form of DTMF, speech recognition or text using a grammar or free-form text.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 97 def ask(params={}, &block) params = set_language(params) if block_given? @ask_hash = {:ask => build_elements(params)} #create_nested_hash('ask', params) instance_exec(&block) has_params?(@ask_hash[:ask], 'ask', ['choices', 'say']) if @nested_on_hash if @nested_on_hash[:on][0][:ask].nil? @nested_on_hash[:on][0][:ask] = @ask_hash[:ask] elsif @nested_on_hash[:on][0][:ask].is_a? Array @nested_on_hash[:on][0][:ask] << @ask_hash[:ask] else ask = @nested_on_hash[:on][0][:ask] @nested_on_hash[:on][0][:ask] = Array.new @nested_on_hash[:on][0][:ask] << ask @nested_on_hash[:on][0][:ask] << @ask_hash[:ask] end else @response[:tropo] << @ask_hash @ask_hash = nil end else hash = build_action('ask', params) if @nested_on_hash if @nested_on_hash[:on][0][:ask].nil? @nested_on_hash[:on][0][:ask] = hash[:ask] elsif @nested_on_hash[:on][0][:ask].is_a? Array @nested_on_hash[:on][0][:ask] << hash[:ask] else ask = @nested_on_hash[:on][0][:ask] @nested_on_hash[:on][0][:ask] = Array.new @nested_on_hash[:on][0][:ask] << ask @nested_on_hash[:on][0][:ask] << hash[:ask] end else @response[:tropo] << hash end end render_response if @building.nil? end |
#call(params) ⇒ String? #call(params, &block) ⇒ String?
Prompts initiates a new call. May only be used when no call is active.
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 207 def call(params={}, &block) if block_given? create_nested_hash('call', params) instance_exec(&block) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('call', params) @response[:tropo] << hash end render_response if @building.nil? end |
#choices(params = {}) ⇒ Object
Choices to give the user on input
230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 230 def choices(params={}) hash = build_action('choices', params) if @ask_hash @ask_hash[:ask].merge!(hash) elsif @nested_hash @nested_hash[@nested_name.to_sym].merge!(hash) else @response[:tropo] << hash render_response if @building.nil? end end |
#conference(params) ⇒ String? #conference(params, &block) ⇒ String?
Creates a conference or pushes a user to an existing conference
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 275 def conference(params={}, &block) if block_given? create_nested_hash('conference', params) instance_exec(&block) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('conference', params) @response[:tropo] << hash end render_response if @building.nil? end |
#generalLogSecurity(params) ⇒ String? #generalLogSecurity(params, &block) ⇒ String?
that can turn off all logging on the Tropo platform
891 892 893 894 895 896 897 898 899 900 901 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 891 def generalLogSecurity(params={}, &block) if block_given? create_nested_hash('generalLogSecurity', params) instance_exec(&block) @response[:tropo] << @nested_hash else hash = build_action('generalLogSecurity', params) @response[:tropo] << hash end render_response if @building.nil? end |
#getConceptByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘concept’ of action named actionName.
488 489 490 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 488 def getConceptByActionName(resultactions, actionName) resultactions[actionName]['concept'] end |
#getDispositionByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘disposition’ of action named actionName.
461 462 463 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 461 def getDispositionByActionName(resultactions, actionName) resultactions[actionName]['disposition'] end |
#getFieldValueByActionNameFieldName(resultactions, actionName, fieldName) ⇒ String
Returns a String representing the value field of fieldName of action named actionName.
498 499 500 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 498 def getFieldValueByActionNameFieldName(resultactions, actionName, fieldName) resultactions[actionName][fieldName] end |
#getInterpretationByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘interpretation’ of action named actionName.
470 471 472 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 470 def getInterpretationByActionName(resultactions, actionName) resultactions[actionName]['interpretation'] end |
#getUploadStatusByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘uploadStatus’ of action named actionName.
452 453 454 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 452 def getUploadStatusByActionName(resultactions, actionName) resultactions[actionName]['uploadStatus'] end |
#getUtteranceByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘utterance’ of action named actionName.
479 480 481 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 479 def getUtteranceByActionName(resultactions, actionName) resultactions[actionName]['utterance'] end |
#getValueByActionName(resultactions, actionName) ⇒ String
Returns a String representing the value field ‘value’ of action named actionName.
443 444 445 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 443 def getValueByActionName(resultactions, actionName) resultactions[actionName]['value'] end |
#hangup ⇒ String? Also known as: disconnect
This function instructs Tropo to “hang-up” or disconnect the current session.
May trigger these events:
- hangup
- error
297 298 299 300 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 297 def hangup @response[:tropo] << { :hangup => nil } render_response if @building.nil? end |
#message(params) ⇒ String? #message(params, &block) ⇒ String?
Message initiates a new message to a destination and then hangs up on that destination. Also takes a say method in order to deliver a message to that desintation and then hangup.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 336 def (params={}, &block) if block_given? @nested_hash = {:message => build_elements(params)} @nested_name = 'message' instance_exec(&block) has_params?(@nested_hash[:message], 'message', ['say', 'to', 'name']) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('message', params) @response[:tropo] << hash end render_response if @building.nil? end |
#initialize(params) ⇒ Object #initialize(params, &block) ⇒ Object
Sets event handlers to call a REST resource when a particular event occurs
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 370 def on(params={}, &block) if block_given? if @nested_hash create_nested_on_hash(params) instance_exec(&block) if @nested_hash[@nested_name.to_sym][:on].nil? @nested_hash[@nested_name.to_sym][:on] = Array.new end @nested_on_hash[:on].each do |hash| @nested_hash[@nested_name.to_sym][:on] << hash end @nested_on_hash = nil @nested_on_hash_cnt = nil else @on_hash = { :on => build_action('on', params)} instance_exec(&block) @response[:tropo] << @on_hash @on_hash = nil end else create_on_hash hash = build_action('on', params) # @on_hash[:on] << hash if @nested_hash if @nested_hash[@nested_name.to_sym][:on].nil? @nested_hash[@nested_name.to_sym][:on] = Array.new end @nested_hash[@nested_name.to_sym][:on] << hash else @on_hash = nil @response[:tropo] << { :on => hash } render_response if @building.nil? end end end |
#parse(response) ⇒ Hash
Parses the JSON string recieved from Tropo into a Ruby Hash, or if already a Ruby Hash parses it with the nicities provided by the gem
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 413 def parse(response) response = JSON.parse(response) if response.class == String # Check to see what type of response we are working with if response['session'] transformed_response = { 'session' => { } } response['session'].each_pair do |key, value| value = transform_hash value if value.kind_of? Hash transformed_response['session'].merge!(transform_pair(key, value)) end elsif response['result'] transformed_response = { 'result' => { } } response['result'].each_pair do |key, value| value = transform_hash value if value.kind_of? Hash value = transform_array value if value.kind_of? Array transformed_response['result'].merge!(transform_pair(key, value)) end end transformed_response = Hashie::Mash.new(transformed_response) end |
#record(params) ⇒ Object #record(params, &block) ⇒ Object
Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded. If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below. The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set. At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form. If specified, the audio file may also be transcribed and the text returned to you via an email address or HTTP POST/Multipart Form.
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 566 def record(params={}, &block) if block_given? @nested_hash = {:record => build_elements(params)} @nested_name = 'record' instance_exec(&block) has_params?(@nested_hash[:record], 'record', ['url']) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('record', params) @response[:tropo] << hash end render_response if @building.nil? end |
#redirect(params = {}) ⇒ String?
The redirect function forwards an incoming SIP call to another destination before answering it. The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state. Use transfer when working with active answered calls.
594 595 596 597 598 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 594 def redirect(params={}) hash = build_action('redirect', params) @response[:tropo] << hash render_response if @building.nil? end |
#reject ⇒ String?
Allows Tropo applications to reject incoming calls before they are answered. For example, an application could inspect the callerID variable to determine if the caller is known, and then use the reject call accordingly.
607 608 609 610 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 607 def reject @response[:tropo] << { :reject => nil } render_response if @building.nil? end |
#reset ⇒ nil
Resets the action hash if one desires to reuse the same Generator object
624 625 626 627 628 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 624 def reset @response = { :tropo => Array.new } @voice_session = false @text_session = false end |
#response ⇒ String
Renders the JSON string to be sent to Tropo to execute a set of actions
616 617 618 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 616 def response @response.to_json end |
#say(params) ⇒ String? #say(params, &block) ⇒ String?
Plays a prompt (audio file, text to speech or text for IM/SMS). There is no ability to wait for a response from a user. An audio file used for playback may be in one of the following two formats:
Wav 8bit 8khz Ulaw
MP3
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 657 def say(value=nil, params={}) # This will allow a string to be passed to the say, as opposed to always having to specify a :value key/pair, # or still allow a hash or Array to be passed as well if value.kind_of? String params[:value] = value elsif value.kind_of? Hash params = value elsif value.kind_of? Array params = value else raise ArgumentError, "An invalid paramater type #{value.class} has been passed" end response = { :say => Array.new } if params.kind_of? Array params.each do |param| param = set_language(param) hash = build_action('nestedSay', param) response[:say] << hash end else params = set_language(params) if @on_hash || @ask_hash || @nested_hash hash = build_action('nestedSay', params) else hash = build_action('say', params) end response = { :say => hash } end if @ask_hash if @ask_hash[:ask][:say].nil? @ask_hash[:ask][:say] = response[:say] elsif @ask_hash[:ask][:say].is_a? Array @ask_hash[:ask][:say] << response[:say] else say = @ask_hash[:ask][:say] @ask_hash[:ask][:say] = Array.new @ask_hash[:ask][:say] << say @ask_hash[:ask][:say] << response[:say] end elsif @on_hash && @nested_on_hash.nil? @on_hash[:on].merge!(response) elsif @nested_hash && @nested_on_hash.nil? @nested_hash[@nested_name.to_sym].merge!(response) elsif @nested_on_hash if @nested_on_hash[:on][0][:say].nil? @nested_on_hash[:on][0][:say] = response[:say] elsif @nested_on_hash[:on][0][:say].is_a? Array @nested_on_hash[:on][0][:say] << response[:say] else say = @nested_on_hash[:on][0][:say] @nested_on_hash[:on][0][:say] = Array.new @nested_on_hash[:on][0][:say] << say @nested_on_hash[:on][0][:say] << response[:say] end # @nested_on_hash[:on][@nested_on_hash_cnt].merge!(response) # @nested_on_hash_cnt += 1 else @response[:tropo] << response render_response if @building.nil? end end |
#start_recording(params = {}) ⇒ String? Also known as: start_call_recording
Allows Tropo applications to begin recording the current session. The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form.
739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 739 def start_recording(params={}) if block_given? create_nested_hash('start_recording', params) instance_exec(&block) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('start_recording', params) @response[:tropo] << hash end render_response if @building.nil? end |
#stop_recording ⇒ String? Also known as: stop_call_recording
Stops the recording of the current session after startCallRecording has been called
759 760 761 762 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 759 def stop_recording @response[:tropo] << { :stopRecording => nil } render_response if @building.nil? end |
#to_hash ⇒ Hash
Returns the current hash object of the response, as opposed to JSON
841 842 843 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 841 def to_hash @response end |
#transfer(params) ⇒ nil, String #transfer(params, &block) ⇒ nil, String
Transfers an already answered call to another destination / phone number. Call may be transferred to another phone number or SIP address, which is set through the “to” parameter and is in URL format. Supported formats include:
tel: classic phone number (See RFC 2896), must be proceeded by a + and the country code (ie - +14155551212 for a US #)
sip: SIP protocol address
When this method is called the following occurs:
The audio file specified in playvalue is played to the existing call. This could be "hold music", a ring-back sound, etc. The audio file is played up to playrepeat times.
While audio is playing, a new call is initiated to the specified "to" address using the callerID specified.
If answerOnMedia is true, the audio from the new call is connected to the existing call immediately.
The system waits for an answer or other event from the new call up to the timeout.
If the call successfully completes within the timeout, the existing call and new call will be connected, onSuccess will be called, and the transfer call will return a success event.
If the call fails before the timeout, onCallFailure will be called and the method will return an onCallFailure event.
If the call fails due to the timeout elapsing, onTimeout will be called and the method will return a timeout event
823 824 825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 823 def transfer(params={}, &block) if block_given? create_nested_hash('transfer', params) instance_exec(&block) @response[:tropo] << @nested_hash @nested_hash = nil @nested_name = nil else hash = build_action('transfer', params) @response[:tropo] << hash end render_response if @building.nil? end |
#wait(params) ⇒ String? #wait(params, &block) ⇒ String?
The wait function suspends the script’s current thread of execution for the specified length of time, or until the active session changes state (such as a user hanging up), whichever occurs first.
867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/tropo-webapi-ruby/tropo-webapi-ruby.rb', line 867 def wait(params={}, &block) if block_given? create_nested_hash('wait', params) instance_exec(&block) @response[:tropo] << @nested_hash else hash = build_action('wait', params) @response[:tropo] << hash end render_response if @building.nil? end |