Step 11: Add the checkout context

After users have ordered food, they can proceed to checking out and providing the delivery details. This step makes sense only after the food is in the cart. If the user provides the delivery details at the beginning of the dialog, this would be meaningless. For this reason, we will wrap this part of the dialog in a context.

In Alan AI, a context is a conversational branch, or the part of the dialog, that should become active at a specific moment of time. Let’s update our dialog script:

  1. Add a new intent for initiating checkout:

    Dialog script
    intent(`That's (all|it)`, '(Ready to)- checkout', p => {
        p.play('What is the delivery address?');
    });
    
  2. Define the checkout context. Inside the checkout context, we will place a voice command for getting the delivery address and use the ADDRESS slot to capture the location:

    Dialog script
    let checkout = context(() => {
        intent('(My address is|It is)- $(ADDRESS: 456 Maple Avenue, 123 Oak Street, 789 Pine Road...)', p => {
            p.play(`Your order will be delivered to ${p.ADDRESS.value}`);
        });
    });
    
  3. Voice commands placed in a context are inactive until the context is activated. We need to activate the context after the food is in the cart. Update the voice command for initiating checkout — add the p.then() function and pass the context name to it:

    Dialog script
    intent(`That's (all|it)`, '(Ready to)- checkout', p => {
        p.play('What is the delivery address?');
        // Activating the checkout context
        p.then(checkout);
    });
    
    let checkout = context(() => {
        intent('(My address is|It is)- $(ADDRESS: 456 Maple Avenue, 123 Oak Street, 789 Pine Road...)', p => {
            p.play(`Your order will be delivered to ${p.ADDRESS.value}`);
        });
    });
    

Now, try the dialog in the Debugging Chat:

  1. Order the food

  2. Initiate checkout and provide the delivery address