VoiceXML Developer Series: A Tour Through VoiceXML, Part IV
Wow, I feel like we've really come a long way in a month. We've had an in-depth tour of most of the VoiceXML basics. I've hope you've been learning as much as I have. In this fourth segment of our complete tour through VoiceXML, we will learn how to test user input after it has been spoken. We'll also learn how to utilize conditional statements such as if and else to control call flow. Lastly, we'll learn how to catch and handle errors and other VoiceXML events. All in this edition of the VoiceXML Developer.
Handle events with <nomatch>, <noinput>, <help>, and <error>
One important aspect of writing a robust VoiceXML application is handling events that are thrown by the VoiceXML gateway. Failing to do so may cause your application to end abruptly. There a number of events that should be handled in all VoiceXML applications that are used in production. These are help, nomatch, noinput, and error.
Event handlers can be used in fields, forms, menus, and subdialogs. The noinput event is triggered when the system doesn't receive input as expected. The nomatch event is triggered when a user gives input that doesn't match the defined grammar. The help event is triggered when the user says "help" at any point in the dialog. An error event can be triggered for many different reasons including a system failure and unavailable resources.
This example is a phone-based hot dog stand that includes handlers for all the events listed above.
An example dialog using the VoiceXML above follows:
Computer: Welcome to Mr. T's snack shop. Alright. What you want sucker? Operator: <no response> Computer: Hey, I ain't got all day punk. It's time to pick and pay before I get upset! Operator: I want a watermelon. Computer: Hey stupid! Order a hotdog, pretzel, chips, or soda punk! It's time to pick and pay before I get upset! Operator: help Computer: Help? The only help I give starts with a capital tee and ends with my fist. It's time to pick and pay before I get upset! Operator: soda Computer: So, you want a soda. Well, pay up sucker!
Event handlers are called based upon the scope in which they exist in relation to the element that threw the event. In the example above, Mr T is an impatient hot dot vendor. He will ask you for your order on line 6. Because the timeout attribute of the prompt element is set to 3, if you don't give Mr. T your order in three seconds, the prompt element will throw a noinput event. Because the <noinput> element on line 8 is in the same scope as the prompt, the VoiceXML interpreter will execute its contents, which express Mr. T's impatience with the user, and prompt the user again with <reprompt /> on line 9.
One neat feature of prompts is the ability to execute different content based upon the number of times the user has been prompted. This is accomplished by setting the count attribute to the instance number that the prompt should be played. So the first prompt that's played is, "Alright. What you want sucker?" The next time that the prompt is called, the user would hear, "It's time to pick and pay before I get upset!" This second prompt would get played in the case where a user does not provide input within three seconds and the noinput event is called.
The count attribute can also be used in <nomatch> and <noinput> elements to make the dialog more natural by providing different feedback if the user makes the same error more than once. Subsequent nomatch or noinput events may indicate that the user is having problems understanding what they're supposed to say. Therefore, the count attribute is a way to provide the user with more detailed instructions the second time they (or the VoiceXML interpreter) make a mistake.
Speaking of nomatch, this event is thrown when a user provides spoken or DTMF input that doesn't match a grammar. Line 14 contains the <nomatch> element for our dialog.
Another important event handler is <help>. Help is a special keyword in VoiceXML that, when spoken by a user, triggers the help event. The <help> element catches this event on line 24. Since users may not be aware of this feature, you may want to include it in your prompts. For example, "Please say your name and social security number or say help for instructions".
Lastly, when the VoiceXML interpreter encounters a fatal error, it throws the error event, which can be caught with the <error> element on line 29. You may want to let the user know that an error has occurred and either return them to a previous menu or exit the application. For example, "An error has occurred while processing your request. Please hold while I transfer you to a customer service representative."
Page 1 of 2