| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

AdhearsionAndRails

Page history last edited by Banned User 14 years, 1 month ago

Sample Adhearsion and Rails Application

 

Quiz Show!

 

The following application example was created using Max OSX, Adhearsion 0.8, Ruby on Rails 1.2.6 and Asterisk 1.4.

 

To show the integration of Ruby on Rails and Adhearsion, we are going to create an automated quiz show application. The way the application will work is as follows:

 

The host of the quiz show will be able to go to a website and enter the text of questions they would like to ask the contestants. They will also enter the correct answer the contestant should enter using their keypad, along with a value of what each question is worth. A contestant will call the system and using their phone, be asked the series of questions (from the least to most point values). The system will then determine and inform the contestant if the correct answer was given or not. The system will let the contestant know how many questions they must answer, keep track of the number of points they have won and if they answer all of the questions correctly, tell them their final point total.

 

For the website we will be using the awesome Ruby on Rails framework to allow a host to enter their questions. In order to get the questions to be read you must use some type of a "text-to-speech" application in your dialplan to read the questions to the contestants. For this example we will be using Swift, but many others exist and should be somewhat portable in the future since Adhearsion has a "speak()" which should abstract the implementation.

 

Adhearsion

 

Create project

 

We'll start out by creating our Adhearsion application using the standard ahn command. Change to any directory you wish where you will create the application.

 

localhost$ mkdir ahnproject

localhost$ cd ahnproject

localhost$ ahn create quizshow

create  

create  components/simon_game/lib

create  components/simon_game/test

etc..

 

Next we'll create our Ruby on Rails application inside our Adhearsion project. I've found it best and easiest to create the Ruby on Rails application right inside our adhearsion application, however, we need to uncomment a link in the config/startup.rb.

 

edit the line and remove the # to enable Rails in your adhearsion project.

config.enable_rails :path => 'gui', :env => :development

 

Ruby on Rails

 

Create Rails Project

 

Let's jump right in and create the Ruby on Rails application ina "gui" directory.

 

localhost$ cd quizshow

localhost$ rails gui

create  

create  app/controllers

create  app/helpers

create  app/models

etc...

 

You now will have a gui directory under your current directory. Change into the gui directory.

 

localhost$ cd gui

 

Generate Model

 

We know that we'll be dealing with questions in the application, so let's create a model for our questions. Our questions, will have 3 properties: ask, correct_answer and value. Using the scaffold_resource in Rails we can easily create an interface into our questions.  

 

localhost$ ruby script/generate scaffold_resource question ask:string correct_answer:string value:int

 

This will create all of the artifacts that are needed to model the questions that we will be asking. In fact, using the scaffolding in rails, you should be able to fire up your application and start entering questions right now. But first, you'll need to create the database. Execute this command to create your database in MySQL.

 

localhost$ mysqladmin create quizshow_development -u root -p

 

and enter in your root password for your database.

 

Run Migration

 

Now that your database is created you just need to run the Rails migrate task to create the questions table. But first you'll need to look in the config/database.yml file and verify that the username and password to access your database is correct. Open up the database.yml file and verify the properties are correct. For the mySQL database, you would have a cofiguration similar to:

 

adapter: mysql

database: quizshow_development

username: joe

password: secret

socket: /tmp/mysql.sock

 

Then you will issue the command:

 

localhost$ rake db:migrate

 

If your database can be accessed and the migrations run successfully, you will see output similar to the following.

 

== CreateQuestions: migrating =====================================

-- create_table(:questions)

   -> 0.6679s

== CreateQuestions: migrated (0.6681s) ===========================

 

Now it's time to have some fun. Let's fire up the rails environment and start putting some questions into the database.

 

localhost$ ruby script/server

 

and point your browser to: http://localhost:3000/questions

 

Add some questions with answers and values to the database. For example: Ask = "What is 2 plus 2?", Correct answer = "4", Value = 100 and Ask = "What year did the United State declare independence from Britian?", Answer = "1776", Value = "200".

 

Back to Adhearsion

 

Dialplan.rb 

 

