Volume 1, Issue 6 - June 2001
   
 

How Much is that Pizza, Anyway?

By Rob Marchand

(Continued from Part 1)

So, this package of information will be sent to a web server. How do we get at the data? We can't cover all the server side technologies, but we'll have a quick look at two that will give you an idea of how easy this can be. First (finally!) consider the infamous pizzaCart.pl.

#!/usr/local/bin/perl -w

# For perl, see http://www.cpan.org
# For Lincoln Stein's CGI module for perl:
# See http://www.cpan.org/modules/by-module/CGI/CGI.pm-2.753.tar.gz

use CGI; # This creates a new query object, which
# parses the HTTP request, and decodes the parameters. $q = new CGI; # We can now access the form variables via the
# query object: $pizza_cost = 10.00; $type = $q->param('orderItem'); $count = $q->param('orderCount'); # Decide what to say if ( $type eq 'pizza' ){ $message = sprintf( "%s \$%.2d", "Your $type order will cost",
$count * $pizza_cost ); }else { $message = "We don't really sell $type"; } # Send the HTTP response header print $q->header; # Now send the script. print qg( <?xml version="1.0"?> <vxml version="1.0"> <form> <block> <prompt> $message </prompt> <prompt> Thanks for calling! </prompt> <disconnect/> </block> </form> </vxml> ); #end of the in-line script data. exit;


Here's a look at the same functionality using PHP:

<?php

//This is a sample of a PHP page that produces VoiceXML
//Variables submitted to this page are
//available using their field variable
//name.  So, for example, we have a
//filed variable 'foo', then it shows up

//here after a submit, as '$foo'.

print(",?xml version=\"1.0\"?>");

?>

<vxml version="1.0">

<meta name"maintainer" content=""/>
<meta name="application" content="PHP Sample"/>

<?php

$pizza_cost = 10.00;

if ( $orderItem =='pizza' ){
  $message = sprintf( "%s\$%.2d", " Your $orderItem will cost",
    $orderCount * $pizza_cost );


}else {
  $message = "We don't really sell $orderItem";
}

//We can switch back and forth between in-line
//VoiceXML and PHP code.  The line below shows
//a code snippet in-line.

?>

<form>
  <block>
    <prompt>
      <? print($message) ?>
    </prompt/>

    <prompt>
      Thanks for calling!
    </prompt>
    <disconnect/>
  </block>
</form>
</xvml>


Computer:
What would you like to order? Say pizza, drinks, salad or wings
Human: salad
Computer: How many salad would you like?
Human: Thirty five
Computer: Please wait while I add thirty five salad to your order.
Computer: We don't really sell salad
Computer: Thanks for calling!

Or...

Computer: What would you like to order? Say pizza, drinks, salad or wings
Human: pizza
Computer: How many pizza would you like?
Human: Twenty two
Computer: Please wait while I add twenty two pizza to your order.
Computer: Your pizza will cost two hundred and twenty dollars
Computer: Thanks for calling!

Each script decodes the data that is passed as part of the request from the VoiceXML platform (PHP provides this implicitly, as noted in the script, while Perl requires use of something like CGI.pm). The scripts then generate a message based on the type of item that has been ordered, and deliver a new VoiceXML page containing the customized message. Regardless of which server-side technology you're using you'll probably follow a similar model of template processing. The main point to take away from this is that you can reuse the same technologies you're using now. PHP and the Perl plus CGI.pm combination are targeted at producing HTML. We can use them as is to produce VoiceXML.

Things to Keep in Mind

There are some things you'll need to keep in mind when working with VoiceXML server-side applications, some of which apply to the traditional visual web as well, and some which don't (or which you usually don't have to worry about).

  • Content types - When delivering VoiceXML pages, audio files, and so on, you may have to worry about the content types being sent by the web server when delivering these files. Some VoiceXML platforms care about these, and some don't. So your mileage may vary.

  • Performance - If your application has lots of big audio files, external grammar files, or even large VoiceXML pages, you'll want to think about caching issues, and how you can take advantage of them (again, more on this next month).

  • Web Server Configuration - There may be other issues you'll need to sort out with your web server configuration. For example, if you're using JSP, you'll need a servlet container and JSP processor (like Tomcat). If you have questions about these things, please feel free to drop me a line (), and I'd be happy to provide further information.

What's Next?

Hopefully this has given you a small taste of how VoiceXML can fit into your server-side development. Some of you will be saying 'Hey, why not use some ECMAScript to do this!' Well, you're right, we could do that, and we will next month. We're also going to talk about caching, and how you can take advantage of the caching-related features provided by VoiceXML.

Watch future issues of VoiceXML Review for more articles about getting started with VoiceXML.

back to the top

 

Copyright © 2001 VoiceXML Forum. All rights reserved.
The VoiceXML Forum is a program of the
IEEE Industry Standards and Technology Organization (IEEE-ISTO).