Asterisk & Tropo and a Single Adhearsion Dialplan

October 8th, 2010 by Jason Goecke

A key goal of Tropo AGItate is to allow you to use Tropo seamlessly with an existing Asterisk server. With AGItate you may add any Tropo feature to your existing Asterisk server without installing additional Asterisk modules and using the AGI protocol you already know. These features include:

  • Speech Synthesis/TTS in 7 languages and 2 dialects
  • Speech Recognition/ASR in 7 languages and 2 dialects
  • Transcription of audio messages, like voicemail
  • Multi-channel support for SMS, Instant Messaging and Twitter
  • Conferencing
  • and more

The ease of interoperability between Asterisk and the Tropo cloud highlights exactly why we run and make available an open SIP network. Every application created gets a SIP address automatically:

To show this in action I have chosen Adhearsion as the framework to serve diaplans for both Asterisk and Tropo to process a single caller on an Asterisk server. Here is a quick overview of how it works:

Through the use of SIP, SIP headers and call tagging in Adhearsion, you may write a single dialplan that handles the call and interaction between the Asterisk and Tropo cloud seamlessly. Here is a quick walkthrough of the dialplan itself and the dialplan taking calls in action:

The entire dialplan may be seen here:

# Serves up FastAGI to your Asterisk server
asterisk_agi {
  # Add a custom SIP Header to the session so that when we send to
  # Tropo we may know which Asterisk call the Tropo call is
  # servicing
  execute 'SIPAddHeader', "x-ahn-id: #{channel}"

  # Send the call to your Tropo AGItate app on Tropo
  # option 'g' is required in order for the call to come back
  # to the dialplan
  dial 'SIP/9991479110@sip.tropo.com', { :options => 'g' }

  # After the call comes back find this call and then
  # grab the tag of the result we are looking for
  this_call = Adhearsion.active_calls.find(channel)
  favorite_muppet = ''
  this_call.tags.each do |tag|
    hash = JSON.parse tag
    favorite_muppet = hash['favorite_muppet'] if hash['favorite_muppet']
  end

  # Play back the appropriate audio file based on the user's input by passing the appropriate value
  # in a custom SIP header
  case favorite_muppet
  when 'kermit'
    # Tropo supports wav/mp3 playback, so pass it a link
    execute 'SIPAddHeader', "x-ahn-mp3file: http://downloads.members.tripod.com/Tiny_Dancer/beingreen.mp3"
  when 'swedish chef'
    execute 'SIPAddHeader', "x-ahn-mp3file: http://dl.dropbox.com/u/25511/Voxeo/TTS-Example/SwedishChef.mp3"
  else
    execute 'SIPAddHeader', "x-ahn-choice: bad"
  end
  dial 'SIP/9991479110@sip.tropo.com', { :options => 'm(silence)' }
}

# Serves up FastAGI to your Tropo AGItate application
tropo_agi {
  # Grab the SIP headers from the Tropo cloud delivered in the first message over AGI and made
  # available in Adhearsion as 'tropo_headers'
  headers = JSON.parse tropo_headers

  if headers['x-ahn-mp3file']
    # If we have the mp3file header, then lets play the fileback, otherwise ask for the input
    play headers['x-ahn-mp3file']
  elsif headers['x-ahn-choice']
    # If the bad choice header is here, we know they did not choose a Muppet they should have
    play 'You silly muppet fan, the best muppets are kermit or the swedish chef. Try again another time. Goodbye.'
  else
    # Invoke the Tropo Speech Synthesis/TTS to speak to the caller
    # https://www.tropo.com/docs/scripting/say.htm
    play 'Welcome to the Muppetorium.'
    # Invoke the Tropo Speech Recognition/ASR to ask the caller for input
    # https://www.tropo.com/docs/scripting/ask.htm
    result = execute 'ask', { :prompt   => 'Which is your favorite muppets character?',
                              :choices  => 'kermit, fozzie, statler, waldorf, oscar, bert, ernie, swedish chef',
                              :attempts => 3,
                              :timeout  => 10 }.to_json

    # Parse the result from Tropo into a Ruby hash
    result = JSON.parse result.split('200 result=')[1]

    # Find the active Adhearsion call object based on the SIP header passed into Tropo
    call = Adhearsion.active_calls.find headers['x-ahn-id']

    # Tag that call object with the value of the speech recognition, hangup the call between Asterisk and Tropo
    # so that Asterisk may continue handling it on its own
    call.tag({ :favorite_muppet => result['interpretation'] }.to_json)
  end
}

Or downloaded here. Thats it! You now have a fully featured Asterisk instance leveraging the best of what the cloud has to offer without adding or purchasing any additional software for your Asterisk box.

To get started with AGItate we created a howto walkthrough that you may see here. Tropo is free for development use, so there is no reason not to give it a try. Enjoy!

Related posts:

  1. Tropo Now Speaks Asterisk Gateway Interface (AGI)
  2. Outbound with Tropo AGItate
  3. Howto Install and Use Tropo AGItate
  4. WebSockets and Tropo, Putting the Real-Time in Your Communications
  5. Update to AGItate

Tags: , , , , , , , , , , ,

Leave a Reply

Please note: By submitting a comment you agree to comply with our Comment Policy. We welcome all comments, positive or negative, but do reserve the right to remove all or part of blog comments that do not comply with our policy.

Additionally, the first time you leave a comment on this blog, it will be held for moderation. After that first comment has been approved, future comments will be posted without delay.

Additional comments powered by BackType