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.
Example:-
Hello Bob!
Press 1 to speak with support
Press 2 to record your message
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php // contacts array will List everyone your contacts $contacts = array( "9495797676"=>"John", "9495776878"=>"Lily", "3102168544"=>"Bob", "8587771234"=>"Wendy", "6058393817"=>"Amanda" ); // 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"; ?> <Response> <Say>Hello <?php echo $name; ?>.</Say> <Gather numDigits="1" action="hello-world-handle-key.php" method="POST"> <Say> Press 1 To speak with Support. Press 2 to record your message. Press any other key to start over. </Say> </Gather> </Response> |
hello-world-handle-key.php
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.
- Once the recording if complete the Record API will call hello-world-handle-recording.php URL.
The example below will help you understand how we handle this in the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // Check the digit caller pressed if($_REQUEST['Digits'] != '1' and $_REQUEST['Digits'] != '2') { header("Location: hello-world.php"); die; } // 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"; ?> <Response> <?php if ($_REQUEST['Digits'] == '1') { ?> <Dial>19499300360</Dial> <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 } ?> </Response> |
If the caller had Pressed 2 then the code above call’s :
hello-world-handle-recording.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // play the recorded message back to the caller. header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <Response> <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> <Say>Goodbye.</Say> </Response> |
By following these steps:-
- We have successfully greeted the caller by name.
- Prompted them to indicated their choice by pressing either 1 or 2.
- Based on their choice the call was either connected to support or they were prompted to record a message.
- 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