Friday, September 2, 2011

Koo Koo PHP

PHP

 
 

Building KooKoo applications in php is as easy as it gets.

response.php is a KooKoo php library that has been designed to help you create KooKoo markup faster. It is not mandatory for you to use the KooKoo PHP library to create the markup. Plain XML is sufficient.

In order to use the KooKoo php library, just download KooKoo PHP library and include it in your source code.
KooKoo php library has two main classes:
  1. Response: This is the root tag

  2. collectdtmf: Collect user input from your callers
Response is the main class. To talk to KooKoo you have to first create a response object.
1 $r = new Response();
If you want to maintain session through the response object you instantiate it with a state variable
1 $r = new Response("start");

Once a response object is created, you can inform KooKoo to do several tasks on your behalf.For each task there is corresponding method in the Response object. The supported tasks and their corresponding methods are given below:

  1. playtext: Play some text to your callers
  2. playaudio: Play some audio to your callers
  3. Send Sms: Tells KooKoo to send an SMS to a specified number.(Available only to paid users)
  4. record: Record caller conversation
  5. Hangup: Tells KooKoo to hangup the call.
  6. dial: Make an outbound call (Available only to paid users)

 

Play text

 
Description:Tells KooKoo to play some text using the default text to speech engine.The characters usage of playtext tag is limited to 100 characters.
1 Method:addPlayText($text)
2 Arguments:The text to play
3 Example:$r->addPlayText("I love KooKoo");
 

Play audio

 
Description: Tells KooKoo to play an audio file.
1 Method:addPlayAudio($url)
2 Arguments:The URL of the audio to play
3 Example:$r->addPlayAudio("http://www.example.com/test.wav");
 

Send sms

 
Description:Tells KooKoo to send an SMS to a specified number.
1 Method: sendSms($text,$number)
2 Arguments:The text of the sms message (should be less that 160 characters)
3 and the mobile number to send the sms to.
4 Example:$r->sendSms("This is an sms message",919000455345);
 

Record

 
Description: Tells KooKoo to play a beep to the caller and record his message.
01 The URL of the recorded message will be sent back to your application.
02 Method: addRecord($filename,$format,$silence,$maxduration,$option)
03 Arguments:
04 filename:The name to be given to the recorded audio.
05 format:The format of the recorded audio. Right now only wav is supported.
06 silence:Number of seconds of silence before we assume the caller has finished recording.
07 Default value is 2 secs.
08 maxduration:Maximum number of seconds to allow recording. Default value is 30 secs.
09 option:Any additonal options to be provided to the record function.
10 Right now this parameter is not used.So you can leave it.
11 Example:$r->addRecord("test.wav");
 

Hangup

 
Description: Tells KooKoo to hangup the call.
1 Method:addHangup()
2 Example:$r->addHangup();
 

Collecting user input

 
Description: In KooKoo collecting user input is handled by the collectdtmf tag. Since the collectdtmf tag is a complex tag with other tags nested inside it, we have a CollectDtmf object to handle the collectdtmf tag. If your program needs to collect user input, first instantiate the CollectDtmf object
1 $cd=new CollectDtmf();
You can set the following attributes on the CollectDtmf object
1.max digits: The maximum number of digits to collect from the user before stopping
1 $cd->setMaxDigits("4");//This will tell KooKoo to collect 4 digits and return
2.terminating character: Specify the terminating character which when pressed will tell KooKoo to stop accepting user input and return.
1 $cd->setTermChar("#");//This means that when the user presses #,
2 kookoo stops accpeting user input and return the digits pressed so far.
3.time out: The number of milliseconds to wait for an user to input a digit.If the user does not enter any digit for the specified amount of time, KooKoo assumes that the user has finished entering the digits and returns the digits entered so far to your application.
1 $cd->setTimeOut("2000");//Time out is now set to 2 seconds.
You can also add playtext and playaudio tags to the collectdtmf. These will be played as prompts to the user prompting him to enter input. The format of the addPlayText and addPlayAudio methods are same as that of the Response object.
1 $cd->addPlayText("Please enter your pin number");
Once all the attributes are set to the CollectDtmf object we add the object to the Response.
1 $r->addCollectDtmf($cd);
So once the response object has been prepared with all the necessary actions, we send the response object to KooKoo
1 $r->send();
KooKoo will receive the Response object, perform the actions specified and return results if there are any. Your application receives the results, processes them and creates another Response object if necessary. This flow continues until you are finished with your application. To put this all together, an example application, Adder, is explained below.
 

