Making An Applet

Hello Monkey v1.2

To finish up our Hello Monkey quickstart we’re going to implement a call screening function that is going to check the incoming caller’s phone number against a user or group of your choice. If the incoming number matches the user or group you’ve selected, then the primary dropzone will be used to direct the call. If the caller is unknown then a secondary dropzone will be used to direct the call.

We’ll begin by adding the UserGroupPicker element as well as a second dropzone.

<div class="vbx-applet monkey-applet">
	<h2>Create A Custom Message</h2>
	<p>Enter in a custom message that your callers will be greeted by.</p>
	<textarea class="medium" name="prompt-text"><?php 
		echo AppletInstance::getValue('prompt-text') 
	?></textarea>
	
	<br />
	
	<h2>Call Screening Options</h2>
	<table class="vbx-menu-grid options-table">
	<thead>
		<tr>
			<td>Number To Screen</td>
			<td>&nbsp;</td>
			<td>Action if caller matches</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<fieldset class="vbx-input-container">
					<input type="text" class="small" value="<?php 
						echo AppletInstance::getValue('key')  
					?>" name="key"/>
				</fieldset>
			</td>
			<td>&nbsp;</td>
			<td>
			<?php echo AppletUI::dropZone('primary', 'Drop item here'); ?>
			</td>
		</tr>
	</tbody>
	</table>

	<h3>Fallback Action</h3>
	<p>This action will be executed if the caller is unknown.</p>
	<?php echo AppletUI::DropZone('fallback'); ?>
</div>

Now that we’ve got the UI setup we need to expand twiml.php.

<?php

// The response object constructs the TwiML for our applet
$response = new TwimlResponse;

// Add a say based on what has been entered in on the call flow
$response->say(AppletInstance::getValue('prompt-text'));

// $primary stores the url to the primary action that was selected
$primary = AppletInstance::getDropZoneUrl('primary');

// $fallback stores the url to the fallback action selected
$fallback = AppletInstance::getDropZoneUrl('fallback');

// We're taking the caller's number and the number entered in on the applet
// and normalizing them so we can then compare them
$caller = normalize_phone_to_E164($_REQUEST['Caller']);
$screenNumber = normalize_phone_to_E164(AppletInstance::getValue('key'));

if($caller == $screenNumber) {
	// If the caller's number matches the number that we proceed with the
	// primary action.
	$response->redirect($primary);
}
else {
	// if the caller is unknown, proceed with the fallback action
	$response->redirect($fallback);
}

// This will create the twiml for hellomonkey
$response->respond();

Now when someone calls and this applet is used it will greet them with your custom message and check if the number they’re calling from matches the selected user/group. If so it will add the primary redirect and if the caller is unknown then the fallback redirect is added.

If you’re interested in expanding your Hello Monkey applet be sure to check out the Applet API page.

Hello Monkey v1.1 Hello Monkey v1.3