Oct 11 / sandeep

Developing Kookoo apps with codeigniter

Prerequisite : you should know the Codeigniter framework and Kookoo API.

KooKoo communicates with our application by posting GET requests to our web server and passing request parameters. but Codeigniter, by default crafts it’s URL in a search engine friendly format. It uses a segment-based approach.

So the first and foremost tweak that we need to do is configure Codeigniter to mix the segments and querystring. this howto article can help you in doing it Howto article.

This is what you can do if you need to have a mixed segment and querystring approach. You can enable this either globally for your whole application or just locally within a controller.

To enable this globally, go to your application/config.php file, and look for $config['uri_protocol'] variable and change it to:

$config['uri_protocol'] = “PATH_INFO”;
After that, find the $config['enable_query_strings'] variable and change it to:
$config['enable_query_strings'] = TRUE;

Now your application is ready to accept both segments and querystrings.
Next thing is you need to parse the url string so that you can get the Parameters available in your class.
so add this line in your Controller Constructor method.
parse_str($_SERVER['QUERY_STRING'],$_GET);

thats it now we are ready to work with Get style of URL’s in codeigniter.

Kookoo has provided a PHP library for working with Kookoo. this is the modified version for codeignitor
so please download this and you can add this file to your application/libraries. Kookoo codeignitor lib

once its done you can start writing your logic in your Class. here is one such example for my Book isbn APP.

class Kookooapp extends Controller {

	function Kookooapp()
	{
		parent::Controller();
                $this->load->library('Kookoo');
                $this->load->library('session');
                parse_str($_SERVER['QUERY_STRING'],$_REQUEST);
	}

        function index()
	{

                $calldata = array(
                   'callno'  => '',
                   'state'   => '',
                   'isbn'    => '',
                   'logged_in' => TRUE
               );

		//for email
		$to = "sandeep.eecs@gmail.com";
                $subject = "KooKooApp call";

                if($_REQUEST['event']=="NewCall") {
                    $this->kookoo->response("5692");
                     $this->kookoo->collectdtmf('15','#','5000');
                     $this->kookoo->addPlayText("Please Enter the Book. isebean number and terminate with hash");
                     $this->session->set_userdata('state', 'getISBN');
                    $this->session->set_userdata('callno', $_REQUEST['cid']);
                    $this->kookoo->getXML();

                } else if ($_REQUEST['event']=="GotDTMF" && $this->session->userdata('state') == "getISBN") {
                    $isbn = $_REQUEST['data'];
                    $this->kookoo->response("5692");
                    $bookdata = $this->flipkartdata($isbn);
                    $emailtext="We got a call from ". $this->session->userdata('callno')." for a book with ISBN ".$isbn ;
                    mail($to,$subject,$emailtext,$headers);
                    if($bookdata['msg']=="gotdata")
                    {
                        $this->session->set_userdata('title', $bookdata['title']);
                        $this->session->set_userdata('fkprice', $bookdata['price']);
                        $this->kookoo->collectdtmf('1','#','5000');
                        $this->kookoo->addPlayText("We found book with title .");
                        $this->kookoo->addPlayText($bookdata['title']);
                        $this->kookoo->addPlayText(" and FlipKart Price is ");
                        $this->kookoo->addPlayText($bookdata['price']);
                        $this->kookoo->addPlayText(" Please Press one to find Price of other Stores");
                        $this->kookoo->addPlayText(" and terminate with hash");
                        $smstext="www.mydiscountbay.com"."flipkart Price ".$this->session->userdata('price'). "booktitle :".$this->session->userdata('title') ;
                        $this->kookoo->sendsms($smstext,$this->session->userdata('callno'));

                        $calldetails = $this->session->userdata('callno'). " called for " .$isbn;
                        $this->kookoo->sendsms($calldetails,'9916810809');

                        $this->session->set_userdata('isbn', $isbn);
                        $this->session->set_userdata('state', 'other store price');
                        $this->kookoo->getXML();
                    }
                    else
                    {
                        $this->kookoo->collectdtmf('15','#','5000');
                        $this->kookoo->addPlayText("Invalid Isbn number Please try again");
                        $this->session->set_userdata('state', 'getISBN');
                        $this->kookoo->getXML();
                    }
                }

                 else if ($_REQUEST['event']=="GotDTMF" && $this->session->userdata('state') == "other store price") {
               	    $option = $_REQUEST['data'];                    

                    $isbn= $this->session->userdata('isbn');
                    $lmprice=$this->getprices->landmark($isbn);
                    $ibprice=$this->getprices->infibeam($isbn);
                    if($lmprice=="N/A")
                    $lmprice="Book not found";
                    else
                    $lmprice = "Rupees ".$lmprice;
                    if($ibprice=="N/A")
                    $ibprice="Book not found";
                    else
                    $ibprice = "Rupees ".$ibprice;
                    $this->kookoo->response("5692");
                    $this->kookoo->PlayText("The Book Price at Landmark is ");
                    $this->kookoo->PlayText($lmprice);
                    $this->kookoo->PlayText("The Book Price at infibeam is ");
                    $this->kookoo->PlayText($ibprice);
                    $this->kookoo->playtext("Thank you for calling Us.");
                    $smstext="www.mydiscountbay.com book with :".$isbn. "price at landmark is  ".$lmprice ."and Price at infibeam is ".$ibprice;
                    $this->kookoo->sendsms($smstext,$this->session->userdata('callno'));
                    $this->kookoo->hangup();
                    $this->kookoo->getXML();
                    //session_destroy();
                    //$this->session->unset_userdata($calldata);
                   $this->session->set_userdata('state', '');

                }

                       	}

        function flipkartdata($isbn)
        {

            if(!$this->isbn->check($isbn))
		{
			$data = array('msg' => "The ISBN number is not Valid"	);
		}
		else{
			$flipkart=$this->flipkart->getdata_html($isbn);
				$data = array(
                                              'title' => $flipkart['title'],
				'author' => $flipkart['author'],
                                'msg'=> "gotdata",
				'price'=> $flipkart['price']);
		}

                return $data ;
        }

}

Post your comments if you found this usefull.

Leave a Comment