Hello,
This afternoon I got a ticket over here at the Fortress of Solitude, err I mean Voxeo Support that I thought I would share with the class. This developer opened a ticket asking how to implement control document execution in his applications. In short he was looking to prompt his callers, in a loop, and stay in said loop until the user explicitly opts to hung up, or proceed with the call. This developer was stuck on whether or not he would have to explicitly repeat the ask() with each iteration.
The developer was also wondering how he could alternate prompts in hopes of giving his application a more natural feel for his users. I could immediately see how implementing this could be confusing for some of our developers, and also how this could be helpful on any channel not just Voice (SMS, Instant Message, Twitter,ect). This being the case I thought this most certainly warranted a blog posting to shared the example with our developers in hopes it may prove helpful; And now for our feature presentation….
<?php
answer();
//Global options for ask, declare once, use many =)
$globalOptions = array(
"choices" => "one( 1, one), two( 2, two),exit(3,exit)",
"repeat" => 3,
"onBadChoice" => create_function( '$event', 'say("I am sorry, I did not understand what you said.");' ),
"onTimeout" => create_function( '$event', 'say("I am sorry. I didn\'t hear anything.");' )
);
$promptArray = array(
"Hello, to select the first option please press or say one, for the second option please press or say two. To exit the application please say exit or just press 3",
"For option one press or say one, or for option two press or say two"
);
$promptIteratorVariable = 0;
/**
* whichPromptToSay
*
* @return Array element of our array ($promptArray) depending on count of $promptIteratorVariable
*/
function whichPromptToSay(){
global $promptArray;
if ($promptIteratorVariable==0){
$promptIteratorVariable++;
return $promptArray[0];
}else{
return $promptArray[1];
}
}
/**
* whichVerbToSay()
*
* @return element of our verb array
*/
$verbArray = array("selected","chose","picked","opted for");
function whichVerbToSay(){
return $verbArray[rand(0,(count($verbArray)-1))];
}
//We will continue our loop until false, but first we need to set to true
$continueLoop = true;
while($continueLoop){
$result = ask(whichPromptToSay(),$globalOptions);
if($result->name=='choice'){
if ($result->value=="one"){
say( "You " . whichVerbToSay() . " option one");
}elseif ($result->value=="two"){
say( "You " . whichVerbToSay() . " option two");
}elseif ($result->value=="exit"){
say( "peace out");
$continueLoop = false;
}
}
}
hangup();
?>
The thing to take home from this example is that you can make use of functions to add a bit of flair to your applications, as well as loops to control application execution. These two techniques will hopefully not only help you have cleaner code, but also help you provide a better, more natural, user experience for your callers! If there are any questions on anything presented here please don’t hesitate to let us know, as our team is most certainly standing by to offer any help our developers may require!
Regards,
John Dyer
Customer Engineer
Voxeo Support

