O'Reilly Emerging Telephony

oreilly.comSafari Books Online.Conferences.
advertisement
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA
AddThis Social Bookmark Button

Print Subscribe to Telephony Subscribe to Newsletters

Hacking Asterisk and Rails with RAGI
Pages: 1, 2

Rails + Asterisk = Peanut Butter + Chocolate

What could be a better way to create these new apps and services than to combine Asterisk's VoIP handling capabilities with the power and efficiency of Ruby on Rails?



For that purpose, we have RAGI: Ruby Asterisk Gateway Interface. RAGI is a simple API and set of helper classes that facilitate programmable phone logic, or IVR, from a Ruby environment by implementing the Asterisk AGI protocol. In Rails apps, RAGI makes handling phone call interaction something similar to rendering a web page. From a systems point of view, it allows you to attach your Asterisk server to your Rails server something like this:

figure 2

Diagram Notes:

  • Phone calls come in to your Asterisk server, and control over how to handle the call (ask the user a question, play a sound) is handed to your web application server.
  • Ruby on Rails is configured to load up RAGI when it starts, so that your calls have access to your entire set of classes defined in your web application (whatever you've built for user registration, data feeds, tagging objects, etc., will be available inside of the phone call session).
  • A CallHandler subclass is created for every type of call session you might need (user checking account balance, checking voice mail, etc.).
  • You can write services that handle incoming calls or place outgoing calls. Your only limit to capacity is bandwidth and CPU.

Programming with RAGI

With RAGI, you spend most of your time focusing on application logic and you get full access to the objects and services you've already defined for the rest of your web application. Spend less time hacking low-level Asterisk and VoIP configurations, and more time on user experience!

Let's take a look at the body of a call handler to get a sense of what this looks like:


module MyCallHandler
  class CallHandler < RAGI::CallHandler

# when someone calls the number, 
# the call routes to this "dialup" method
def dialup
  answer()
      
    # Who is calling?  Read the caller id
    user_phone_number = @params[RAGI::CALLERID]
    
    # Look up the user in the db using Rails.
    # Since the db has a "phone_number" field, Rails
    # automatically provides a "find_by_phone_number" method
    # on the User class -- gotta love Rails!
    user = User.find_by_phone_number(user_phone_number)
    
    # Service logic here...
    #    if (user.account_balance <= 0) --> hangUp
    #    else --> speak "hello" + user.first_name
    #    Play their voice mail
    #    Ask for key presses
    #    Connect this call to conference call
    #    Etc.
    hangUp()
  end
end

RAGI exposes most of the major functions and capabilities of Asterisk itself. And if what's already there is not enough, you can add and contribute back, since RAGI is open source. The major API functions are:

  • Play sounds.
  • Record sounds.
  • Place a call (can be processed through RAGI when answered).
  • Forward a caller to another number by placing a call.
  • Listen for key presses (DTMF signals).
  • Send key presses (DTMF signals).
  • Text to speech.

Summary

VoIP connectivity has quickly become ubiquitous. Its openness and low cost means that it can be combined with traditional web infrastructure to create powerful new communications applications and services. In this article, I've sketched out what those new services may look like, and illustrated an approach to developing such applications using open source software such as Asterisk and Ruby on Rails.

Would you like to learn more?

I'll be doing a workshop on RAGI at the O'Reilly Emerging Telephony conference, January 24 to 26 in San Francisco. Also, in the new year, look for a complete tutorial on O'Reilly about using RAGI. In the meantime, please feel free to get started on your own by visiting the RAGI developer pages.

Joe Heitzeberg is founder of SnapVine, Inc., a pioneering mobile voice applications startup.


Return to O'Reilly Emerging Telephony


Comments on this article
Full Threads Oldest First

Showing messages 1 through 5 of 5.

  • P.S.
    2006-07-31 19:09:34  brandondrew [View]

    By the way, this is a great article: it not only is informative about how to make two useful technologies work together, but goes beyond describing what is and discusses the implications of what could be.
  • where's the bias?
    2006-07-31 19:05:49  brandondrew [View]


    "Sad, in this day and age, that your article is so unnecessarily US-biased."

    BackSeat:
    I'm not sure what argument you're trying to make, except perhaps that the writer of this article ought to have written everything catering to you. Your evidence that the piece is "US-biased" is that you haven't heard of the things he mentions. Is it his fault that you haven't heard of any of these things? Is he supposed to replace "411" with local equivalents for every country on earth, or just the UK, since you happen to be in the UK, and you're more important than anyone else?

    When I read The Register or the BBC's web site, I don't expect them to write from a U.S. point of view, or to avoid refereneces to things from the UK, or to spell "color" the way I spell it, or anything like that. So why should a U.S. writer be expected to write from a U.K. point of view?

    Over here in England, technology has finally arrived. One family just outside London now has a "computer", and two people in my very street now have flushing toilets. We're not that far behind you!

    You then go on to sarcastically mention how technology is arriving in the UK, as if he implied or assumed otherwise. There's nothing in his article to suggest that, so apparently you just assume that he (or all Americans) think the UK is backwards, and it's your job to correct us, and in so doing imply that we're idiots.

    You're being both illogical and rude.
  • Worldwide from LA to FLA
    2006-01-04 00:27:48  BackSeat [View]

    You say, "In this world, hackers and entrepreneurs everywhere...", but list a load of things I've never heard of: "Vonage, AT&T CallVantage, BroadVoice" (who?), "a better kind of 411" (what's 411?), "to use on Craigslist" (what's that?).

    Sad, in this day and age, that your article is so unnecessarily US-biased. Over here in England, technology has finally arrived. One family just outside London now has a "computer", and two people in my very street now have flushing toilets. We're not that far behind you!
  • OpenWengo
    2005-12-20 16:11:46  patcito [View]

    "Currently, Gizmo is the most open VoIP client out there"
    No Gizmo is not the most open, the most open is OpenWengo, it's fully GPL and only use GPL and LGPL libraries, you just can't get more open and free than that. You can use your account from asterisk and use different services from the clients. Here it is:
      http://www.openwengo.com
    I hope you will correct your article.
  • Bruce Stewart photo good stuff!
    2005-12-20 12:42:32  Bruce Stewart | O'Reilly AuthorO'Reilly Blogger [View]

    I'm really looking forward to your workshop at ETel, this sounds like a great development platform for quick and easy voice apps.


Search Emerging Telephony

Search

Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement
Sign up today to receive special discounts,
product alerts, and news from O'Reilly.
Privacy Policy >
View Sample Newsletter >
  • Youtube
  • http://www.youtube.com/OreillyMedia
  • Twitter
  • Subscribe
  • View All RSS Feeds >
O'Reilly Media

800-889-8969 or 707-827-7019
Monday-Friday 7:30am-5pm PT
©2011, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
  • About O'Reilly
  • Academic Solutions
  • Contacts
  • Customer Service
  • Careers
  • Press Room
  • Privacy Policy
  • Terms of Service
  • Writing for O'Reilly
  • Community
  • Authors
  • Forums
  • Membership
  • Newsletters
  • RSS Feeds
  • User Groups
  • Partner Sites
  • makezine.com
  • makerfaire.com
  • craftzine.com
  • igniteshow.com
  • PayPal Developer Zone
  • O'Reilly Insights on Forbes.com