Skip to content

Commit

Permalink
Added support for Asterisk protocol error 511 and a sleep when auto-a…
Browse files Browse the repository at this point in the history
…nswer to allow audio settle with the likes of Skype.
  • Loading branch information
jsgoecke committed Oct 11, 2010
1 parent 6ca4c52 commit edcfbf4
Showing 1 changed file with 56 additions and 15 deletions.
71 changes: 56 additions & 15 deletions lib/tropo-agitate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def answer
#
# @return [String] the response in AGI raw form
def ask(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

options[:args][:recognizer] = @tropo_agi_config['tropo']['recognizer'] if options[:args]['recognizer'].nil?
options[:args][:voice] = @tropo_agi_config['tropo']['voice'] if options[:args]['voice'].nil?
Expand Down Expand Up @@ -155,7 +157,9 @@ def callerid(options={})
#
# @return [String] the response in AGI raw form
def dial(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

destinations = parse_destinations(options[:args])
options = { :callerID => '4155551212' }
Expand Down Expand Up @@ -186,7 +190,10 @@ def dial(options={})
#
# @return [String] the response in AGI raw form
def file(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

@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]
Expand Down Expand Up @@ -237,7 +244,9 @@ def hangup
# param [Hash] a hash of items
# @return [String] the response in AGI raw form
def meetme(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

options = options[:args][0].split('|')
@current_call.conference options[0].chop
Expand Down Expand Up @@ -269,7 +278,9 @@ def method_missing(method, *args)
#
# @return [String] the response in AGI raw form
def monitor(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

This comment has been minimized.

Copy link
@bklang

bklang Oct 11, 2010

Contributor

I can't help but wonder if there's a way to do this more cleanly, ie. not repeating these 3 lines in each method. My first thought is to have check_state raise an exception if the state isn't valid and let the code that sends the response ("511...") catch the exception and send the appropriate error.

Let me know if this isn't clear and I'll see about a patch.

@current_call.startCallRecording options[:args]['uri'], options[:args]
@agi_response + "0\n"
Expand All @@ -288,7 +299,12 @@ def monitor(options={})
#
# @return [String] the response in AGI raw form
def playback(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
log 'Yeah!'
show 'Valid State: ', valid_state
return valid_state if valid_state != true
log 'Fuck yeah!'

This comment has been minimized.

Copy link
@bklang

bklang Oct 11, 2010

Contributor

Debugging code? :)


asterisk_sound_url = fetch_asterisk_sound(options[:args][0])
if asterisk_sound_url
Expand All @@ -313,7 +329,9 @@ def playback(options={})
#
# @return [String] the response in AGI raw form
def record(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

options = options[:args][0].split
silence_timeout = strip_quotes(options[options.length - 1]).split('=')
Expand All @@ -337,7 +355,9 @@ def record(options={})
#
# @return [String] the response in AGI raw form
def redirect(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

@current_call.redirect options[:args][0]
@agi_response + "0\n"
Expand Down Expand Up @@ -365,7 +385,9 @@ def reject
#
# @return [String] the response in AGI raw form
def say(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

@current_call.say options[:args]['prompt'], options[:args]
@agi_response + "0\n"
Expand All @@ -381,7 +403,9 @@ def say(options={})
# @param [Hash] options used to build the say
# @return [String] the response in AGI raw form
def saydigits(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

ssml = "<say-as interpret-as='vxml:digits'>#{options[:args][0]}</say-as>"
@current_call.say ssml, :voice => @tropo_agi_config['tropo']['voice']
Expand All @@ -399,7 +423,9 @@ def saydigits(options={})
#
# @return [String] the response in AGI raw form
def sayphonetic(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

text = ''
options[:args][0].split(//).each do |char|
Expand All @@ -422,7 +448,9 @@ def sayphonetic(options={})
#
# @return [String] the response in AGI raw form
def saytime(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

@agi_response + "0\n"
rescue => e
Expand All @@ -438,7 +466,9 @@ def saytime(options={})
#
# @return [String] the response in AGI raw form
def senddtmf(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

base_uri = 'http://hosting.tropo.com/49767/www/audio/dtmf/'
options[:args][0].split(//).each do |char|
Expand Down Expand Up @@ -532,7 +562,9 @@ def wait(options={})
#
# @return [String] the response in AGI raw form
def wait_for_digits(options={})
check_state
valid_state = check_state
# Return the error code if call is disconnected/hungup
return valid_state if valid_state != true

if @wait_for_digits_options.nil?
timeout = strip_quotes(options[:args][0].split(' ')[1]).to_i
Expand Down Expand Up @@ -562,7 +594,16 @@ def asterisk_sound_files
##
# Automatically answers the call/session if not explicityly done
def check_state
@current_call.answer if @current_call.state == 'RINGING'
show "Current state: ", @current_call.state
case @current_call.state
when 'DISCONNECTED'
return "511 result=Command Not Permitted on a dead channel\n"
when 'RINGING'
@current_call.answer
# Sleep to allow audio to settle
sleep 2
end
true
end

##
Expand Down

0 comments on commit edcfbf4

Please sign in to comment.