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:
- 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).
CallHandlersubclass 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.
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