Now that we have our database populated, let's turn back to our Adhearsion application to allow us to ask questions to contestants that call out application. Stop the Rails application ( Control-C ) and change out of the directory you currently are in.

 

localhost$: cd ..

 

We'll next create the code in the dailplan.rb for the logic for the calls. Add the following block of code to your dialplan.rb. You'll want to make sure you have the swift applciation installed on your system to allow for the text to be interpreted and spoken.

 

quizshow {

  questions = Question.find(:all, :order => "value")

  execute "swift","\"Welcome to the Quiz Show! There are #{questions.size} questions you must answer to win. Here is a your first question.\""

  execute("Wait", "1")

  amount_won = 0

  questions.each { |question|

    execute "swift","\"The value of this question is #{question.value} points. You currently have #{amount_won} points.\""

    execute("Wait", "1")

    execute "swift","\"#{question.ask}\""

    user_input = input

    if (user_input == question.correct_answer)

      execute "swift","\"Correct answer.\""

      amount_won = amount_won + question.value

    else

      execute "swift","\"Incorrect answer. Please try again.\""

      redo

    end

  }

  execute "swift","\"Congratulations you answered all of the questions correctly for the Quiz Show. Your final score is #{amount_won} points.\""

  hangup()

 

The block above is where the application logic occurs. We'll step through the Ruby code if you are not familiar with the language. The first line gets all of the questions from the database.

 

questions = Question.find(:all, :order => "value")

 

This creates a variable called "questions" that holds all of the our questions models. 

 

The "execute" lines should be somewhat straightforward as we are just calling out to swift to welcome the user and tell than that their first question is coming.

 

We set the initial amount that user has won to 0 and then iterate over the questions and calling swift to read the text for each one. Before we read the question however, we let the user know the value of the question and how many points they currently have with the line.

 

execute "swift","\"The value of this question is #{question.value} points. You currently have #{amount_won} points.\"" 

 

Pretty cool how we can insert variables right into the spoken sentences!

 

Now, asking the question to the contestant is simply:

 

execute "swift","\"#{question.ask}\""

 

this integration with ActiveRecord and Ruby on Rails is pretty neat stuff! We then wait for the user's input with the line:

 

user_input = input

 

Once our PBX receives the response, Adhearsion moves on and checks if the value entered is the correct answer. If it's correct, we tell the contestant they were correct, increment their point total and continue with the iteration. Once all of the questions are answered, we congratulate the contestant and let them know that they have won the quiz show.

 

We're pretty much ready to go. Because we enabled rails in our startup.rb, Adhearsion is able to connect to our database and also let it know about our "Question" model object and since our Ruby on Rails application is under the 'gui' directory, Adhearsion is able to find all of the models and database configuration it needs.

 

Bring it all together

 

OK, we should be all ready to go on the Ruby side. You should now have your Ruby on Rails and Adhearsion applications created and ready to be started. So, what are we waiting for, let's fire them up!

 

If you haven't started it yet, open a new terminal and go to your quizshow directory and issue the command:

 

localhost$ ahn start .

 

then open up another terminal, change to your 'gui' directory and issue the command:

 

localhost$ cd gui

localhost$ ruby script/server

 

The final piece is just configuring your PBX's dialplan to call Adhearsion. This has been covered in many other sections, so you should be somewhat familiar with it, but just in case you forgot, here is what I have put in my extensions.conf

 

[quizshow]

exten => _X.,1,AGI(agi://192.168.50.52) ; change this to the IP of the server running adhearsion

 

Summary

 

Voila! When you dial into your PBX you will be sent to our quiz show application. Have fun creating new questions and amaze your friends and family showing them how easy it is to change questions and point totals.

 

As a future exercise you might consider including the ability for a person to pause the game and have their score stored in the database for them to return at a later point. We'll leave this and countless other features that could be added to this application as exercises for the reader.

 

I hope you have enjoyed working through this sample application. This sample just scratches the surface of how one can use Adhearsion to create an application that bridges a web interface with database backed information and interacting with that information through a phone. Have fun letting your mind roam and think of future possibilities!

Comments (0)

You don't have permission to comment on this page.