Asterisk on Rails:
Using RAGI to create voice applications
What is Ruby on Rails?
What is Ruby on Rails?
• Ruby – the programming language– 10 years old, originated in Japan– Interpreted, not statically typed, object-oriented– “more powerful than Perl, and more object-oriented than Python”
• Rails – the framework– A full-stack web framework written using the Ruby programming
language. Everything that you need to write a web application. – “web applications that formerly might have taken weeks or months
to develop can be produced in a matter of days.” - Tim O'Reilly
Rails
• “Convention over configuration”• “Don’t repeat yourself”• Features:
– Model-View-Controller– Simple database access via ActiveRecord– Integrated AJAX– Pretty URLs via routing– Flexible caching– Integrated testing framework– Integrated debugging
Sites that use Rails:http://www.basecamp.comhttp://www.43things.comhttp://www.odeo.comMany more
What is Asterisk?
“Asterisk is an Open Source PBX”
x101 = Amit
x102 = Sally
x103 = Arnaud• Internet
• Phone lines• Linux hardware
• Asterisk
• Optional: TDM card• Phones
• VoIP phones
• Softphones
• SIP
• IAX
• POTS
Call to (415)555-1212…
“Please enter the
extension…”
What could you use Asterisk to do?
• Set up an office with 4 phone lines, 25 extensions and 1 receptionist
• Make the best home phone system ever
• Build an IVR, call queuing and live customer support app for a banking call center
• Power voicemail for Vonage
• Call a list of phone numbers with a recorded message, “This is congressman Baird, did you know you can add yourself to the National Do Not Call list? Press 1 to add yourself now…”
Asterisk: The Apache of Telephony
• Asterisk is an open source bundle of telephony protocols and VoIP magic, in a PBX form factor.
• Features: extensions, voicemail, conferencing, IVR, 3-way calling, caller ID, speech to text (Sphinx), text to speech (Festival)
• Protocols: SIP, IAX, H.323, ADSI, +
• Extensibility: the dialplan, AGI, manager API, C API
Interfacing with Asterisk• There are several ways to write applications for
Asterisk out of the box– The dialplan - extensions.conf
• Hard to write and debug complicated systems, but fast.
– AGI (Asterisk Gateway Interface)• Asterisk calls out to an application during dialplan.• Run telephony and app processes on separate servers
– AMI (Asterisk Manager Interface)• Monitor asterisk call channel state
– Modify the source code / loadable module • Hey it is open source.• Changes will be need to be migrated each version.• C code, will run fast, extensive access to asterisk internals via APIs
RAGI = Rails + Asterisk
What is RAGI?
• RAGI = Ruby Asterisk Gateway Interface
• Asterisk AGI implementation in Ruby + more
• Supports both incoming and outgoing calls
• Adds a “Handler” class type to Rails apps for doing phone apps
Architecture
Ruby on Rails Asterisk
RAGI Server
Call Handleranswer();i = 0;n.times doendhangUp()
VoIP Protocol HandlingApplication Logic
databaseAGI
Why RAGI?
• You are using Rails and/or Ruby for other development.
• Asterisk alone is not enough: Wrestling with “extension.conf” is fine for PBX but not well suited for data-driven apps with complex flows
• Deployment: separate your app server from your telephony server
• Development: Direct access to your object hierarchy within your phone app
What could you use RAGI to do?
• Add a mobile interface to your web app that has more reach than with WAP or SMS
• Add a phone interface to your blog (posting + listening)
• Mash up RSS and IVR for a better kind of 411
• Route the call center calls to the CRM app (finally)
• Broadcast live audio from the game to people listening to streaming audio off a website
• Alternate reality games… the pay phone rings, a clue is given, punch in your cell phone number, etc
• Rentable Phone Numbers - Temporary phone numbers for use in classified ads
• Click-n-talk with others who are viewing this web page
• Learn Chinese; fill out your profile and we’ll match you with a qualified instructor in China connecting you over voip and handling payment...
Nice reach
VoIP/PSTN Service
Asterisk + RAGI + RoR
Mobile phones
Fixed line phones
VoIP Clients
SIP, IAX
SIP
, Jabber
Web Browsers
HTTP
Who is using it?
• Released 2.5 months ago
• Nearly 500 downloads times
• Emails and generally– “Thanks, now I have an excuse to learn Rails”
and– “Thanks, now I have an excuse to learn Asterisk”
e.g. Mobile Payments in Ghana
Mobile Payments in Ghana (2)
RAGI saved me a lot of time, especially with the sample application using such an easy to use API. Ruby on Rails (ActiveRecord, really) helped a lot to bootstrap a working model and I was able to tie the model and RAGI in to my application logic easily. I was new to Ruby, though I know quite a few languages, and I had to absorb Ruby, Rails, RAGI and Asterisk.
Short story is after something like a week and a half, we had come up with the version that you can listen to in the audio demonstrations of the system.
“
”http://www.corenett.com/
mailto:[email protected]
Guido Sohne
Let’s Build an App Right Now
Note: See screencast on http://www.snapvine.com/code/ragi
“Dial-a-Package.com”
Track packages from web and phone
Web: Use the web to enter UPS tracking numbers
Phone: Dial in to hear delivery status
For visual people…
For visual people…
For visual people…
For visual people…
For visual people…
For visual people…
For visual people…
For visual people…
Components
• Ruby on Rails web & phone app UI
• MySQL data storage
• Asterisk telephony low level
• RAGI connect Rails and Asterisk
Outline of Steps
• 1) Create Rails Install
• 2) Create database instance
• 3) Build web app
• 4) Install RAGI
• 5) Build call handler
Write the call handler
1. User dials in
2. Authenticate by caller ID (or ask)
3. For each user delivery,• Fetch and parse info from the web• Use text to speech to read status
4. Hangup
Listen…
Summary
• Asterisk = open source PBX, VoIP and telephony protocols
• RAGI = build phone interactions in your Ruby and Rails apps
• More info:
http://www.snapvine.com/code/ragi
Today’s demo code and screencast will be posted online soon!
Learn More
Contact: [email protected]
RAGI, these slides, a new tutorial and other goodies:
http://www.snapvine.com/code/ragi
http://www.asterisk.org
http://www.voip-info.org/wiki/
Backup Slides
Example CallHandler API
• answer, hangup• play_sound, record_file• dial• play_tone (various)• send_DTMF• MeetMe conferencing• get_data – read user input• speak_text – text to speech• say_digits, say_number, say_time• get call status• execute arbitrary Asterisk server extension
Configure RAGI
Ruby on Rails Asterisk
RAGI Server
Call Handler
Phone call
answer();i = 0;n.times doendhangUp()
Configuration:
1 Tell Asterisk where your RAGI server is (agi)
2 Tell Rails how to boot up with RAGI
Your call handlers are found using URL, just like a web browser.
e.g. exten => 101,2,agi(agi://192.168.1.203/banking/check_balance)
Call Handler
Goes here when the call is answered
RAGI Classes
• CallHandler – subclass this for each IVR you create
• CallConnection – an API used by a call handler to operate on the call (e.g. play_sound, get_data, say_digits, etc)
• CallServer – a process in your Rails app that communicates with your Asterisk server
• CallInitiate – a class used to trigger outbound calls
VoIP Termination / Origination• Origination – creation of a call session from the PSTN to a VoIP network. Think of this as a
mobile device dialing a number, and that call session routing back through the mobile network, to that number (DID), which is routed over VoIP to your Asterisk server. Think of it as “dialing a phone number to reach your VoIP server”
• Termination – creating a call session from the VoIP network to the PSTN. This of this as your server calling out to a regular phone number (e.g. Skype Out). Traffic is routed to a termination service provider who then routes the VoIP to the closest point of presence, where they typically have a switch that hands of to a PSTN network (i.e. to make a local phone call)
• Providers – There are many providers of termination and origination. Many ISPs have entered the business, and there are plenty of resellers of Level3 and Global Crossing offering the service.
• Prices – termination is ~$0.01 per minute within the USA and origination (phone numbers) for a few dollars per month flat rate unlimited minutes. International rates vary.
• Setup – once you have found a provider, it is easy to configure asterisk to accept inbound phone calls (origination) and hand them to particular RAGI handlers by configuring asterisks’ basic config files. Outbound (termination) is similarly easy to configure so that you can use RAGI to place calls to phone numbers.
• More info - http://www.voip-info.org/wiki/view/VOIP+Service+Providers+B2B
RAGI – developer SDK
“RAGI alone is like the 5th thing that makes me think I should sincerely add Ruby to Python as my two default languages” – Kendall Clark, Managing Editor of XML.com
“Ruby Asterisk Gateway Interface serves as a bridge between Ruby on Rails and Asterisk - Pretty exciting stuff” - David Heinemeier Hansson, creator of Ruby on Rails
“This has the potential to be really interesting, because Asterisk+Rails should be about a decade more advanced then anything that the IVR people are used to seeing.” – scottstuff blog
• Over 500 downloads in the first 2.5 months!
Ruby Asterisk Gateway Interface http://www.snapvine.com/code/ragi