Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async dateTimePromptValidator(promptContext) {
if (promptContext.recognized.succeeded) {
// This value will be a TIMEX. And we are only interested in a Date so grab the first result and drop the Time part.
// TIMEX is a format that represents DateTime expressions that include some ambiguity. e.g. missing a Year.
const timex = promptContext.recognized.value[0].timex.split('T')[0];
// If this is a definite Date including year, month and day we are good otherwise reprompt.
// A better solution might be to let the user know what part is actually missing.
return new TimexProperty(timex).types.has('definite');
}
return false;
}
}
const promptMessageText = 'On what date would you like to travel?';
const promptMessage = MessageFactory.text(promptMessageText, promptMessageText, InputHints.ExpectingInput);
const repromptMessageText = 'I\'m sorry, for best results, please enter your travel date including the month, day and year.';
const repromptMessage = MessageFactory.text(repromptMessageText, repromptMessageText, InputHints.ExpectingInput);
if (!timex) {
// We were not given any date at all so prompt the user.
return await stepContext.prompt(DATETIME_PROMPT,
{
prompt: promptMessage,
retryPrompt: repromptMessage
});
}
// We have a Date we just need to check it is unambiguous.
const timexProperty = new TimexProperty(timex);
if (!timexProperty.types.has('definite')) {
// This is essentially a "reprompt" of the data we were given up front.
return await stepContext.prompt(DATETIME_PROMPT, { prompt: repromptMessage });
}
return await stepContext.next([{ timex }]);
}
returnResult.newReservation.time = ((parseInt(parsedTimex.hour) < 10) ? '0' + parsedTimex.hour : parsedTimex.hour);
returnResult.newReservation.time += ':';
returnResult.newReservation.time += ((parseInt(parsedTimex.minute) < 10) ? '0' + parsedTimex.minute : parsedTimex.minute);
returnResult.newReservation.time += ':';
returnResult.newReservation.time += ((parseInt(parsedTimex.second) < 10) ? '0' + parsedTimex.second : parsedTimex.second);
if (!validtime || (validtime.length === 0)) {
// Validation failed!
returnResult.outcome.push(new ReservationOutcome(`Sorry, that time does not work. I can only make reservations that are in the daytime (6AM - 6PM)`, DATE_TIME_ENTITY));
returnResult.newReservation.time = '';
returnResult.status = reservationStatus.INCOMPLETE;
}
}
// Get date time LG string if we have both date and time
if (returnResult.newReservation.date !== '' && returnResult.newReservation.time !== '') {
returnResult.newReservation.dateTimeLGString = new TimexProperty(returnResult.newReservation.date + 'T' + returnResult.newReservation.time).toNaturalLanguage(today);
}
}
}
// Take the first found value.
if (locationEntity !== undefined) {
let cafeLocation = locationEntity.entityValue[0][0];
// Capitalize cafe location.
returnResult.newReservation.location = cafeLocation.charAt(0).toUpperCase() + cafeLocation.substr(1);
}
// Accept confirmation entity if available only if we have a complete reservation
if (confirmationEntity !== undefined) {
if (confirmationEntity.entityValue[0][0] === 'yes') {
returnResult.newReservation.reservationConfirmed = true;
returnResult.newReservation.needsChange = undefined;
private async initialStep(stepContext: WaterfallStepContext): Promise {
const timex = (stepContext.options as any).date;
const promptMsg = 'On what date would you like to travel?';
const repromptMsg = 'I\'m sorry, for best results, please enter your travel date including the month, day and year.';
if (!timex) {
// We were not given any date at all so prompt the user.
return await stepContext.prompt(DATETIME_PROMPT,
{
prompt: promptMsg,
retryPrompt: repromptMsg,
});
} else {
// We have a Date we just need to check it is unambiguous.
const timexProperty = new TimexProperty(timex);
if (!timexProperty.types.has('definite')) {
// This is essentially a "reprompt" of the data we were given up front.
return await stepContext.prompt(DATETIME_PROMPT, { prompt: repromptMsg });
} else {
return await stepContext.next({ timex });
}
}
}
async finalStep(stepContext) {
// If the child dialog ("bookingDialog") was cancelled or the user failed to confirm, the Result here will be null.
if (stepContext.result) {
const result = stepContext.result;
// Now we have all the booking details.
// This is where calls to the booking AOU service or database would go.
// If the call to the booking service was successful tell the user.
const timeProperty = new TimexProperty(result.travelDate);
const travelDateMsg = timeProperty.toNaturalLanguage(new Date(Date.now()));
const msg = `I have you booked to ${ result.destination } from ${ result.origin } on ${ travelDateMsg }.`;
await stepContext.context.sendActivity(msg);
} else {
await stepContext.context.sendActivity('Thank you.');
}
return await stepContext.endDialog();
}
}
module.exports.examples = () => {
describe(new TimexProperty('2017-05-29'));
describe(new TimexProperty('XXXX-WXX-6'));
describe(new TimexProperty('XXXX-WXX-6T16'));
describe(new TimexProperty('T12'));
};
isAmbiguous(timex) {
const timexPropery = new TimexProperty(timex);
return !timexPropery.types.has('definite');
}
}
isAmbiguous(timex) {
const timexPropery = new TimexProperty(timex);
return !timexPropery.types.has('definite');
}
}
private isAmbiguous(timex: string): boolean {
const timexPropery = new TimexProperty(timex);
return !timexPropery.types.has('definite');
}
}