UNPKG

botbuilder-dialogs

Version:

A dialog stack based conversation manager for Microsoft BotBuilder.

145 lines 7.07 kB
"use strict"; 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.ConfirmPrompt = void 0; /** * @module botbuilder-dialogs */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const Recognizers = require("@microsoft/recognizers-text-choice"); const choices_1 = require("../choices"); const prompt_1 = require("./prompt"); const promptCultureModels_1 = require("./promptCultureModels"); /** * Prompts a user to confirm something with a "yes" or "no" response. * * @remarks * By default the prompt will return to the calling dialog a `boolean` representing the users * selection. */ class ConfirmPrompt extends prompt_1.Prompt { /** * Creates a new ConfirmPrompt 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`. * @param choiceDefaults (Optional) Overrides the dictionary of Default Choices on [[PromptCultureModels.getSupportedCultures()]]. */ constructor(dialogId, validator, defaultLocale, choiceDefaults) { super(dialogId, validator); this.style = prompt_1.ListStyle.auto; this.defaultLocale = defaultLocale; if (choiceDefaults == undefined) { const supported = {}; promptCultureModels_1.PromptCultureModels.getSupportedCultures().forEach((culture) => { supported[culture.locale] = { choices: [culture.yesInLanguage, culture.noInLanguage], options: { inlineSeparator: culture.separator, inlineOr: culture.inlineOr, inlineOrMore: culture.inlineOrMore, includeNumbers: true, }, }; }); this.choiceDefaults = supported; } else { this.choiceDefaults = choiceDefaults; } } /** * 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* () { // Format prompt to send let prompt; const channelId = context.activity.channelId; const culture = this.determineCulture(context.activity); const choiceOptions = this.choiceOptions || this.choiceDefaults[culture].options; const choices = this.confirmChoices || this.choiceDefaults[culture].choices; if (isRetry && options.retryPrompt) { prompt = this.appendChoices(options.retryPrompt, channelId, choices, this.style, choiceOptions); } else { prompt = this.appendChoices(options.prompt, channelId, choices, this.style, choiceOptions); } // Send prompt yield context.sendActivity(prompt); }); } /** * 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 culture = this.determineCulture(context.activity); const results = Recognizers.recognizeBoolean(utterance, culture); if (results.length > 0 && results[0].resolution) { result.succeeded = true; result.value = results[0].resolution.value; } else { // If the prompt text was sent to the user with numbers, the prompt should recognize number choices. const choiceOptions = this.choiceOptions || this.choiceDefaults[culture].options; if (typeof choiceOptions.includeNumbers !== 'boolean' || choiceOptions.includeNumbers) { const confirmChoices = this.confirmChoices || this.choiceDefaults[culture].choices; const choices = [confirmChoices[0], confirmChoices[1]]; const secondOrMoreAttemptResults = choices_1.recognizeChoices(utterance, choices); if (secondOrMoreAttemptResults.length > 0) { result.succeeded = true; result.value = secondOrMoreAttemptResults[0].resolution.index === 0; } } } return result; }); } /** * @private */ determineCulture(activity) { let culture = promptCultureModels_1.PromptCultureModels.mapToNearestLanguage(activity.locale || this.defaultLocale || promptCultureModels_1.PromptCultureModels.English.locale); if (!(culture && this.choiceDefaults[culture])) { culture = promptCultureModels_1.PromptCultureModels.English.locale; } return culture; } } exports.ConfirmPrompt = ConfirmPrompt; //# sourceMappingURL=confirmPrompt.js.map