Get Started

TwiML Quickstart: Hello Monkey

Record a message from the caller

Easy so far... how about letting the caller record her own monkey howl? Sounds like fun.

  • quickstart/twiml/1.4/hello-monkey.php
    <?php
    
    	// make an associative array of callers we know, indexed by phone number
    	$people = array(
    		"+14158675309"=>"Curious George",
    		"+14158675310"=>"Boots",
    		"+14158675311"=>"Virgil",
    		"+14158675312"=>"Marcel"
    	);
    	
    	// if the caller is known, then greet them by name
    	// otherwise, consider them just another monkey
    	if(!$name = $people[$_REQUEST['From']])
    		$name = "Monkey";
    		
    	// 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>
    	<Play>http://demo.twilio.com/hellomonkey/monkey.mp3</Play>
    	<Gather numDigits="1" action="hello-monkey-handle-key.php" method="POST">
    		<Say>
    			To speak with a real monkey, press 1.  
    			Press 2 to record your own monkey howl.  
    	  		Press any other key to start over.
    	  	</Say>
    	</Gather>
    </Response>
        

We added an extra option in the <Say> block telling the caller if she presses '2' she can record audio. The next step is to add an additional processing instruction to our <Gather> 'action' handler.

  • quickstart/twiml/1.4/hello-monkey-handle-key.php
    <?php
    
    	// if the caller pressed anything but 1 or 2 send them back
    	if($_REQUEST['Digits'] != '1' and $_REQUEST['Digits'] != '2') {
    		header("Location: hello-monkey.php");
    		die;
    	}
    	
    	// otherwise, if 1 was pressed we Dial 3105551212. If 2 
    	// we make an audio recording up to 30 seconds long.
    	header("content-type: text/xml");
    	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
    <?php if ($_REQUEST['Digits'] == '1') { ?>
    	<Dial>+13105551212</Dial>
    	<Say>The call failed or the remote party hung up.  Goodbye.</Say>
    <?php } elseif ($_REQUEST['Digits'] == '2') { ?>
    	<Say>Record your monkey howl after the tone.</Say>
    	<Record maxLength="30" action="hello-monkey-handle-recording.php" />
    <?php } ?>
    </Response>
        

We added a new option to handle the case when someone presses '2'. We can again simulate the pressing of the '2' key by adding ?Digits=2 to the URL.

http://demo.twilio.com/hellomonkey/1.4/hello-monkey-handle-key.php?Digits=2

The handler now outputs a new response asking for your hoooooooooowl. The <Record> tag lets you record audio from the caller.

  • The 'maxLength' parameter tells Twilio to record up to 30 seconds of audio.
  • The 'action' parameter tells Twilio what to do after the caller is done recording. In this case we pass off to the hello-monkey-handle-recording.php page.
  • quickstart/twiml/1.4/hello-monkey-handle-recording.php
    <?php
    
    	// tell the caller that they should listen to their howl
    	// and play the recording back, using the URL that Twilio posted
    	header("content-type: text/xml");
    	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    ?>
    <Response>
    	<Say>Thanks for howling... take a listen to what you howled.</Say>
    	<Play><?php echo $_REQUEST['RecordingUrl']; ?></Play>
    	<Say>Goodbye.</Say>
    </Response>
        

The recording handler simply plays the recording back for the caller and says goodbye. The URL of the recording is passed to the handler in the request as the RecordingUrl parameter. The URL is externally accessible so you can send the URL of the howl to your friends or reference it in a webpage. Check out the Voice Recorder demo for another example of making voice recordings.