botbuilder-dialogs
Version:
A dialog stack based conversation manager for Microsoft BotBuilder.
114 lines (105 loc) • 4.34 kB
text/typescript
/**
* @module botbuilder-dialogs
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import * as Recognizers from '@microsoft/recognizers-text-date-time';
import { Activity, InputHints, TurnContext } from 'botbuilder-core';
import { Prompt, PromptOptions, PromptRecognizerResult, PromptValidator } from './prompt';
/**
* Result returned by the `DateTimePrompt`.
*/
export interface DateTimeResolution {
/**
* TIMEX expression representing ambiguity of the recognized time.
*/
timex: string;
/**
* Type of time recognized. Possible values are 'date', 'time', 'datetime', 'daterange',
* 'timerange', 'datetimerange', 'duration', or 'set'.
*/
type: string;
/**
* Value of the specified [type](#type) that's a reasonable approximation given the ambiguity
* of the [timex](#timex).
*/
value: string;
}
/**
* Prompts a user to enter a datetime expression.
*
* @remarks
* By default the prompt will return to the calling dialog a `DateTimeResolution[]`.
*/
export class DateTimePrompt extends Prompt<DateTimeResolution[]> {
/**
* The prompts default locale that should be recognized.
*/
defaultLocale: string | undefined;
/**
* Creates a new DateTimePrompt instance.
*
* @param dialogId Unique ID of the dialog within its parent `DialogSet` or `ComponentDialog`.
* @param validator (Optional) validator that will be called each time the user responds to the prompt.
* @param defaultLocale (Optional) locale to use if `TurnContext.activity.locale` is not specified. Defaults to a value of `en-us`.
*/
constructor(dialogId: string, validator?: PromptValidator<DateTimeResolution[]>, defaultLocale?: string) {
super(dialogId, validator);
this.defaultLocale = defaultLocale;
}
/**
* Prompts the user for input.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext), context for the current
* turn of conversation with the user.
* @param state Contains state for the current instance of the prompt on the dialog stack.
* @param options A [PromptOptions](xref:botbuilder-dialogs.PromptOptions) object constructed
* from the options initially provided in the call to Prompt.
* @param isRetry `true` if this is the first time this prompt dialog instance
* on the stack is prompting the user for input; otherwise, false.
* @returns A `Promise` representing the asynchronous operation.
*/
protected async onPrompt(
context: TurnContext,
state: any,
options: PromptOptions,
isRetry: boolean,
): Promise<void> {
if (isRetry && options.retryPrompt) {
await context.sendActivity(options.retryPrompt, undefined, InputHints.ExpectingInput);
} else if (options.prompt) {
await context.sendActivity(options.prompt, undefined, InputHints.ExpectingInput);
}
}
/**
* Attempts to recognize the user's input.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext), context for the current
* turn of conversation with the user.
* @param _state Contains state for the current instance of the prompt on the dialog stack.
* @param _options A [PromptOptions](xref:botbuilder-dialogs.PromptOptions) object constructed
* from the options initially provided in the call to Prompt.
* @returns A `Promise` representing the asynchronous operation.
*/
protected async onRecognize(
context: TurnContext,
_state: any,
_options: PromptOptions,
): Promise<PromptRecognizerResult<DateTimeResolution[]>> {
const result: PromptRecognizerResult<DateTimeResolution[]> = { succeeded: false };
const activity: Activity = context.activity;
const utterance: string = activity.text;
if (!utterance) {
return result;
}
const locale: string = activity.locale || this.defaultLocale || 'en-us';
const results: any[] = Recognizers.recognizeDateTime(utterance, locale);
if (results.length > 0 && results[0].resolution) {
result.succeeded = true;
result.value = results[0].resolution.values;
}
return result;
}
}