Step 10: Remove items with voice¶
We can also allow users to remove items from the cart with voice.
To the dialog script, add a new voice command. This command is similar to the command for adding items, except for sending a negative number with the
updateOrder
command.Dialog script¶intent(`Remove (a|an|) $(ITEM ${itemList}), (please|)`, `Remove $(NUMBER) $(ITEM ${itemList}), (please|)`, p => { p.play(`Removing ${p.ITEM.value}`, 'No problem', 'Your order is updated'); let number = p.NUMBER ? p.NUMBER.number : 1; p.play({command: 'updateOrder', item: p.ITEM.label, quantity: -number}); });
In the app, update the
changeOrder()
function to the following:HTML file¶function changeOrder(item, quantity) { let number = (order[item] ? order[item] : 0) + quantity; // Removing the item or updating the item quantity if (number <= 0) { delete order[item]; } else { order[item] = number; } updateCart(); }
Now, when the number of items is less or equal to 0, the item is removed from the order; otherwise the number for the named position is updated.
In the app, click the Alan AI button and try adding and removing items from the cart with voice.