VoiceXML Developer Series: A Tour Through VoiceXML, Part IV
Processing input with <filled>
Once we have received input that matches the field or form level grammar(s), it's time to do something with it. This might include validating a credit card number, looking up information based upon a customer name, or submitting the information to a back-end script. The <filled> element can exist within a field or form scope. That is, if the element exists inside a <field> element, it will be called when that field has been filled with a value. If it exists inside the <form> element, but outside the <field> elements, it will be called once ALL form fields have been filled. So basically, you can check and validate information each time a field is filled, all at once when all fields have been filled, or both. In this example on line 18, we simply play a prompt once the user has made a selection. In the next example, on lines 34 and 44, we're actually validating the input with conditional statements.
You can view the main example for this edition of VoiceXML Developer here. To try this example, call VoiceXML Planet at 510-315-6666; press 1 to listen to the demos, then press 3 to hear this example. This example simulates an E-Commerce application for Frank's Flute Factory. Frank would like to reduce the costs of his call center for repeat customers by having them order via an automated system rather than having to talk to a sales representative. He feels this will reduce his costs and enable his customers to place orders faster. The example is a prototype application for his company. The example utilizes the examples we've discussed previously and introduces a few new elements that we haven't seen yet. These are: <option>, <if> and <else>, <throw> and <catch>, and <subdialog>. These new elements will be discussed below.
Use <option> for selection lists within a <form>
Processing Input with conditional statements
VoiceXML contains three conditional statements that work together to provide more control over dialog flow. These are <if>, <else>, and <elseif>. We can use any variable or field value in the current dialog or in the root document in these conditional statements. The <if> element on line 36, which is executed after the user has selected a flute, checks the type of flute that the user selected. If the flute is not cedar or maple, the outOfStock event is thrown with the <throw> element. We can create our own custom events and handlers through this mechanism. On line 54, the <catch> element, whose event attribute is set to outOfStock, catches the event and tells the customer that oak flutes are temporarily out of stock, and allows the user to select another type of flute with the <goto> element on line 56.
If the user did select a cedar or maple flute, we assign a prompt string on line 37 for the confirmPrompt variable, which was declared on line 2, and is passed to a subdialog on line 43.
Componentizing VoiceXML with <subdialog>
Once the user has provided a valid flute type, a <subdialog> is called, which asks the user to confirm that they want to purchase the flute type that the system recognized. The subdialog is called on line 43 and contains several attributes. The src attribute specifies the file that contains the VoiceXML for the subdialog. The name attribute contains the variable name for the subdialog. Values that are returned from the subdialog will be accessed using this value. The enctype attribute identifies the encoding type that will be used for the HTTP request. This will typically be set to application/x-www-form-urlencoded. The method attribute defines the HTTP method for submitting the data. You will typically use either the get or post methods for this attribute. On line 44, we pass the confirmPrompt variable that was assigned previously using the <parameter> element. The name attribute defines the name of the variable as it will be accessed in the subdialog. The contents of the subdialog, confirm.vxml are listed here.
The userPrompt variable is declared on line 2. This subdialog asks the user to confirm that they want to purchase the flute by prompting the user to press 1 for yes or 2 for no. We utilize the boolean internal grammar for this purpose, and return the results back with the <return> element on line 14.
Back in the main example, once the subroutine has been called, we test the value of the returned value. Because the type was boolean, the value will be either true or false. If the value is true on line 46, we thank the user for their order. If the user pressed 2, we cancel their order and prompt them to select another flute on line 48 inside the <else>.
In this edition of the VoiceXML Developer, we've seen some real examples of VoiceXML in action. We're also getting close to finishing up our tour. We've learned about almost all of the VoiceXML elements! Join us next time at the VoiceXML Developer, where we'll dive into the world of voice recognition grammars, the most powerful (and complicated) aspect of VoiceXML. See you next time.
About Jonathan Eisenzopf
Jonathan is a member of the Ferrum Group, LLC based in Reston, Virginia that specializes in Voice Web consulting and training. He has also written articles for other online and print publications including WebReference.com and WDVL.com. Feel free to send an email to firstname.lastname@example.org regarding questions or comments about the VoiceXML Developer series, or for more information about training and consulting services.
Page 2 of 2