UNPKG

botbuilder-dialogs

Version:

A dialog stack based conversation manager for Microsoft BotBuilder.

115 lines 5.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateTimePrompt = void 0; /** * @module botbuilder-dialogs */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const Recognizers = __importStar(require("@microsoft/recognizers-text-date-time")); const botbuilder_core_1 = require("botbuilder-core"); const prompt_1 = require("./prompt"); /** * Prompts a user to enter a datetime expression. * * @remarks * By default the prompt will return to the calling dialog a `DateTimeResolution[]`. */ class DateTimePrompt extends prompt_1.Prompt { /** * 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, validator, defaultLocale) { 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. */ onPrompt(context, state, options, isRetry) { return __awaiter(this, void 0, void 0, function* () { if (isRetry && options.retryPrompt) { yield context.sendActivity(options.retryPrompt, undefined, botbuilder_core_1.InputHints.ExpectingInput); } else if (options.prompt) { yield context.sendActivity(options.prompt, undefined, botbuilder_core_1.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. */ onRecognize(context, _state, _options) { return __awaiter(this, void 0, void 0, function* () { const result = { succeeded: false }; const activity = context.activity; const utterance = activity.text; if (!utterance) { return result; } const locale = activity.locale || this.defaultLocale || 'en-us'; const results = Recognizers.recognizeDateTime(utterance, locale); if (results.length > 0 && results[0].resolution) { result.succeeded = true; result.value = results[0].resolution.values; } return result; }); } } exports.DateTimePrompt = DateTimePrompt; //# sourceMappingURL=datetimePrompt.js.map