Managing Conference Calls With twilio API in CakePHP

Recenetly I worked on a project where I have make Conference call with multiple user. Add users or remove users to a conference on the fly. After certain time all calls in a conference had be dropped. At first I was bit lost how to do this. Then I found out about twilio. They have all the APIs that I needed. Here I will show how to make conference calls and managing them. I expect that you know how to create twillo app and have twillo phone number. Also For beginner you can check out their tutorials http://www.twilio.com/docs/quickstart

Before we start, let’s get 2 thing clear

  • Every call has his SID. SID is like unique identification for a call. We will use it to drop calls.
  • An conference does have SID. It also have parameter FriendlyName, which is entered by twillo app ie we. We will use this for identifying running conferences.

Step 1
Download twillo php client library https://github.com/twilio/twilio-php. Add the twilio folder to vendors folder. Add the account_sid and auth_token in bootstrap.php

Configure::write("twilio_details", array('account_sid'=>"ACd8243f5e652f4b748f953026e88d7956", 'auth_token'=>"4e3b54b7a3e2819edb4309b14faacab8"));

Step 2
Create our primary table ‘conferences’ where will save our conference instances. The name field will save the value of FriendlyName.

CREATE TABLE  `conferences` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 550 ) NOT NULL ,
`created` DATETIME NOT NULL ,
`modified` DATETIME NOT NULL
) ENGINE = INNODB;

We will use ‘conference_calls’ table to save all calls mad in a conference.

CREATE TABLE `conference_calls` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `conference_id` int(11) NOT NULL,
  `call_sid` int(11) NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Using “cake bake” create model controller. I added the files and tables in zip file end of this post.

Step 3
Let’s start making conferences. So create a function in conferences_controller.php conference_call(). Where we will call a bunch of numbers.

function conference_call() {
	App::import('Vendor', 'Twilio', array('file' => 'twilio'.DS.'Services'.DS.'Twilio.php'));
	$account_sid = Configure::read("twilio_details.account_sid");
	$auth_token = Configure::read("twilio_details.auth_token");
	$client = new Services_Twilio($account_sid, $auth_token);
	$room_id = md5('jambura.blog').time();
	$participants = array('+11111111110', '+11111111111', '+11111111112');	
	$chk=0;
	foreach($participants as $participant):
		pr($participant);
		try{
			$call = $client->account->calls->create(
			  '+11111111119', // From this number
			  $participant, // Call this number
			  'url_toyour_server/conference_calls/conf_call_init?confname='.$room_id
			);
		}catch (Exception $ex){
			$chk=1;
			echo $ex->getMessage();
		}
	endforeach;
	if($chk==0)
	{
		$this->data['Conference']['name']=$room_id;
		$this->Conference->create();
		$this->Conference->save($this->data);
	}
	exit;
}

Let’s talk abou the code – As you see I initiate the Services_Twilio() with account_sid and auth_token. then create a unique id for $room_id. The $room_id must be unique. Then I create some demo phone number in array. Then i call each number. The main focus her should be this line ‘url_toyour_server/conference_calls/conf_call_init?confname=’.$room_id. When twillo callls to the number it also send an request to this url url_toyour_server/conference_calls/conf_call_init?confname=’.$room_id with details about the call. Suppose when twillo calls ‘+11111111110’, it also send a post data with call SID and other information to the url and expect and return xml from that url. Let’s build our xml then.

Step 4
Create conf_call_init() function conference_calls_controller.php

function conf_call_init() {
	$conference_call= $this->ConferenceCall->Conference->findByName($this->params['url']['confname']);
	if(!$conference_call)
	{
		exit;
	}
	else
	{			
		$this->data['ConferenceCall']['conference_id']=$conference_call['Conference	']['id'];
		$this->data['ConferenceCall']['call_sid']=$this->params['form']['CallSid'];
		
		$this->ConferenceCall->create();
		$this->ConferenceCall->save($this->data);
		
		header('Content-type: text/xml');
		echo '<?xml version="1.0" encoding="UTF-8" ?>
				<Response>
					<Dial>
						<Conference>'.$this->params['url']['confname'].'</Conference>
					</Dial>
				</Response>';
		exit;
	}
}

So here we first check that the name is exits in our sysmtem or not. Then we save the call SID to ourdatabase. And retns the conference name to twillo. twillo connect call using this tag.

So our conference is create done. Super easy.

Step 4
Now we want to drop all the calls from a conference.

function conference_call() {
	App::import('Vendor', 'Twilio', array('file' => 'twilio'.DS.'Services'.DS.'Twilio.php'));
	$account_sid = Configure::read("twilio_details.account_sid");
	$auth_token = Configure::read("twilio_details.auth_token");
	$client = new Services_Twilio($account_sid, $auth_token);
	
	$this->Conference->recursive = 2;
	$conferences =  $this->Conference->find('all');
	foreach($conferences as $conference):
			foreach ($client->account->conferences->getIterator(0, 1, array('FriendlyName' => $conference['Conference']['name'])) as $conf) 
			{					
				if($conf->status != 'completed')
				{
					foreach($conference['ConferenceCall'] as $runing_conference_per_call):
							$call = $client->account->calls->get($runing_conference_per_call['call_sid']);	
							$call->hangup();					
					endforeach;
				}
			}
	endforeach;
	exit;
}	

Here we loop through all the conferences in our database. Then we check the status of each conferences. If the status is not complete then we drop all the calls.

In this tutorial, I showed you the basic functionality like creating conference and dropping calls. I hope this post helps you build awesome twillo app. You can download all codes and table from HERE.

About Zakir Hyder

This entry was written by .

20. June 2012 by Zakir Hyder
Categories: Jambura | Tags: , , , | Comments

Comments

  1. […] Managing Conference Calls With twilio API in Cakephp Recenetly I worked on a project where I have make Conference call with multiple user. Add users or remove users to a conference on the fly. After certain time all calls in a conference had be dropped. At first I was bit lost how to do this. Then I found out about twilio. They have […] […]