Jul 18 2008

Asterisk Applications with Ruby On Rails

Category: AsteriskBipin Balakrishnan @ 12:15 pm

rails.jpg

Recently while surfing in google i found that we can develop nice web+telephony applications by the combination of Asterisk and Ruby on Rails.All we required is knowledge in Asterisk and any Framework architecture.I am not much familiar in ruby still managed to develop my first app with it.

The system is built using the following components:
Asterisk – the open source PBX which is used to interface to VOIP/PSTN provider over SIP to place real telephone calls
Ruby on Rails – the super-productive web application framework built in the Ruby programming language
MySQL – database is used to store the records associated with the application data model. All database access is handles by Ruby on Rails.
RAGI – Ruby Asterisk Gateway Interface, an API for building Asterisk telephony applications in Ruby (and Ruby on Rails).

It is assumed that the you are already familiar with:

Asterisk
How to install
How to setup
Basics of what AGI is
Ruby on Rails
How to install
Rails app structure (controllers, views, models, etc)
MySQL
How to install
Basic SQL operations and queries

First Create a Web App with Ruby On Rails

mkdir    ragi_testapp
cd  ragi_testapp
rails tutorial

Create a database called ‘ragidb’

CREATE TABLE `deliveries` (
 `id` int(11) NOT NULL auto_increment,
 `user_id` int(11) NOT NULL default '0',
 `tracking_number` varchar(64) NOT NULL default '',
 `delivery_status` varchar(255) default NULL,
 `updated_on` datetime default NULL,
 `created_on` datetime default NULL,
 PRIMARY KEY (`id`)
 );
CREATE TABLE `users` (
 `id` int(11) NOT NULL auto_increment,
 `phone_number` varchar(10) NOT NULL default '',
 `updated_on` datetime default NULL,
 `created_on` datetime default NULL,
 PRIMARY KEY (`id`) );

Insert some values to the user table.

Database setup for rails

vim ragi_testapp/tutorial/config/database.yml
development:
 adapter: mysql
 database: ragidb
 socket: /var/run/mysqld/mysqld.sock
 timeout: 5000
 username: root
 password:

Enter into the tutorial directory

cd ragi_testapp/tutorial/
 ruby script/generate model User
 ruby script/generate model Delivery
 ruby script/generate controller User

Start Webbrick Server

ruby script/server

Enter into ‘controller’ directory of project

Edit user_controller.rb

class UserController < ApplicationController
 #scaffold :user
 def index
 @users = User.find(:all)
 respond_to do |format|
 format.html # index.html.erb
    end
 end
 end

Create file “index.html.erb” in veiw/user folder of the project

<h1>Listing Users</h1>
<table>
 <tr>
 <th>Phone Number</th>
 <th>Updated date</th>
 <% for user in @users %>
 <tr>
 <td><%=h user.phone_number %></td>
 <td><%=h user.updated_on %></td>
 </tr>
 <% end %>
 </tr>
 </table>

Run http://localhost:3000/user/ in the browser we can see the rails app running fetching the values in the user table.

Now the Asterisk part.

Install RAGI. This is easy, because RAGI is packaged as a “gem”. Just type the following from our command prompt:

gem install ragi

Include the configuration below in the “environment.rb” in the config folder in the project.

#The following code tells Rails to start a Ragi server as a separate thread.
Dependencies.mechanism = :require
# Simple server that spawns a new thread for the server
class SimpleThreadServer < WEBrick::SimpleServer
     def SimpleThreadServer.start(&block)
     Thread.new do block.call
    end
  end
end
require 'ragi/call_server'
RAGI::CallServer.new(:ServerType => SimpleThreadServer)

In this step, we’ll tell Asterisk where are RAGI server is so that the appropriate calls can be routed into our new application.

Now edit extension.conf of asterisk add

[ragi]
 exten => 9999,1,Set(RAGI_SERVER="localhost:4573")
 exten => 9999,2,NoOp(${RAGI_SERVER})
 exten => 9999,3,Answer()
 exten => 9999,4,AGI(agi://${RAGI_SERVER}/tutorial/dialup)
 exten => 9999,5,Hangup

When we call 9999 the call will be routed to RAGI server which can be a local system or a remote system.

Create a directory called “handlers” in your Rails app directory at the same level in your directory structure as controllers, models and views. This is where you will put the class that implements.

Create a new file called “ tutorial_handler.rb” and open that file to edit.

require 'ragi/call_handler'
class TutorialHandler < RAGI::CallHandler
 def dialup
 say_digits('12345')
 say_digits(User.count.to_s)
  end
 end

This is basically your “hello world” call handler. The class is “TutorialHandler” and is an implementation of the RAGI “CallHandler” class. The method “dialup” is automatically called when a phone call is routed by Asterisk through RAGI to this handler.Configure a sip phone and make call to 99999 and can see Ruby application answering the call.

And lots we can play :)

Tags: , , ,

3 Responses to “Asterisk Applications with Ruby On Rails”

  1. Chris says:

    If you tell me how to “Configure a sip phone and make call to 99999″ then I’d love you forever!

  2. admin says:

    Edit sip.conf file and paste this
    [200]
    port = 5060
    type=friend
    context=ragi
    username=200
    secret=200
    callerid=200
    host=dynamic
    canreinvite=yes

    Install a softphone and give username,password and the domain name as in the in the sip.conf file and make call to 9999….:)

  3. jsgoecke says:

    Give Adhearsion (http://adhearsion.com) a try too as it works nice with Rails and Asterisk…

Leave a Reply