spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / perl / tutorial 20 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

Tellme More

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Dynamic VoiceXML

Continuing with our PIN number scenario, it's time to develop the CGI script that provides a response to the user's input. First, let's look at our completed VoiceXML form.

Example 4.

<?xml version="1.0"?>
<vxml version="1.0" > 
  <form id="login"> 
    <field name="pin"> 
  	   <grammar>
		      <![CDATA[
           Four_digits
      	  ]]>
  		  </grammar>	
		    <prompt>Please enter your 4 digit pin code.</prompt> 
  		  <filled>
  		    <submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl"/>
   	  </filled>
      <noinput count="1">No PIN entered.
        <reprompt/>
      </noinput>
      <noinput count="2">You must enter your PIN number to proceed.
        <reprompt/>
      </noinput>
      <noinput count="3">Please press or say exactly four numbers.
      </noinput>		 
   	  <nomatch count="1">Invalid pin code.
        <reprompt/>
      </nomatch>
      <nomatch count="2">Please press or say exactly four numbers.
        <reprompt/>
      </nomatch>
      <nomatch count="3">Too many attempts. 
                         Please call back another time.
        <exit/>
      </nomatch>
	   </field> 
  </form>
</vxml>
		

As you've probably already guessed, the CGI script will be written in Perl. As it turns out, if you know how to use the CGI.pm Perl module, you'll know how to handle VoiceXML form input.

#!/usr/bin/perl -w

use strict;
use CGI;

my %users = (
     '1234' => 'Eisenzopf',
     '2468' => 'King'
);

my $q = new CGI;
my $pin = $q->param('pin');
print "Content-Type: text/xml\n\n";
print <<VXML;
<?xml version="1.0"?>
<vxml>
  <form>
    <block>
VXML

print "Good Morning Mr. $users{$pin}. How may I help you?" 
   || "$pin is an invalid pin code.";

print <<VXML;
    </block>
  </form>
</vxml>
VXML

Basically, we create a new instance of the CGI.pm module. We access the value of the pin field with the following line of code:

my $pin = $q->param('pin');

It's important to make sure you return the proper HTTP header. The default header will not work properly. Instead, you must use the following statement as the first output back to Tellme:

print "Content-Type: text/xml\n\n";

The rest of the script is straightforward Perl. We lookup the user's last name based on their PIN code. If we find a name, we welcome them, otherwise, we return an error. It's a very rudimentary script, but it's our first taste of developing a dynamic VoiceXML application.


home / experts / perl / tutorial 20 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

http://www.internet.com