Thursday, November 18, 2010

Build an online and telephony poll in 10 minutes using KooKoo and Wufoo

The development paradigm has changed so much in the recent years that all you need to build applications now is an idea. Almost all services now provide an API and this has led to a lot of Mashups appearing.

Mashups allow you to build your idea very fast.
Earlier, the flow for developing an application involved the following:
  1. Get an idea (The most important step :) )
  2. Identify functionality needed to develop the idea (functionality 1, functionality 2 ......)
  3. Develop functionality n
  4. Test functionality n
  5. Repeat 3 and 4 for all functionalities
  6. Integrate functionality
  7. Deploy and announce to the world.
Now, its much easier:
  1. Get an idea (This step cannot be replaced)
  2. Identify functionality needed to develop the idea (functionality 1, functionality 2 ......)
  3. Identify APIs which provide the functionalities
  4. Integrate APIs
  5. Deploy and announce to the world.
Though the number of steps in both cases are almost the same, in the second approach most of your headaches are taken care of as you don't need to develop and test each individual functionality. You just need to concentrate on your core idea and integration.

In this post I will show you how easy it is to build a Kaun Banega Crorepati (Who wants to be a millionaire) style of telephone voting application using this approach. For this application we will use the KooKoo and Wufoo APIs.


Step 1: Get an idea

We want to build a polling application. The poll should be available both on the web as well as on the phone. Our poll will have 4 options. People can visit our web site and poll their choice by selecting an option or call a number and poll their choice. For each option, people should call a different phone number. That is, for selecting option 1, users call 91-40-XXXXXX.For option 2, they call 91-40-YYYYY etc. This is something similar to the Kaun Banega Crorepati (Who wants to be a millionaire) type of polling.

Step 2: Identify functionality needed to develop the idea


The functionalities identified for this application are:

  1. Web based form to display the poll on the web
  2. Database to store the results
  3. Telephony interface
  4. Reports of the poll
Step 3: Identify API's which provide the functionalities

For functionalities 1,2 and 4, Wufoo provides a drag and drop approach to creating forms and creating reports.Wufoo also has a free account for trial so we will use Wufoo to create our polling form. Just sign up for a free account and create a form as shown below.


You can catch a better quality video at http://www.youtube.com/watch?v=sCx-lD7MqDg
As you can see, we can create a form with a back end database to store all our submissions in under a minute.


Additionally, Wufoo provides an API using which we can access the form programmatically. You can access the API information in the code page of the form as shown.
The main items to remember in the API page are the API Key, the form name (kbc-poll) and the field id (1). We will use these details to connect to the form through a program.






For functionality 3 we are going to go with KooKoo (because I work on KooKoo :) ). KooKoo provides you an API using which you can perform telephony functions. KooKoo also has a free account. But for this demo I will use my account where I have provisioned 4 numbers to my account (040-30512831,30512832,30512833 and 30512834) Since the numbers are provisioned to my account, whenever a call lands on these numbers KooKoo informs me by calling my application URL. So I need to write code to handle the calls and that is exactly what we will do in the next step.

Step 4: Integrate APIs


Let us create a php page called kbcpoll.php to handle the calls. We will add this page as our application URL in KooKoo settings.After adding the URL you can see the phone numbers assigned to your account as well as your application URL in your dashboard as shown below.




Both KooKoo and Wufoo provide php libraries to access their API. We will use those to make our development easier. So to start coding our kbcpoll.php page let us first include those libraries.



require_once('response.php'); //KooKoo Library
require_once('WufooApiExamples.php');//Wufoo Library
Next, let us create a data structure to store our choices

$ans['914030512831']='PHP'; //This corresponds to the PHP choice in the web form
$ans['914030512832']='Java'; //Corresponds to java choice
$ans['914030512833']='C'; //corresponds to C choice
$ans['914030512834']='Ruby';//corresponds to ruby choice

Create a KooKoo response object: When KooKoo informs us of the new call we need to create a response and send the response to KooKoo.
$r=new Response();


Handle the new call event: When we are informed of the new call, we create a wufoo response and post the wufoo response using the wufoo library as shown below. After sending the wufoo response, we inform KooKoo to play some text to the caller about his choice.

if($_REQUEST['event']=="NewCall") //KooKoo informs of the new call through the event parameter
{
$caller_id=$_REQUEST['cid']; //The caller id is sent in the cid parameter
$called_number=$_REQUEST['called_number']; //The called number is sent in the called_number parameter

$resp['Field1']=$ans[$called_number]; //We assign the user choice based on the called number
//So if called_number is 914030512831
, then choice is PHP
$w=new Wufoo(); //Create a new Wufoo response
$w->postWufoo($resp,'{your wufoo username}','{form name}','{your api key}'); //post the selection to our form

$r->addPlayText('Thanks for calling. You selected '.$ans[$called_number]); //Inform KooKoo to play some text
$r->addHangup(); //Tell KooKoo to hangup the call
$r->send();
}
Thats it. With just 10 lines of code we were able to add a telephony channel for our poll. The only thing left is

Step 5:
Deploy and announce to the world
Just put this code on a web server and test it making HTTP requests and finally test it by making calls to the defined numbers. Once you start making calls you will see the data coming into your wufoo form entries table. You can then create reports using those entries just as easily. So as you can see, we were able to move from an idea to implementation in just 10 minutes, thanks to the power of APIs and mashups. You can pick and choose your APIs based on your preference. The same app could have been built by replacing Wufoo with Google spreadsheets(they also have an API) and KooKoo with Twilio/Tropo etc. So go ahead, create your mashups. Do let me know if you have any mashups in mind and maybe I can help you out.


The complete project source can be downloaded here: KooKoo Poll

The online poll created is available at http://kookoo.wufoo.com/forms/kbc-poll/
Use it to create your own polls.