Twillio Cloud IVR Experiment Day-2

Looking around I found some new feature called ‘TwiML Endpoints‘ under the SIP menu. The TwiML Endpoint solution basically adds some basic IP Blocking Capabilities and Credential Management.

I don’t want to get bogged down by security right now and hence I’m going to proceed with the PHP Side as it is more powerful but in enterprise situations the TwiML Hybrid solution would be a better fit to offload load on customer’s Script Hosting Server. Second reason being that even in PHP Script the instructions sent back to Twillio are in TwiML, hence there is no double learning here.

So here is what I’m going to focus on building today…

This call flow should quickly help me to understand voice prompts (Both mp3 and tts, Call Related variable information collection, menu presentment, call recording, recording playback, call external number and finally disconnection. Lets start:

First I created the PhP for the Introduction menu (JFtest_intro.php):


    header(“content-type: text/xml”);

    echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;

$name = $_REQUEST[‘From’];



    <Say>Hello <?php echo $name ?>.</Say>



It’s a simple code that collects the CLI stored in the global variable ‘from’ into a local variable name and then play it back using tts

It then calls the main menu (JFtest_mainmenu.php)


    <Say>You have reached Main Menu.</Say>

    <Gather numDigits=”1″ action=”JFtest_mainmenu_handle.xml” method=”POST”>


            To speak with a monkey, press 1.

            Press 2 to Check Your Voice.

             Press 3 to call Julian Frank.

Press 4 to Disconnect the call..




This code initiates the dialed digit collection and specifies the ‘handle’ script to be used to handle any entries…This is a simple one…the handle Script (JFtest_mainmenu_handle.php) has more action


    header(‘Content-type: text/xml’);

    echo ‘<?xml version=”1.0″ encoding=”UTF-8″?>’;

    echo ‘<Response>’;

    # @start snippet

    $user_pushed = (int) $_REQUEST[‘Digits’];

    # @end snippet

    if ($user_pushed == 1)

    {    echo ‘<Say>Transfering call to Monkey</Say>

     <Play></Play>&#8217;;    }

    elseif ($user_pushed == 2) {

    echo ‘<Say>Record your Voice after the beep. Press # or * to stop recording</Say>

    <Record maxLength=”10″ action=”JFtest_voicecheck.php” />’;


elseif ($user_pushed == 3) {

    echo ‘    <Say>Calling Julian. Leave a voicemail.</Say>



elseif ($user_pushed == 4) {

    echo ‘ <say>Bye Bye</say>



echo ‘<Redirect>JFtest_mainmenu.php</Redirect>’;

    echo ‘</Response>’;


That’s a lot of code…but nothing complicated….

The code $User_Pushed picks the digits dialed into $User_Pushed….This is then used to select the option we want to perform.

Options 1: Its simple…It just does a TTS and then plays the mp3 hosted in twilio site

Option 2: This is interesting . I wanted the user to speak for 10 seconds and then speak it back….Looks like I cant do it in the same script and need a handler script ‘JFtest_voicecheck.php’ to handle the recorded voice. Will provide that code later.

Option 3 : Simple ..Dial my Google Voice number …You can leave me a voicemail…Be Kind though J

Option 4: Disconnects the call using <Hangup/> code.

The JFtest_voicecheck.php handler doesn’t do much in my test


    header(“content-type: text/xml”);

    echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;



    <Say>Thanks for howling… take a listen to what you howled.</Say>

    <Play><?php echo $_REQUEST[‘RecordingUrl’]; ?></Play>




It just thanks and plays back the recorded content stored in temporary Global variable ‘RecordingUrl’. I could extend this to store in some location to make this a voicemail application. Twillio also seems to have DB Integration capabilities…Gotto try it out but later…Till then I’ll try to survive all the Smog created by Diwali crackers here..


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s