adder.php

 
1 <? php
2 session_start();
3 require_once("response.php");
4 //you can download response.php from kookoophp.zip file located below
5 $r = new Response();//create a new response object for your application
Now you are ready to receive requests from the IVR. A general user scenario is given below.
  • Step 1: A customer calls your phone number.
  • Step 2: KooKoo receives the call and informs us of the new call at the URL we have specified in our KooKoo dashboard.Let us say our URL is http://www.yoursite.com/adder.php
  • Step 3: The PHP at the URL receives the request. We need to handle the message from KooKoo about the new call.
  • KooKoo communicates with our application by posting GET requests to our web server and passing request parameters. Add the following to adder.php to handle the KooKoo new call message.We recieve the call and ask KooKoo to interact with the caller to get the first number to calculate.
    01 if($_REQUEST['event']=="NewCall")
    02 {
    03     $cd = new CollectDtmf();//Create Collect DTMF object
    04     //which accepts dtmf for your application
    05     $cd->setTermChar("#");//(The terminating chracter. KooKoo will stop accepting input
    06     //after receiving this character.Default value is '#')
    07     $cd->setTimeOut("4000");( KooKoo will wait for '4000' milliseconds for the
    08     //user to enter some input.Default value is 4000 (4 seconds)).
    09     $cd->addPlayText("welcome to adder.Please enter your first number.Terminate with hash");
    10     //addPlayText function used to play the prompts in your application
    11     $r->addCollectDtmf($cd);
    12     //import the collect dtmf to response object
    13     //now add the Collect DTMF object to response object
    14     //(Basically Tracking the DTMF input on single response object)
    15     $_SESSION['state'] = "firstNumber";
    16     //Setting our current state to indicate that we are collecting first number.
    17     //We will use the session variables to track our flow.
    18     $r->send();
    19     //send the XML back to KooKoo
    20 }
    Step 4: KooKoo will play the specified text, collect the user input and inform us again at our URL. To handle that event add the following code sequentially
    01 else if ($_REQUEST['event']=="GotDTMF" && $_SESSION['state'] == "firstNumber")
    02 {//here the event is GotDTMF(This Events Are Requested from KooKoo)
    03     $first = $_REQUEST['data'];//store the first number
    04     $cd = new CollectDtmf();//Create Collect DTMF object
    05     //which accepts dtmf for your application
    06     $cd->setTermChar("#");//(The terminating chracter. KooKoo will stop accepting
    07     //input after receiving this character.Default value is '#')
    08     $cd->setTimeOut("4000");( KooKoo will wait for '4000' milliseconds for the user to
    09     //enter some input.Default value is 4000 (4 seconds)).
    10     $cd->addPlayText("Please enter the Second number.Terminate with hash");
    11     //addPlayText function used to play the prompts in your application
    12     $_SESSION['firstNumber']=$first;//store first number in session variable
    13     $_SESSION['state'] = "secondNumber";
    14     //Setting our current state to indicate that we are collecting second number.
    15     //We will use the session variables to track our flow.
    16     $r->addCollectDtmf($cd);
    17     $r->send();//send the XML back to KooKoo
    18 }
    Step 5: KooKoo will collect the second number and inform us. To handle this event add the following code sequentially.
    01 else if ($_REQUEST['event']=="GotDTMF" && $_SESSION['state'] == "secondNumber")
    02 {   //here the event is GotDTMF(This Events Are Requested from KooKoo)
    03     $second = $_REQUEST['data'];//store the second number
    04     $total = $_SESSION['firstNumber']+$second;
    05     $r->addPlayText("your total is $total");
    06     //addPlayText function used to play the prompts in your application
    07     $r->addHangup();//stops the call
    08     $r->send();//send the XML back to KooKoo
    09 }
    10 ?>
    Click to download

    kookoophp

    .

    The KooKoo PHP Library makes it easy to write KooKoo songs without having to touch XML.

    USAGE:

    There are two main classes. Response and CollectDtmf. First create a response object and append any tags to it.Since CollectDtmf is a complex tag we have a seperate Class for it. To append a collectdtmf tag, first create a collectdtmf object and then append that object to the response tag.