Step 6: Pick out important data with slots¶
In our app, users can order different items: pepperoni, burritos, burgers and so on. To capture the user’s choice, we will add a user-defined slot to the voice command. Slots allow Alan AI to identify and retrieve the necessary information from the user’s input.
To define a slot, enter
$
followed by brackets. In brackets, provide the slot name followed by a colon and a list of values separated with,
.intent('(I want|I will take|Add) (a|an)- $(ITEM: pepperoni, margherita, burrito, burger, taco, apple pie...), -please-', p => { p.play('Adding pepperoni for you'); });
Now, users can order different items.
For our convenience, we can save the list of slots to a variable and use it in the command pattern. If adding a variable, don’t forget to replace single quotes in the command pattern with backticks to let Alan AI capture the nested expression:
const itemList = "pepperoni, margherita, burrito, burger, taco, apple pie"; intent(`(I want|I will take|Add) (a|an)- $(ITEM: ${itemList}), please-`, p => { p.play('Adding pepperoni for you'); });
Finally, let’s update Alan AI’s response: it should name the chosen item. The value captured with the
ITEM
slot is stored to thep.ITEM.value
variable. Again, replace single quotes with backticks to play the nested variable:const itemList = "pepperoni, margherita, burrito, burger, taco, apple pie"; intent(`(I want|I will take|Add) (a|an)- $(ITEM: ${itemList}), please-`, p => { p.play(`Adding ${p.ITEM.value} for you`); });