Introduction
In this article, I am going to demonstrate how to define validation rules for slots and how to define prompts to warn users if a user provides an unacceptable value for any slot this way Alexa can validate user response. Slot Validation is one of the components required to create a dialog model for an Alexa skill. Dialog models are created to simplify the Alexa skills code to collect and confirm slot values. The dialog model identifies the prompts and utterances to collect, validate, and confirms the slot values and intents.
Components of a Dialog Model
There are four components to the dialog model:
Required slots and prompts
A required slot represents information that your skill must have in order to fulfill the user's request. For instance, the RegisterBirthday intent requires, at a minimum, the day (day), month (month), and year (year) to save birthday details for the future. You can control the order in which Alexa asks for the slot values.
Slot confirmation prompts
You can optionally designate a required slot as requiring confirmation. This means that the user must also confirm the slot value with a yes/no response before proceeding.
Intent confirmation prompts
You can specify that the entire intent requires confirmation. This is common for skills that order products or make reservations. A confirmation prompt typically repeats back to the user all the information previously provided for the user to confirm.
Slot validation rules and prompts
You can configure a set of validation rules that the slot value provided by the user must pass to be considered valid. If the value fails validation, Alexa can use your prompts to ask for a corrected value. You can use slot validation rules with both required and non-required slots.
Creating Custom Alexa Skill
A custom Alexa skill named birthday time has been created. The skill contains an intent name RegisterBirthday intent having a day, month, and year as different types of slot values.
Day slot of RegisterBirthday intent
Define slot validation rules
Slot validation lets you create validation rules for your slot values. Alexa can then check the user's response against these rules and prompt the user if the user provides an unacceptable value.
There are several possible rules for the different types of slots.
To create slot validation rules:
- In the left-hand navigation, find the intent and its list of slots, then click the slot name. Alternatively, from the page for a particular intent, find the slot in the Intent Slots section below the Sample Utterances and click the Edit Dialog link.
- Under the slot type field, select Validations.
- Under Create Validation Rule, select the rule you want to use and click the plus.
- The list shows just the validators that are relevant for the slot's type.
- Fill in any additional fields specific to the rule, such as the value to compare to. This depends on the rule.
- Below the rule, enter at least one prompt that Alexa should say to ask the user for a new value.
- You can include the slot name in curly brackets ({ }) so that Alexa speaks back the value that failed validation.
- Be sure to save and build the interaction model.
Below, I have defined validation rules for one of the slots of RegisterBirthday intent that is day slot.
Creating a validation rule
Validation rules are checked when a slot value is spoken by a user. To create a validation rule, we need to choose a validator first. There are two validators available that accept only a set of values and reject only a set of values.
We can choose either of the two options depending on what is required in the skill.
For the day slot, I have chosen “accept only a set of values” validator. I have also added values that are acceptable under this validation rule. These values are all the days of the month starting from 1 to 31.
Alexa will prompt for a new value. If the user provides unacceptable values which are not within the specified set, Alexa can then check the user's response against these rules and prompt the user if the user provides an unacceptable value. In this case, Alexa can prompt “Please say a valid day”.
Json code for defining slot validation is as follows:
- "intents": [
- {
- "name": "RegisterBirthdayIntent",
- "confirmationRequired": true,
- "prompts": {
- "confirmation": "Confirm.Intent.746177723927"
- },
- "slots": [
- {
- "name": "day",
- "type": "AMAZON.Ordinal",
- "confirmationRequired": true,
- "elicitationRequired": true,
- "prompts": {
- "confirmation": "Confirm.Slot.1170703155793",
- "elicitation": "Elicit.Slot.653974572406"
- },
- "validations": [
- {
- "type": "isInSet",
- "prompt":
- "Slot.Validation.190694681136.1424851421300",
- "values": [
- "22",
- "23",
- "24",
- "25",
- "26",
- "27",
- "28",
- "29",
- "30",
- "31",
- "10",
- "11",
- "12",
- "13",
- "14",
- "15",
- "16",
- "17",
- "18",
- "19",
- "1",
- "2",
- "3",
- "4",
- "5",
- "6",
- "7",
- "8",
- "9",
- "20",
- "21"
- ]
- }
- ]
- },
- {
- "name": "month",
- "type": "MonthType",
- "confirmationRequired": true,
- "elicitationRequired": true,
- "prompts": {
- "confirmation": "Confirm.Slot.1170703155793",
- "elicitation": "Elicit.Slot.282358190483.487594834711"
- },
- "validations": [
- {
- "type": "hasEntityResolutionMatch",
- "prompt":
- "Slot.Validation.190694681136.463018349705"
- }
- ]
- },
- {
- "name": "year",
- "type": "AMAZON.FOUR_DIGIT_NUMBER",
- "confirmationRequired": true,
- "elicitationRequired": true,
- "prompts": {
- "confirmation":
- "Confirm.Slot.1170703155793.576520739409",
- "elicitation":
- "Elicit.Slot.1253554922892.1358447173887"
- },
- "validations": [
- {
- "type": "isGreaterThan",
- "prompt":
- "Slot.Validation.1253554922892.1358447173887",
- "value": "1900"
- },
- {
- "type": "isLessThan",
- "prompt":
- "Slot.Validation.1253554922892.1358447173887",
- "value": "2018"
- }
- ]
- }
- ]
- }
- ]
Output
From the output, we can see that after invoking the skill, Alexa asks for the date of birth. If the user provides an unacceptable value, then Alexa will prompt a message saying “Please say a valid day” to ensure the user provides a valid day of the month.
Summary
In this article, I have demonstrated how to define validation rules for slots and how to define prompts to warn users if a user provides an unacceptable value for any slot this way Alexa can validate user response. I have also explained one of the components of the dialog model. Proper examples of Alexa speech prompts and user utterances are provided along with the JSON code snippet of the interaction model.