Twilio is Turning Traditional PBX Upside Down

Twilio is Turning Traditional PBX Upside Down

September 1st, 2011 @

Over the years I have worked on a few interactive voice response systems for my customers, they have been complex and time-consuming to setup and run. We are hooked on the self-service trend and everyone knows how to press 1 for support. So to get there quickly and with minimal cost, Twilio makes it easy for web developers to extend application’s reach to the phone and SMS. With their platform, in the clouds, we can have an interactive system for real estate listings or apartments, checking reward points, finding a local reseller, or even a dating chat line, the possibilities go on.

To get your own Twilio application started, you will need to create a developer account , with that you will get a sandbox phone number and pin for testing your applications.

In this example we will allow for the caller to enter their zip code and get in touch with a local representative directly. The application will look up the phone number of representative servicing their area and forward the caller to that number. We could just as easily take a voicemail and email it to the representative, or call the representative and play back the message.

(1) Step one, the entry point for your application, what the caller first hears when calling, a simple XML fileĀ  (TwiML, Twilio Language) with instruction for the Twilio App.

Our Twilio number’s Voice URL is set to handle-incoming-call.xml. When someone calls the number Twilio will request this page with the standard TwiML request parameters.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="handle-user-input.php" numDigits="5">
<Say>Welcome to Our Company.</Say>
<Say>To speak to an agent in your area, enter your 5 digit zip code.</Say>
</Gather>
<!-- If customer doesn't input anything, prompt and try again. -->
<Say>Sorry, I didn't get your response.</Say>
<Redirect>handle-incoming-call.xml</Redirect>
</Response>

(2) Step two, create a table in MYSql to store the Representative names, numbers and zip codes.

CREATE TABLE representative_directory (
zipcode varchar(5) not null primary key,
phone_number varchar(32) not null,
fullname varchar(50) not null
);

INSERT INTO representative_directory (zipcode , phone_number, fullname) values ('90210','585-555-1112', 'John Smith');
INSERT INTO representative_directory (zipcode , phone_number, fullname) values ('14614','585-555-1113', 'Jane Smith');
INSERT INTO representative_directory (zipcode , phone_number, fullname) values ('14616','585-555-1114', 'Jim Smith');

(3) Step three, a simple PHP file namedĀ handle-user-input.php, that produces XML after querying the representatives phone number based on the user supplied zip code. note that we also chose to send errors in the output to the caller, if this were in production we would use the default office number and send an email to the administrator with the error.

<?php
//DB Constants - Change to your settings
$db_host='localhost';
$db_name='company_directory';
$db_user='db_username';
$db_passwd='db_password';
$defualt_office = '5855551234';
function getPhoneNumberByZipcode($zip){
global $db_name, $db_host,$db_user,$db_passwd;
mysql_connect($db_host, $db_user, $db_passwd) or die('<Say>Could not connect: ' . mysql_error(). '</Say></Response>');
mysql_select_db($db_name) or die('<Say>Could not select database</Say></Response>');
$query = 'SELECT phone_number FROM representative_directory where zipcode = '.mysql_real_escape_string($zip);
$result = mysql_query($query) or die('<Say>Query failed: ' . mysql_error(). '</Say></Response>');
$phone_number=false;
if($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$phone_number= $line['phone_number'];
}
mysql_close();
return $phone_number;
}

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

$user_pushed = (int) $_REQUEST['Digits'];

$phone_number = getPhoneNumberByZipcode($zip);
if($phone_number){
echo '<Say>Connecting you to an agent.</Say>';
echo '<Dial>+1'.$phone_number.'</Dial>';
}else{
echo "<Say>Sorry, I can't find an agent in your area. Connecting to head office.</Say>";
echo '<Dial>+1'.$defualt_office.'</Dial>';
}
echo '</Response>';
?>

All you need to do is place these two files on a web server, create and connect the database, then point the Twilio Voice URL in the App and you’re ready to accept calls. It’s that easy.

For a nice example of handling SMS messages longer than 160 character, see this article http://www.enhanceyourcalm.com/2011/06/how-to-handle-incoming-twilio-texts.html


Category : Blog &Featured

One Comment → “Twilio is Turning Traditional PBX Upside Down”


  1. Max Wood

    1 year ago

    Scott, We are a small software developer of desktop systems for a very narrow market niche (see website). We are interested in a product as you described in your blog here: http://bit.ly/ySJUCA

    Please drop me an email if it is something you can help with.

    Thanks.

    - Max

    Reply

Leave a Reply

Latest Posts

Recomendations

"Scott Davignon is one of the best managers I have ever worked with. During the time we worked together, I found him as a person with vast technical knowledge and great expertise of IT solutions... Read More."

Muhammad Saad Cheema, Consultant at InfoReliance Corporation (MCPD, MCTS, OCA, MCAD, Security+)

Subscribe Now