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 AI’s built-in JavaScript libraries — moment-timezone
.
To the dialog script, add the
getTime
context with a command to capture the delivery time:Dialog script¶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
beforemoment()
: this allows accessing the built-intimezone
library.If you have several contexts in the dialog script, it is recommended that you activate them one from another one. Update the intent in the
checkout
context: activate thegetTime
context from it and add anotherp.play()
function to prompt the user to set the delivery time:Dialog script¶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}`); }); });
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 AI 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.