VoiceXML Adventure Game
     Jonathan Eisenzopf
     Copyright © 2001 by internet.com
     March 8, 2001

     Continuing from Part I [http://webref.com/perl/tutorial/20] of our comprehensive series
     of VoiceXML tutorials, in Part II we will develop a simple, but fully dynamic
     VoiceXML application in Perl for the Tellme.com voice portal.


Overview
     "You are in an open field west of a big white house with a
     boarded front door. There is a small mailbox here."

     Do you remember the original Zork text adventure game and Choose Your Own Ad-
     venture books? Maybe you're even old enough to remember the original Adventure
     game. If you don't, you're missing out. We're going to develop a simple Zork-like ap-
     plication using VoiceXML. In Part III, we will create a more advanced version that uti-
     lizes its own XML format for building text adventures.


Examples
     As we step through each code example, you will also be able to dial into Tellme and
     demo the code live on the Mother of Perl Tellme extension. Simply dial 1-800-tellme,
     wait for the Tellme main menu, then dial 1-73759. You will hear a greeting and a
     menu of options. Dial 2 for Tutorial 21 and then press the number that corresponds
     with the example in the tutorial. For example, when you see:

     Example 1.

     you can interact with the source code by pressing 1 on your phone. Each example is
     also linked to the XML source code file, where you can examine the file in your Web
     browser, or launch it directly into your favorite XML editor.

     In addition to the live examples, you will find the source code for all examples in the
     tutorial21.zip [tutorial21.zip] file, which you can use for free to create your own
     VoiceXML applications.


Creating a Grammar for DTMF Input

Grammars
                                               1
Before we proceed any further, we need to talk a bit more about grammars. They are a
      very important part of any VoiceXML document. A grammar is a set of expressions
      that define what DTMF tones and spoken input will be recognized by the system.
      These grammars are limited to what you program them to recognize. Depending on
      where you place the grammar, the scope can be the whole document, a form, or a field
      within a form. The Tellme grammar syntax uses the Nuance Grammar Specification
      Language (GSL).

      It's not too difficult to define a simple grammar that allows a user to make a selection
      via DTMF tones. Let's define a simple grammar that provides our users with the option
      to open one of three doors in our adventure.

          <grammar><![CDATA[
                  [
                     [dtmf-1] {<option "door1">}
                     [dtmf-2] {<option "door2">}
                     [dtmf-3] {<option "door3">}
                  ]
                ]]>
          </grammar>


      First, grammars are placed within the grammar element. You should also surround the
      grammar with a CDATA section, which tells the XML parser not to process the text in-
      side. In the grammar above, we have defined three options. Each option is defined by a
      grammar statement. The statement consists of an expression to be matched, and the
      corresponding value to return when a match occurs.

      The expression used to match a digit is: dtmf-$ where $ is the number we want to
      match. For example, dtmf-1 would be matched if a user presses 1 on their telephone
      keypad.

      The second part of the expression defines the value that will be returned when we get a
      match. For example, {<option "foo">} will return the text foo when the user in-
      put matches the first part of the expression.

      Next we're going to apply this simple grammar to a real VoiceXML document.


Getting DTMF Input
      The first version of our adventure game below will only accept DTMF input. The user
      can select 1, 2, or 3, which corresponds with one of the three doors.

      An example user session follows:


                                                 2
Tellme: You are in a small room with three doors.

Tellme: To open the first door, press 1.

Tellme: To open the second door, press 2.

Tellme: To open the third door, press 3.

User: (pressed 1)

Tellme: You see a large hungry monkey.

Now, let's take a look at the VoiceXML document.

Example 1. [example1.xml]

<?xml version="1.0"?>
 <vxml version="1.0">
   <form>
     <field name="answer">
       <grammar><![CDATA[
            [
              [dtmf-1] {<option "door1">}
              [dtmf-2] {<option "door2">}
              [dtmf-3] {<option "door3">}
            ]
          ]]>
       </grammar>
       <prompt><audio>You are in a small room with three
 doors.</audio>
       <pause>300</pause> <audio>To open the first door, press
 1.</audio>
       <pause>300</pause> <audio>To open the second door, press
 2.</audio>
       <pause>300</pause> <audio>To open the third door, press
 3.</audio> </prompt>
     </field>
     <filled>
       <result name="door1">
          <audio>You see a large hungry monkey.</audio>
                 <reprompt/>
       </result>
       <result name="door2">
          <audio>You see another room with three doors, a man, and his
 monkey.</audio>
          <reprompt/>
       </result>
       <result name="door3">

                                            3
<audio>You see a man scratching his
      monkey.</audio>
              <reprompt/>
            </result>
          </filled>
        </form>
      </vxml>

     For more information on using the filled and result elements, please see Page 3
     [http://webref.com/perl/tutorial/20/3.html] of the first part of this tutorial. You might
     have also noticed the pause element in the example above. Using the element forces
     Tellme.com to pause between prompts. In this case, we want to stop for 300 millisec-
     onds between options to duplicate the pause that would occur in natural speech.


Adding Voice Selections
     Now that we have a simple interactive application working, we need to add the ability
     for users to provide voice commands in addition to DTMF input. This is done by
     adding additional grammar expressions.

         <grammar><![CDATA[
                 [
                    [dtmf-1 one] {<option "door1">}
                    [dtmf-2 two] {<option "door2">}
                    [dtmf-3 three] {<option "door3">}
                    [(touch the monkey)] {<option "monkey">}
                 ]
               ]]>
         </grammar>


     If you compare the new grammar element above to the one on the previous page, you'll
     notice that we've added addition information for each option as well as a new line. The
     addition information consists of a single word for each option. For example, the first
     line of the grammar instructs the Tellme VoiceXML parser to match when the user
     presses 1 on their keypad or if they speak the word one. The last line of the grammar
     matches on the phrase, "touch the monkey." There will not be a match if the user says
     "touch" or "monkey." It will only match when the user says "touch the monkey." This
     is because the phrase is contained within paretheses, which require that the whole
     phrase be matched.

     Example 2. [example2.xml]

     <?xml version="1.0"?>
      <vxml version="1.0">
        <form>

                                                4
<field name="answer">
                           <grammar> <![CDATA[
                     [
                       [dtmf-1 one] {<option "door1">}
                       [dtmf-2 two] {<option "door2">}
                       [dtmf-3 three] {<option "door3">}
                       [(touch the monkey)] {<option "monkey">}
                     ]
                  ]]>
                           </grammar>
                           <prompt> <audio>You are in a small room with thr
      doors.</audio>
                             <pause>300</pause> <audio>To open the first do
      1.</audio>
                             <pause>300</pause> <audio>To open the second d
      2.</audio>
                             <pause>300</pause> <audio>To open the third do
      3.</audio>
                           </prompt>
                 </field>
                 <filled>
                           <result name="door1">
                             <audio>You see a large hungry
      monkey.</audio>
                             <reprompt/>
                           </result>
                           <result name="door2">
                             <audio>You see another room with three doors,
      monkey.</audio>
                             <reprompt/>
                           </result>
                           <result name="door3">
                             <audio>You see a man scratching his
      monkey.</audio>
                             <reprompt/>
                           </result>
                           <result name="monkey">
                             <audio>No! Do not touch the
      monkey!</audio>
                             <reprompt/>
                           </result>
          </filled>
        </form>
      </vxml>



Handling Events
     Next, we need to add handlers for the nomatch and noinput events using the methods
                                             5
specified in Page 4 [http://www.webref.com/perl/tutorial/20/4.html] of the previous tu-
torial.

Example 3. [example3.xml]

<?xml version="1.0"?>
 <vxml version="1.0">
   <form>
            <field name="answer">
                      <grammar> <![CDATA[
                [
                  [dtmf-1 one] {<option "door1">}
                  [dtmf-2 two] {<option "door2">}
                  [dtmf-3 three] {<option "door3">}
                  [(touch the monkey)] {<option "monkey">}
                ]
              ]]>
                      </grammar>
                      <prompt> <audio>You are in a small room with thr
 doors.</audio>
                        <pause>300</pause>
                        <audio>To open the first door, press or say
 1.</audio>
                        <pause>300</pause>
                        <audio>To open the second door, press or say
 2.</audio>
                        <pause>300</pause>
                        <audio>To open the third door, press or say
 3.</audio>
                      </prompt>
                      <nomatch>
                        <audio>You fool! That is not a choice I am giv
 you.</audio>
                        <pause>300</pause>
                        <audio>Press or say 1, 2, or
 3.</audio>
                        <listen/>
                      </nomatch>
                      <noinput>
                        <audio>What are you waiting
 for?</audio>
                        <pause>300</pause>
                        <audio>Press or say 1, 2, or
 3.</audio>
                        <listen/>
                      </noinput>
            </field>
            <filled>
               <result name="door1">
                        <audio>You see a large hungry

                                          6
</audio>
                                     <reprompt/>
                                   </result>
                                   <result name="door2">
                                     <audio>You see another room with three doors,
      monkey.</audio>
                                     <reprompt/>
                                   </result>
                                   <result name="door3">
                                     <audio>You see a man scratching his
      monkey.</audio>
                                     <reprompt/>
                                   </result>
                                   <result name="monkey">
                                     <audio>No! Do not touch the
      monkey!</audio>
                                     <reprompt/>
                                   </result>
          </filled>
        </form>
      </vxml>


Adding Help
     In most VoiceXML applications, you'll want to provide some help facility. Most peo-
     ple expect to get some kind of help when they press 0 on their phone, whether it be a
     live operator or automated assistance. In the example below, we've added a DTMF and
     voice grammar item that calls the help element in the document below.

     Example 4. [example4.xml]


     <?xml version="1.0"?>
      <vxml version="1.0">
        <form>
                <field name="answer">
                       <grammar> <![CDATA[
                 [
                   [dtmf-1 one first] {<option "door1">}
                   [dtmf-2 two second] {<option "door2">}
                   [dtmf-3 three third] {<option "door3">}
                   [(touch the monkey)] {<option "monkey">}
                   [dtmf-0 huh help what doh] {<option "help">}
                 ]
               ]]>
                       </grammar>
                       <prompt>
                         <audio>You are in a small room with three

                                              7
</audio>
                   <pause>300</pause>
                   <audio>Which one do you want to
 open?</audio>
                 </prompt>
                 <nomatch count="1">
                   <audio>Wrong door.</audio>
                   <audio>To open the first door, press or say
 1.</audio>
                   <pause>300</pause>
                   <audio>To open the second door, press or say
 2.</audio>
                   <pause>300</pause>
                   <audio>To open the third door,
                   press or say 3.</audio>
                   <listen/>
                 </nomatch>
                 <nomatch count="2">
                   <audio>You fool! That is not a choice I am giving
 you.</audio>
                   <pause>300</pause> <audio>Press or say 1, 2, or
 3.</audio>
                   <listen/>
                 </nomatch>
                 <nomatch count="3">
                   <audio>Pretend you have a brain and press 1, 2, or
 3.</audio>
                   <listen/>
                 </nomatch>
                 <noinput count="1">
                   <audio>What are you waiting
 for?</audio>
                   <pause>300</pause>
                   <audio>Press or say 1, 2, or 3.</audio>
                   <listen/>
                 </noinput>
                 <noinput count="2">
                   <audio>If you do not press 1, 2, or 3,
                   I am going to kill the monkey!</audio>
                   <listen/>
                 </noinput>
                 <noinput count="3">
                   <audio>Ok. That's it. The monkey is dead and it's
                   your fault because you did not choose 1, 2, or
 3.</audio>
                   <listen/>
                 </noinput>
                 <noinput count="4">
                   <audio>1, 2, or 3.</audio>
                   <listen/>

                            8
</noinput>
                                <help>
                                  <audio>Why are you asking for
      help?</audio>
                         <pause>300</pause>
                         <audio>Go away.</audio>
                         <reprompt/>
                       </help>
                </field>
                <filled>
                  <result name="door1">
                         <audio>You see a large hungry
      monkey.</audio>
                         <reprompt/>
                       </result>
                       <result name="door2">
                         <audio>You see another room with three doors,
                         a man, and his monkey.</audio>
                         <reprompt/>
                       </result>
                       <result name="door3">
                         <audio>You see a man scratching his
      monkey.</audio>
                         <reprompt/>
                       </result>
                       <result name="monkey">
                         <audio>No! Do not touch the
      monkey!</audio>
                         <reprompt/>
                       </result>
          </filled>
        </form>
      </vxml>

     If you hadn't noticed, I also added multiple elements for the nomatch and noinput han-
     dlers.


Making Our Adventure Dynamic
     Now that we have a working VoiceXML document that provides some dynamic capa-
     bilities, it's time to tie in into a back-end Perl script that provides true dynamic capabil-
     ities. For this first run of our dynamic Perl script, we'll develop a script that dynami-
     cally generates rooms. Each room will always have three doors. Because the script is
     random, there are many different possible combinations. Initially, users won't be able
     to interact with any of the objects in the rooms, they'll only be able to open doors. In
     Part III, we'll add this capability.



                                                  9
room.pl
          use CGI;
          use strict;
          my @rooms = (
                       'large ball room',
                       'pool hall',
                       'large red barn',
                       'seven eleven',
                       'bedroom',
                       'bathroom',
                       'funeral parlor',
                       'windy room with bark walls',
                       'dimly lit tunnel',
                       'prison cell',
                       'hospital waiting room',
                       'underground parking garage',
                       'circular stairwell',
                       'arboretum',
                       'janitors closet',
                       'closet',
                       'subway car',
                       'cubicle',
                       'lighthouse tower',
                       'graveyard'
                      );
          my @objects = (
                          'full of clowns dressed in red',
                          'full of smoke but no people',
                          'with a dead horse on the floor',
                          'with a broken slurpy machine. In the corner, a troll
                          'with black curtains and something under the covers',
                          'with padded walls',
                          'with wood bark walls',
                          'with flickering florescent lights',
                          'with an oil spotted shag carpet',
                          'full of mad raving lunatics',
                          'with a man and his monkey',
                          'full of monkeys',
                          'with William Shatner singing tambourine man',
                          'with a large hungry monkey',
                          'full of drunken sailors',
                          'full of blind dwarves',
                          'full of Windows programmers',
                          'with a man scratching his monkey'
                         );
          my $q = new CGI;
          print "Content-Type: text/xmlnn";

                                     10
&prompt;
sub prompt {
my $room    = $rooms[int(rand(scalar(@rooms)))];
my $object = $objects[int(rand(scalar(@objects)))];
   print <<VXML;
<?xml version="1.0"?>
<vxml version="1.0">
  <form>
    <field name="answer">
      <grammar>
         <![CDATA[
           [
             [dtmf-1 one first] {<option "door1">}
             [dtmf-2 two second] {<option "door2">}
             [dtmf-3 three third] {<option "door3">}
             [(touch the monkey)] {<option "monkey">}
             [dtmf-0 huh help what doh] {<option "help">}
           ]
         ]]&gt;
      </grammar>
      <prompt>
         <audio>You are in a $room $object. There are three
doors.</audio>
      </prompt>
      <filled>
         <submit
next="http://www.textant.com/cgi-bin/room.pl"/>
      </filled>
      <nomatch count="1">
         <audio>Wrong door.</audio>
         <audio>To open the first door, press or say
1.</audio>
         <pause>300</pause>
         <audio>To open the second door, press or say
2.</audio>
         <pause>300</pause>
         <audio>To open the third door, press or say
3.</audio>
         <listen/>
      </nomatch>
      <nomatch count="2">
         <audio>You fool! That is not a choice I am giving
you.</audio>
         <pause>300</pause>
         <audio>Press or say 1, 2, or 3.</audio>
         <listen/>
      </nomatch>
      <nomatch count="3">
         <audio>Pretend you have a brain and press 1, 2, or

                           11
</audio>
               <listen/>
             </nomatch>
             <noinput count="1">
               <audio>You need to select a door.</audio>
               <pause>300</pause>
               <audio>Press or say 1, 2, or 3.</audio>
               <listen/>
             </noinput>
             <noinput count="2">
               <audio>If you do not press 1, 2, or 3, I am going to kill
               the monkey!</audio>
               <listen/>
             </noinput>
             <noinput count="3">
               <audio>Ok. That's it. The monkey is dead and it's your
               fault because you did not choose 1, 2, or
      3.</audio>
               <listen/>
             </noinput>
             <noinput count="4">
               <audio>1, 2, or 3.</audio>
               <listen/>
             </noinput>
             <help>
               <audio>Why are you asking for help?</audio>
               <pause>300</pause>
               <audio>Go away.</audio>
               <reprompt/>
             </help>
           </field>
        </form>
      </vxml>
      VXML
      }




Conclusion
     I hope this column has provided a bit of nostalgia for some of you who grew up play-
     ing paper and computer adventure games like D&D, Battletech, Adventure, Zork,
     Leisure Suit Larry, Loom, and Space Quest. Alas, the era of adventure games seems to
     have passed. No longer are games with extensive plots that really draw users into an al-
     ternate universe. Games like BloodNet and Ultima brought a short resurgence, but I'm
     afraid we may not see another for a while. Shootemups like Quake and Half Life have
     all but replaced the adventure games.


                                               12
What's even more depressing is seeing some of the great gaming companies like
FASA, Micropose, Spectrum Holobyte, and Infocom go out of business or get bought
out by larger companies who really don't care too much about making great games.

Well, that's ok, because in Part III, we're going to develop our own text adventure plat-
form in an XML format that will work with HTML, WAP, and VoiceXML. We'll let
our nostalgia run its course and then perhaps we'll look at utilizing this technology for
something truly useful. But right now, it's time to play. We'll see you in Part III. Until
then, I'm going to go back through those sweet memories of yesteryear.




                                           13

More Related Content

PPT
PDF
Harendra Singh,BCA Third Year
PDF
Gaurav Jatav , BCA Third Year
PPTX
Monty problem
PDF
Mithlesh Singh Rawat , BCA Third Year
PDF
Ravi Prakash Yadav , BCA Third Year
PPTX
Vp lecture 2 ararat
PDF
O PROCESSO DE INSERÇÃO PROFISSIONAL DE UMA TUTORA VIRTUAL INICIANTE
Harendra Singh,BCA Third Year
Gaurav Jatav , BCA Third Year
Monty problem
Mithlesh Singh Rawat , BCA Third Year
Ravi Prakash Yadav , BCA Third Year
Vp lecture 2 ararat
O PROCESSO DE INSERÇÃO PROFISSIONAL DE UMA TUTORA VIRTUAL INICIANTE

Viewers also liked (15)

PPTX
Opponeringen
PDF
VIVÊNCIAS DE UM PROFESSOR DE MATEMÁTICA EM INÍCIO DE CARREIRA: RELATO DE UMA ...
PPTX
2.22 session 23 einheit 5
PPT
Fd1 Class Introduction
PDF
Pest Awareness for You
DOC
Roderik van Zeist
PPT
Biology Chapter 28 Section 2[1]
PPTX
[HAFT] 행복나무 프로젝트 - 행복되감기
PPT
Verona prof dev presentation singleton elad 540
PPTX
Curzon, generosity in startup land -it is not just about doing good, phx star...
PDF
MEMÓRIAS DAS MINHAS PRIMEIRAS EXPERIÊNCIAS COMO DOCENTE DE MÚSICA NO ENSINO R...
PDF
Book - Thesis-draft 05- FINAL 2015-12-10
DOCX
Доброта
DOC
John Kauffman Resume 2016 (1)
Opponeringen
VIVÊNCIAS DE UM PROFESSOR DE MATEMÁTICA EM INÍCIO DE CARREIRA: RELATO DE UMA ...
2.22 session 23 einheit 5
Fd1 Class Introduction
Pest Awareness for You
Roderik van Zeist
Biology Chapter 28 Section 2[1]
[HAFT] 행복나무 프로젝트 - 행복되감기
Verona prof dev presentation singleton elad 540
Curzon, generosity in startup land -it is not just about doing good, phx star...
MEMÓRIAS DAS MINHAS PRIMEIRAS EXPERIÊNCIAS COMO DOCENTE DE MÚSICA NO ENSINO R...
Book - Thesis-draft 05- FINAL 2015-12-10
Доброта
John Kauffman Resume 2016 (1)
Ad

Similar to tutorial21 (20)

PDF
Automated and Effective Testing of Web Services for XML Injection Attacks
PDF
tutorial20
PDF
tutorial20
PDF
The Ring programming language version 1.3 book - Part 10 of 88
PDF
The Ring programming language version 1.5.4 book - Part 18 of 185
PPTX
Lab 4 -Linux Files, Directories and Basic Commands Part-2.pptx
PPTX
Chapter 3 Using Unix Commands
PDF
Uses of tmux explained
PPT
Eff Plsql
PDF
tmux - An overview of the features of this powerful terminal multiplexer.
PDF
The Ring programming language version 1.5.3 book - Part 18 of 184
PDF
XML For PHP Developers
PDF
The Ring programming language version 1.9 book - Part 7 of 210
DOC
Linux Shortcuts and Commands:
PPT
Bioinformatica 27-10-2011-p4-files
TXT
PPTX
Simple xml in .net
PPTX
Introduction to Command Line & Batch files
PPT
CrashCourse: XML technologies
DOCX
Ecs 10 programming assignment 4 loopapalooza
Automated and Effective Testing of Web Services for XML Injection Attacks
tutorial20
tutorial20
The Ring programming language version 1.3 book - Part 10 of 88
The Ring programming language version 1.5.4 book - Part 18 of 185
Lab 4 -Linux Files, Directories and Basic Commands Part-2.pptx
Chapter 3 Using Unix Commands
Uses of tmux explained
Eff Plsql
tmux - An overview of the features of this powerful terminal multiplexer.
The Ring programming language version 1.5.3 book - Part 18 of 184
XML For PHP Developers
The Ring programming language version 1.9 book - Part 7 of 210
Linux Shortcuts and Commands:
Bioinformatica 27-10-2011-p4-files
Simple xml in .net
Introduction to Command Line & Batch files
CrashCourse: XML technologies
Ecs 10 programming assignment 4 loopapalooza
Ad

More from tutorialsruby (20)

PDF
&lt;img src="../i/r_14.png" />
PDF
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
PDF
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
PDF
&lt;img src="../i/r_14.png" />
PDF
&lt;img src="../i/r_14.png" />
PDF
Standardization and Knowledge Transfer – INS0
PDF
xhtml_basics
PDF
xhtml_basics
PDF
xhtml-documentation
PDF
xhtml-documentation
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
PDF
HowTo_CSS
PDF
HowTo_CSS
PDF
BloggingWithStyle_2008
PDF
BloggingWithStyle_2008
PDF
cascadingstylesheets
PDF
cascadingstylesheets
&lt;img src="../i/r_14.png" />
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
Standardization and Knowledge Transfer – INS0
xhtml_basics
xhtml_basics
xhtml-documentation
xhtml-documentation
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
HowTo_CSS
HowTo_CSS
BloggingWithStyle_2008
BloggingWithStyle_2008
cascadingstylesheets
cascadingstylesheets

Recently uploaded (20)

PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
2018-HIPAA-Renewal-Training for executives
DOCX
search engine optimization ppt fir known well about this
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Five Habits of High-Impact Board Members
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Abstractive summarization using multilingual text-to-text transfer transforme...
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Enhancing emotion recognition model for a student engagement use case through...
2018-HIPAA-Renewal-Training for executives
search engine optimization ppt fir known well about this
1 - Historical Antecedents, Social Consideration.pdf
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
sbt 2.0: go big (Scala Days 2025 edition)
A proposed approach for plagiarism detection in Myanmar Unicode text
The influence of sentiment analysis in enhancing early warning system model f...
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Convolutional neural network based encoder-decoder for efficient real-time ob...
OpenACC and Open Hackathons Monthly Highlights July 2025
Module 1.ppt Iot fundamentals and Architecture
Zenith AI: Advanced Artificial Intelligence
Five Habits of High-Impact Board Members
Hindi spoken digit analysis for native and non-native speakers
Abstractive summarization using multilingual text-to-text transfer transforme...

tutorial21

  • 1. VoiceXML Adventure Game Jonathan Eisenzopf Copyright © 2001 by internet.com March 8, 2001 Continuing from Part I [http://webref.com/perl/tutorial/20] of our comprehensive series of VoiceXML tutorials, in Part II we will develop a simple, but fully dynamic VoiceXML application in Perl for the Tellme.com voice portal. Overview "You are in an open field west of a big white house with a boarded front door. There is a small mailbox here." Do you remember the original Zork text adventure game and Choose Your Own Ad- venture books? Maybe you're even old enough to remember the original Adventure game. If you don't, you're missing out. We're going to develop a simple Zork-like ap- plication using VoiceXML. In Part III, we will create a more advanced version that uti- lizes its own XML format for building text adventures. Examples As we step through each code example, you will also be able to dial into Tellme and demo the code live on the Mother of Perl Tellme extension. Simply dial 1-800-tellme, wait for the Tellme main menu, then dial 1-73759. You will hear a greeting and a menu of options. Dial 2 for Tutorial 21 and then press the number that corresponds with the example in the tutorial. For example, when you see: Example 1. you can interact with the source code by pressing 1 on your phone. Each example is also linked to the XML source code file, where you can examine the file in your Web browser, or launch it directly into your favorite XML editor. In addition to the live examples, you will find the source code for all examples in the tutorial21.zip [tutorial21.zip] file, which you can use for free to create your own VoiceXML applications. Creating a Grammar for DTMF Input Grammars 1
  • 2. Before we proceed any further, we need to talk a bit more about grammars. They are a very important part of any VoiceXML document. A grammar is a set of expressions that define what DTMF tones and spoken input will be recognized by the system. These grammars are limited to what you program them to recognize. Depending on where you place the grammar, the scope can be the whole document, a form, or a field within a form. The Tellme grammar syntax uses the Nuance Grammar Specification Language (GSL). It's not too difficult to define a simple grammar that allows a user to make a selection via DTMF tones. Let's define a simple grammar that provides our users with the option to open one of three doors in our adventure. <grammar><![CDATA[ [ [dtmf-1] {<option "door1">} [dtmf-2] {<option "door2">} [dtmf-3] {<option "door3">} ] ]]> </grammar> First, grammars are placed within the grammar element. You should also surround the grammar with a CDATA section, which tells the XML parser not to process the text in- side. In the grammar above, we have defined three options. Each option is defined by a grammar statement. The statement consists of an expression to be matched, and the corresponding value to return when a match occurs. The expression used to match a digit is: dtmf-$ where $ is the number we want to match. For example, dtmf-1 would be matched if a user presses 1 on their telephone keypad. The second part of the expression defines the value that will be returned when we get a match. For example, {<option "foo">} will return the text foo when the user in- put matches the first part of the expression. Next we're going to apply this simple grammar to a real VoiceXML document. Getting DTMF Input The first version of our adventure game below will only accept DTMF input. The user can select 1, 2, or 3, which corresponds with one of the three doors. An example user session follows: 2
  • 3. Tellme: You are in a small room with three doors. Tellme: To open the first door, press 1. Tellme: To open the second door, press 2. Tellme: To open the third door, press 3. User: (pressed 1) Tellme: You see a large hungry monkey. Now, let's take a look at the VoiceXML document. Example 1. [example1.xml] <?xml version="1.0"?> <vxml version="1.0"> <form> <field name="answer"> <grammar><![CDATA[ [ [dtmf-1] {<option "door1">} [dtmf-2] {<option "door2">} [dtmf-3] {<option "door3">} ] ]]> </grammar> <prompt><audio>You are in a small room with three doors.</audio> <pause>300</pause> <audio>To open the first door, press 1.</audio> <pause>300</pause> <audio>To open the second door, press 2.</audio> <pause>300</pause> <audio>To open the third door, press 3.</audio> </prompt> </field> <filled> <result name="door1"> <audio>You see a large hungry monkey.</audio> <reprompt/> </result> <result name="door2"> <audio>You see another room with three doors, a man, and his monkey.</audio> <reprompt/> </result> <result name="door3"> 3
  • 4. <audio>You see a man scratching his monkey.</audio> <reprompt/> </result> </filled> </form> </vxml> For more information on using the filled and result elements, please see Page 3 [http://webref.com/perl/tutorial/20/3.html] of the first part of this tutorial. You might have also noticed the pause element in the example above. Using the element forces Tellme.com to pause between prompts. In this case, we want to stop for 300 millisec- onds between options to duplicate the pause that would occur in natural speech. Adding Voice Selections Now that we have a simple interactive application working, we need to add the ability for users to provide voice commands in addition to DTMF input. This is done by adding additional grammar expressions. <grammar><![CDATA[ [ [dtmf-1 one] {<option "door1">} [dtmf-2 two] {<option "door2">} [dtmf-3 three] {<option "door3">} [(touch the monkey)] {<option "monkey">} ] ]]> </grammar> If you compare the new grammar element above to the one on the previous page, you'll notice that we've added addition information for each option as well as a new line. The addition information consists of a single word for each option. For example, the first line of the grammar instructs the Tellme VoiceXML parser to match when the user presses 1 on their keypad or if they speak the word one. The last line of the grammar matches on the phrase, "touch the monkey." There will not be a match if the user says "touch" or "monkey." It will only match when the user says "touch the monkey." This is because the phrase is contained within paretheses, which require that the whole phrase be matched. Example 2. [example2.xml] <?xml version="1.0"?> <vxml version="1.0"> <form> 4
  • 5. <field name="answer"> <grammar> <![CDATA[ [ [dtmf-1 one] {<option "door1">} [dtmf-2 two] {<option "door2">} [dtmf-3 three] {<option "door3">} [(touch the monkey)] {<option "monkey">} ] ]]> </grammar> <prompt> <audio>You are in a small room with thr doors.</audio> <pause>300</pause> <audio>To open the first do 1.</audio> <pause>300</pause> <audio>To open the second d 2.</audio> <pause>300</pause> <audio>To open the third do 3.</audio> </prompt> </field> <filled> <result name="door1"> <audio>You see a large hungry monkey.</audio> <reprompt/> </result> <result name="door2"> <audio>You see another room with three doors, monkey.</audio> <reprompt/> </result> <result name="door3"> <audio>You see a man scratching his monkey.</audio> <reprompt/> </result> <result name="monkey"> <audio>No! Do not touch the monkey!</audio> <reprompt/> </result> </filled> </form> </vxml> Handling Events Next, we need to add handlers for the nomatch and noinput events using the methods 5
  • 6. specified in Page 4 [http://www.webref.com/perl/tutorial/20/4.html] of the previous tu- torial. Example 3. [example3.xml] <?xml version="1.0"?> <vxml version="1.0"> <form> <field name="answer"> <grammar> <![CDATA[ [ [dtmf-1 one] {<option "door1">} [dtmf-2 two] {<option "door2">} [dtmf-3 three] {<option "door3">} [(touch the monkey)] {<option "monkey">} ] ]]> </grammar> <prompt> <audio>You are in a small room with thr doors.</audio> <pause>300</pause> <audio>To open the first door, press or say 1.</audio> <pause>300</pause> <audio>To open the second door, press or say 2.</audio> <pause>300</pause> <audio>To open the third door, press or say 3.</audio> </prompt> <nomatch> <audio>You fool! That is not a choice I am giv you.</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </nomatch> <noinput> <audio>What are you waiting for?</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </noinput> </field> <filled> <result name="door1"> <audio>You see a large hungry 6
  • 7. </audio> <reprompt/> </result> <result name="door2"> <audio>You see another room with three doors, monkey.</audio> <reprompt/> </result> <result name="door3"> <audio>You see a man scratching his monkey.</audio> <reprompt/> </result> <result name="monkey"> <audio>No! Do not touch the monkey!</audio> <reprompt/> </result> </filled> </form> </vxml> Adding Help In most VoiceXML applications, you'll want to provide some help facility. Most peo- ple expect to get some kind of help when they press 0 on their phone, whether it be a live operator or automated assistance. In the example below, we've added a DTMF and voice grammar item that calls the help element in the document below. Example 4. [example4.xml] <?xml version="1.0"?> <vxml version="1.0"> <form> <field name="answer"> <grammar> <![CDATA[ [ [dtmf-1 one first] {<option "door1">} [dtmf-2 two second] {<option "door2">} [dtmf-3 three third] {<option "door3">} [(touch the monkey)] {<option "monkey">} [dtmf-0 huh help what doh] {<option "help">} ] ]]> </grammar> <prompt> <audio>You are in a small room with three 7
  • 8. </audio> <pause>300</pause> <audio>Which one do you want to open?</audio> </prompt> <nomatch count="1"> <audio>Wrong door.</audio> <audio>To open the first door, press or say 1.</audio> <pause>300</pause> <audio>To open the second door, press or say 2.</audio> <pause>300</pause> <audio>To open the third door, press or say 3.</audio> <listen/> </nomatch> <nomatch count="2"> <audio>You fool! That is not a choice I am giving you.</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </nomatch> <nomatch count="3"> <audio>Pretend you have a brain and press 1, 2, or 3.</audio> <listen/> </nomatch> <noinput count="1"> <audio>What are you waiting for?</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </noinput> <noinput count="2"> <audio>If you do not press 1, 2, or 3, I am going to kill the monkey!</audio> <listen/> </noinput> <noinput count="3"> <audio>Ok. That's it. The monkey is dead and it's your fault because you did not choose 1, 2, or 3.</audio> <listen/> </noinput> <noinput count="4"> <audio>1, 2, or 3.</audio> <listen/> 8
  • 9. </noinput> <help> <audio>Why are you asking for help?</audio> <pause>300</pause> <audio>Go away.</audio> <reprompt/> </help> </field> <filled> <result name="door1"> <audio>You see a large hungry monkey.</audio> <reprompt/> </result> <result name="door2"> <audio>You see another room with three doors, a man, and his monkey.</audio> <reprompt/> </result> <result name="door3"> <audio>You see a man scratching his monkey.</audio> <reprompt/> </result> <result name="monkey"> <audio>No! Do not touch the monkey!</audio> <reprompt/> </result> </filled> </form> </vxml> If you hadn't noticed, I also added multiple elements for the nomatch and noinput han- dlers. Making Our Adventure Dynamic Now that we have a working VoiceXML document that provides some dynamic capa- bilities, it's time to tie in into a back-end Perl script that provides true dynamic capabil- ities. For this first run of our dynamic Perl script, we'll develop a script that dynami- cally generates rooms. Each room will always have three doors. Because the script is random, there are many different possible combinations. Initially, users won't be able to interact with any of the objects in the rooms, they'll only be able to open doors. In Part III, we'll add this capability. 9
  • 10. room.pl use CGI; use strict; my @rooms = ( 'large ball room', 'pool hall', 'large red barn', 'seven eleven', 'bedroom', 'bathroom', 'funeral parlor', 'windy room with bark walls', 'dimly lit tunnel', 'prison cell', 'hospital waiting room', 'underground parking garage', 'circular stairwell', 'arboretum', 'janitors closet', 'closet', 'subway car', 'cubicle', 'lighthouse tower', 'graveyard' ); my @objects = ( 'full of clowns dressed in red', 'full of smoke but no people', 'with a dead horse on the floor', 'with a broken slurpy machine. In the corner, a troll 'with black curtains and something under the covers', 'with padded walls', 'with wood bark walls', 'with flickering florescent lights', 'with an oil spotted shag carpet', 'full of mad raving lunatics', 'with a man and his monkey', 'full of monkeys', 'with William Shatner singing tambourine man', 'with a large hungry monkey', 'full of drunken sailors', 'full of blind dwarves', 'full of Windows programmers', 'with a man scratching his monkey' ); my $q = new CGI; print "Content-Type: text/xmlnn"; 10
  • 11. &prompt; sub prompt { my $room = $rooms[int(rand(scalar(@rooms)))]; my $object = $objects[int(rand(scalar(@objects)))]; print <<VXML; <?xml version="1.0"?> <vxml version="1.0"> <form> <field name="answer"> <grammar> <![CDATA[ [ [dtmf-1 one first] {<option "door1">} [dtmf-2 two second] {<option "door2">} [dtmf-3 three third] {<option "door3">} [(touch the monkey)] {<option "monkey">} [dtmf-0 huh help what doh] {<option "help">} ] ]]&gt; </grammar> <prompt> <audio>You are in a $room $object. There are three doors.</audio> </prompt> <filled> <submit next="http://www.textant.com/cgi-bin/room.pl"/> </filled> <nomatch count="1"> <audio>Wrong door.</audio> <audio>To open the first door, press or say 1.</audio> <pause>300</pause> <audio>To open the second door, press or say 2.</audio> <pause>300</pause> <audio>To open the third door, press or say 3.</audio> <listen/> </nomatch> <nomatch count="2"> <audio>You fool! That is not a choice I am giving you.</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </nomatch> <nomatch count="3"> <audio>Pretend you have a brain and press 1, 2, or 11
  • 12. </audio> <listen/> </nomatch> <noinput count="1"> <audio>You need to select a door.</audio> <pause>300</pause> <audio>Press or say 1, 2, or 3.</audio> <listen/> </noinput> <noinput count="2"> <audio>If you do not press 1, 2, or 3, I am going to kill the monkey!</audio> <listen/> </noinput> <noinput count="3"> <audio>Ok. That's it. The monkey is dead and it's your fault because you did not choose 1, 2, or 3.</audio> <listen/> </noinput> <noinput count="4"> <audio>1, 2, or 3.</audio> <listen/> </noinput> <help> <audio>Why are you asking for help?</audio> <pause>300</pause> <audio>Go away.</audio> <reprompt/> </help> </field> </form> </vxml> VXML } Conclusion I hope this column has provided a bit of nostalgia for some of you who grew up play- ing paper and computer adventure games like D&D, Battletech, Adventure, Zork, Leisure Suit Larry, Loom, and Space Quest. Alas, the era of adventure games seems to have passed. No longer are games with extensive plots that really draw users into an al- ternate universe. Games like BloodNet and Ultima brought a short resurgence, but I'm afraid we may not see another for a while. Shootemups like Quake and Half Life have all but replaced the adventure games. 12
  • 13. What's even more depressing is seeing some of the great gaming companies like FASA, Micropose, Spectrum Holobyte, and Infocom go out of business or get bought out by larger companies who really don't care too much about making great games. Well, that's ok, because in Part III, we're going to develop our own text adventure plat- form in an XML format that will work with HTML, WAP, and VoiceXML. We'll let our nostalgia run its course and then perhaps we'll look at utilizing this technology for something truly useful. But right now, it's time to play. We'll see you in Part III. Until then, I'm going to go back through those sweet memories of yesteryear. 13