Collect Response

Collect users response to build the IVR, or transfer the call to right person based on users input.

Collect Response

In the previous example you were able to identify the caller and greet them by name. Now lets take it to next level. Our caller has been greeted by name and is now prompted by the system to make a choice from provided options.


Hello Bob! Press 1 to speak with support Press 2 to record your message


       // contacts array will List everyone your contacts
            $contacts = array(    
        // if the caller is known, then greet them by name
        // otherwise, just identify them as another caller
        if(!$name = $contacts[$_REQUEST['From']])
                $name = "there";

        // now greet the caller
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        <Say>Hello <?php echo $name; ?>.</Say>
        <Gather numDigits="1" action="hello-world-handle-key.php" method="POST">
                        Press 1 To speak with Support.
                        Press 2 to record your message.
                        Press any other key to start over.

We start of by the code we wrote for “Identify caller by name”. We will add gather keyword to it. After greeting the caller, gather keyword is triggered and it prompts the caller to make a choice by pressing the assigned keys from their phone. In our example “1″ is to speak to Support and “2″ is to record the message. As soon as the user makes a choice by pressing either one of the keys we call:-


This files job is to handle and respond to the user input. We pass the key to post/get data with name “Digits” This will check what Digit the caller has pressed.

  • If the caller did not press either 1 or 2, then we redirect to the URL hello-world.php.

  • If they pressed 1, then we will forward the call to 19499300360 and disconnect the call.

  • If they pressed 2, then the Record API will ask them to record their Name and Message.

    • Once the recording if complete the Record API will call hello-world-handle-recording.php URL. This file will play back the message to the caller and hang up.

The example below will help you understand how we handle this in the code

        // Check the digit caller pressed 
        if($_REQUEST['Digits'] != '1' and $_REQUEST['Digits'] != '2') {
                header("Location: hello-world.php");
        // If 1 connect to office.  
        // Let the person record some sound.
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
<?php if ($_REQUEST['Digits'] == '1') { ?>
        <Say> Goodbye.</Say>
<?php } elseif ($_REQUEST['Digits'] == '2') { ?>
        <Say>Lets record your own sound and we will play it back.</Say>
        <Record maxLength="30" action="hello-world-handle-recording.php" />
<?php } ?>

If the caller had Pressed 2 then the code above call’s :


        // play the recorded message back to the caller.  
        header("content-type: text/xml");
        echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        <Say>Your recorded message is being played back.</Say>
        <Play><?php echo $_REQUEST['RecordingUrl']; ?></Play>
        <say>This was a demo system, we are excited to see what you will build.</say>

The output of this code is to Play the recorded message to the caller and hangup.

By following these steps:-

  1. We have successfully greeted the caller by name.

  2. Prompted them to indicated their choice by pressing either 1 or 2.

  3. Based on their choice the call was either connected to support or they were prompted to record a message.

  4. Recorded message is played back to the caller and the call is completed.

Key words used in the above code are: Say Reads the text and convert’s that to audio and play to the caller. Dial Connect the existing call to another party Gather Collect the digits pressed by the caller Play Play a mp3/wav audio file from the web server. Record Caller is given the option to record the message

Last updated