Easy Conference Calling with Twilio

July 14, 2011
Written by
Rahim Sonawalla
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

We’re running a developer contest this week centered around Twilio’s conference calling features, so I thought it would be helpful if I did a quick run through of the verb.

Have Your People Call My People

The most straight-forward way of setting up conference calling is to drop all incoming calls into a conference using the verb:

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
	<Dial>
		<Conference>MyConference</Conference>
	</Dial>
</Response>

The name of the conference is namespaced to your account, so you can name it anything you like. Anyone dialing into “MyConference” for your account will be put in a conference room together.

We can spice up our conference room a bit using the waitUrl attribute. Set waitUrl to an audio file (like an MP3), and you can give your conference participants some music to listen to while they wait for the conference to start.

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
	<Dial>
		<Conference waitUrl="https://api.twilio.com/Cowbwell.mp3">MyConference</Conference>
	</Dial>
</Response>

If you wanted to route incoming phone calls to different conference rooms based on something like their phone number, you would do:

<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<Response>
    <Dial>
    <?php
    if ($_REQUEST['From'] == '+15551234567')
        {
            echo '<Conference>AllByMyself</Conference>';
        } else {
            echo '<Conference>ThePartyRoom</Conference>';
        }
    ?>
    <Dial>
</Response>

If you wanted to mimic the popular conference calling solutions, you might want to ask that your participants enter a PIN before they’re allowed to join.

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
	<Gather action="check-pin.php" numDigits="4">
		<Say>Please enter your four digit PIN.</Say>
	</Gather>
	<!-- If caller doesn't input anything, prompt and try again. -->
	<Redirect />
</Response>

 

<?php
	header('Content-type: text/xml');
	echo '<?xml version="1.0" encoding="UTF-8" ?>';

	$pin = $_REQUEST['Digits'];
?>
<Response>
	<?php
		if ($pin == '1234')
		{
			echo '<Dial>';
			echo '<Conference>PrivateConference</Conference>';
			echo '</Dial>';
		}
		else {
			// Invalid PIN entered. Let the caller know and take them
			// back to the main menu.
			echo '<Say>Sorry, that PIN is not valid.</Say>';
			echo '<Redirect>handle-incoming-call.xml</Redirect>';
		}
	?>
</Response>

Reach Out and Call Somebody

Up until now, we’ve been handling incoming phone calls only. That is, people are only in a conference room if they dial into your Twilio phone number. Most conference systems you’ve worked with have been that model. Using Twilio’s REST API, having the conference call you is simple.

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
	<Dial>
		<Conference>MyConference</Conference>
	</Dial>
</Response>

<?php
	// The PHP Twilio helper library. Get it here http://www.twilio.com/docs/libraries/
	require_once('twilio.php');

	$API_VERSION = '2010-04-01';
	$ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
	$AUTH_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

	$client = new TwilioRestClient($ACCOUNT_SID, $AUTH_TOKEN);

	// The phone numbers of the people to be called
	$participants = array('+15557654321', '+15554446666', '+15552229999');

	// Go through the participants array and call each person.
	foreach ($participants as $particpant)
	{
		$vars = array(
			'From' => '+15551234567',
			'To' => $participant,
			'Url' => 'http://yourserver/conference.xml');

		$response = $client->request("/$API_VERSION/Accounts/$ACCOUNT_SID/Calls", "POST", $vars);
	}
?>

If you’ve got multiple conference calls going on, you can get a list of all the active ones:

<?php
	require_once('twilio.php');

	$API_VERSION = '2010-04-01';
	$ACCOUNT_SID = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
	$AUTH_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

	$client = new TwilioRestClient($ACCOUNT_SID, $AUTH_TOKEN);

	$vars = array('Status' => 'in-progress');

	$response = $client->request("/$API_VERSION/Accounts/$ACCOUNT_SID/Conferences", "GET", $vars);

	if ($response->isError)
	{
		echo "Something went terribly wrong. {$response->ErrorMessage}";
	}
	else {
		if ($response->ResponseXml->Conferences['total'] == 0)
		{
			echo 'There are no active conferences.';
		}
		else {
			echo '<ul>';

			foreach ($response->ResponseXml->Conferences->Conference as $conference)
			{
				echo '<li>'.$conference->FriendlyName.'</li>';
			}

			echo '</ul>';
		}
	}
?>

If you wanted to get the call SIDs of the pariticpants you would add:

else {
	echo '<ul>';

	foreach ($response->ResponseXml->Conferences->Conference as $conference)
	{
		echo '<li>'.$conference->FriendlyName.'</li>';

		$response2 = $client->request("/$API_VERSION/Accounts/$ACCOUNT_SID/Conferences/{$conference->Sid}/Participants", "GET");

		echo '<ul>';

		foreach ($response2->ResponseXml->Participants->Participant as $participant)
		{
			echo '<li>'.$participant->CallSid.'</li>';
		}

		echo '</ul>';
	}

	echo '</ul>';
}

With the call SID of a participant, you can take fine-grained control and mute specific participants.

$client = new TwilioRestClient($ACCOUNT_SID, $AUTH_TOKEN);

$vars = array('Muted' => 'true');

$response = $client->request("/$API_VERSION/Accounts/$ACCOUNT_SID/Participants/{$participant->CallSid}", "POST", $vars);

With all that under your belt, you’re all set to roll your own conference calling solution, or build something completely different. If you do, be sure to enter it into this week’s developer contest. You could win yourself an Amazon Kindle 3G, $100 in Twilio credit, and some awesome Twilio swag. Happy hacking!