Permalink
Browse files

Expanded the support for the AGI STREAM FILE command. Also fixed the …

…SSML for the saydigits method.
  • Loading branch information...
1 parent fecd8cd commit 77011de5b6ae7e4cc25f4da65c3a106bf26ce3ce @jsgoecke jsgoecke committed Mar 11, 2011
Showing with 86 additions and 20 deletions.
  1. +8 −2 README.md
  2. +48 −17 lib/tropo-agitate.rb
  3. +11 −1 spec/tropo-agitate_spec.rb
  4. +19 −0 spec/tropo-commands_spec.rb
View
@@ -195,6 +195,13 @@ These Adhearsion dialplan examples are also available in the 'examples' director
execute 'say', { :prompt => 'I like to have weasels in my cloud.', :voice => 'simon' }.to_json
}
+ # Supports STREAM FILE with and without escape digts
+ # Note, any digits at the end of the string will always be treated as escape digits
+ stream_file_example {
+ raw_response "STREAM FILE tt-monkeys 1234567890*#"
+ raw_response "STREAM FILE tt-monkeys"
+ }
+
# If this is a Tropo call, then all of the SIP headers for the session are available
show_call_data {
ahn_log.tropo_headers_str.debug tropo_headers
@@ -266,8 +273,7 @@ These Adhearsion dialplan examples are also available in the 'examples' director
# Start menu example
# Related Tropo method: https://www.tropo.com/docs/scripting/ask.htm
menu_example {
- menu 'welcome', 'for spanish press 4',
- :timeout => 8.seconds, :tries => 3 do |link|
+ menu 'Welcome. For Shipments press 1. For ordering press 2. For a representative push 3 or for Spanish press 4', :timeout => 8.seconds, :tries => 3 do |link|
link.shipment_status 1
link.ordering 2
link.representative 3
View
@@ -1,7 +1,7 @@
-%w(rubygems socket json net/http uri).each { |lib| require lib }
+%w(rubygems yaml socket json net/http uri).each { |lib| require lib }
#####
# This Ruby Script Emulates the Asterisk Gateway Interface (AGI)
-# VERSION = '0.1.0'
+# VERSION = '0.1.3'
#####
# If we are testing, then add some methods, $currentCall will be nil if a call did not start this session
@@ -17,7 +17,6 @@ def show(val)
end
#########
-
# @author Jason Goecke
class TropoAGItate
module Helpers
@@ -190,25 +189,28 @@ def dial(options={})
# @return [String] the response in AGI raw form
def file(options={})
check_state
-
+
@wait_for_digits_options = parse_input_string options[:args][0], 16
if @wait_for_digits_options.nil?
- options[:args][0] = options[:args][0][0..-15]
-
- asterisk_sound_url = fetch_asterisk_sound(options[:args][0])
- if asterisk_sound_url
- prompt = asterisk_sound_url
+ prompt, escape_digits = extract_prompt_and_escape_digits(options[:args][0])
+
+ asterisk_sound_url = fetch_asterisk_sound(prompt)
+ prompt = asterisk_sound_url if asterisk_sound_url
+
+ if escape_digits.nil?
+ @current_call.say prompt, :voice => @tropo_voice
+ result = @agi_response + "0 endpos=0\n"
else
- prompt = options[:args][0]
+ response = @current_call.ask prompt, { 'choices' => create_choices(escape_digits), 'choiceMode' => 'keypad' }
+ result = @agi_response + response.value[0].to_s + " endpos=0\n"
end
-
- response = @current_call.ask prompt, { 'choices' => '[1 DIGIT], *, #', 'choiceMode' => 'keypad' }
end
show 'File response', response
- @agi_response + response.value[0].to_s + " endpos=0\n"
+ result
rescue => e
log_error(this_method, e)
end
+ alias :streamfile :file
##
# Grabs all of the SIP headers off of the current session/call
@@ -405,7 +407,7 @@ def say(options={})
def saydigits(options={})
check_state
- ssml = "<say-as interpret-as='vxml:digits'>#{options[:args][0]}</say-as>"
+ ssml = "<speak><say-as interpret-as='vxml:digits'>#{options[:args][0]}</say-as></speak>"
@current_call.say ssml, :voice => @tropo_voice
@agi_response + "0\n"
rescue => e
@@ -597,7 +599,7 @@ def asterisk_sound_files
end
private
-
+
##
# Automatically answers the call/session if not explicityly done
def check_state
@@ -606,12 +608,41 @@ def check_state
raise RuntimeError, '511 result=Command Not Permitted on a dead channel'
when 'RINGING'
@current_call.answer
- # Sleep to allow audio to settle
+ # Sleep to allow audio to settle, in the case of Skype
sleep 2
end
true
end
-
+
+ ##
+ # Converts the choices passed in a STREAM FILE into the requisite comma-delimitted format for Tropo
+ #
+ # @param [required, String] escape_digits to convert
+ def create_choices(escape_digits)
+ choices = ''
+ # 1.3.1 does not have the each_char method on the String class
+ if JRUBY_VERSION == '1.3.1'
+ escape_digits.each_byte { |char| choices = choices + char.chr + ',' }
+ else
+ escape_digits.each_char { |char| choices = choices + char + ',' }
+ end
+ choices.chop
+ end
+
+ ##
+ # Extracts the prompt and escape digits from a STREAM FILE request
+ #
+ # @param [required, String] original_string to extract the prompt and escape digits out of
+ def extract_prompt_and_escape_digits(original_string)
+ original_string.gsub!('"', '')
+ match_data = original_string.match /\d{1,}\#$|\d{1,}$|\d{1,}\*\#$|\d{1,}\#\*$|\d{1,}\*$/
+ if match_data.nil?
+ return original_string, nil
+ else
+ return match_data.pre_match.rstrip, match_data[0]
+ end
+ end
+
##
# Returns the URI location of the Asterisk sound file if it is available
#
View
@@ -132,7 +132,6 @@
command = @tropo_agitate.execute_command('GET VARIABLE "FOOBAR"')
command.should == "200 result=1 (green)\n"
-
command = @tropo_agitate.execute_command("EXEC monitor #{{ 'method' => 'POST', 'uri' => 'http://localhost' }.to_json}")
command.should == "200 result=0\n"
@@ -142,6 +141,17 @@
command = @tropo_agitate.execute_command('EXEC recognizer "en-us"')
command.should == "200 result=0\n"
end
+
+ it "should handle the STREAM FILE requests" do
+ command = @tropo_agitate.execute_command('STREAM FILE tt-monkeys 1234567890*#')
+ command.should == "200 result=57 endpos=0\n"
+
+ command = @tropo_agitate.execute_command('STREAM FILE tt-monkeys')
+ command.should == "200 result=0 endpos=0\n"
+
+ command = @tropo_agitate.execute_command('STREAM STREAMFILE tt-monkeys 1234567890*#')
+ command.should == "200 result=57 endpos=0\n"
+ end
it "should return the account data from a directory lookup" do
@tropo_agitate.fetch_account_data[1].should == '49767'
@@ -107,4 +107,23 @@
options = { :args => ["en-us"] }
@tropo_commands.recognizer(options).should == "200 result=0\n"
end
+
+ it "should support a stream file without escape digits" do
+ options = { :args => ["\"hey there!\""] }
+ @tropo_commands.file(options).should == "200 result=0 endpos=0\n"
+ end
+
+ it "should support a stream file with escape digits" do
+ options = { :args => ["\"hey there!\" \"1234567890#\""] }
+ @tropo_commands.file(options).should == "200 result=57 endpos=0\n"
+
+ options = { :args => ["\"hey there!\" \"1234567890#*\""] }
+ @tropo_commands.file(options).should == "200 result=57 endpos=0\n"
+
+ options = { :args => ["\"hey there!\" \"1234567890*\""] }
+ @tropo_commands.file(options).should == "200 result=57 endpos=0\n"
+
+ options = { :args => ["\"hey there!\" \"1234\""] }
+ @tropo_commands.file(options).should == "200 result=57 endpos=0\n"
+ end
end

0 comments on commit 77011de

Please sign in to comment.