Step 13: Get the delivery time

In addition to the user’s address, we need to know the delivery time. To get the time, we will use one of Alan’s built-in JavaScript librariesmoment-timezone.

  1. To the voice script, add the getTime context with a command to capture the delivery time:

    let getTime = context(() => {
        intent('$(TIME)', '$(T now|asap|right now|as soon as possible)', p => {
            let time, date;
            // Delivering in 30 minutes
            if (p.T) {
                time = api.moment().tz(p.timeZone).add(30, 'minutes').format("h:mm a");
            }
            // Delivering at the exact time
            if (p.TIME) {
                time = p.TIME.value;
            }
            p.play(`We will deliver your order at ${time}`);
        });
    });
    

    Note

    Notice api before moment(): this allows accessing the built-in timezone library.

  2. If you have several contexts in the voice script, it is recommended that you activate them one from another one. Update the intent in the checkout context: activate the getTime context from it and add another p.play() function to prompt the user to set the delivery time:

     let checkout = context(() => {
         intent('(My address is|It is) $(LOC)', p => {
             p.play(`Your order will be delivered to ${p.LOC.value}`);
             // Prompting for setting the delivery time
             p.play('What is the expected delivery time?');
             // Activating the getTime context
             p.then(getTime);
         });
     });
    
     let getTime = context(() => {
         intent('$(TIME)', '$(T now|asap|right now|as soon as possible)', p => {
             let time, date;
             if (p.T) {
                 time = api.moment().tz(p.timeZone).add(30, 'minutes').format("h:mm a");
             }
             if (p.TIME) {
                 time = p.TIME.value;
             }
             p.play(`We will deliver your order at ${time}`);
         });
     });
    

    Web page source | Voice script

Here is how the intent for getting the time works:

  • If the user asks to deliver the order now or as soon as possible, Alan gets the current time, adds 30 minutes and plays a confirmation with the delivery time to the user.

  • If the user provides the exact time, the confirmation with this exact time is played back.

Try the dialog in the Debugging Chat or in the app and make sure you can provide the delivery details: address and time.