UNPKG

botbuilder-dialogs

Version:

A dialog stack based conversation manager for Microsoft BotBuilder.

129 lines 6.47 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.ChoicePrompt = void 0; const choices_1 = require("../choices"); const prompt_1 = require("./prompt"); const promptCultureModels_1 = require("./promptCultureModels"); /** * Prompts a user to select from a list of choices. * * @remarks * By default the prompt will return to the calling dialog a `FoundChoice` object containing the * choice that was selected. */ class ChoicePrompt extends prompt_1.Prompt { /** * Creates a new `ChoicePrompt` instance. * * @param dialogId Unique ID of the dialog within its parent `DialogSet`. * @param validator (Optional) validator that will be called each time the user responds to the prompt. If the validator replies with a message no additional retry prompt will be sent. * @param defaultLocale (Optional) locale to use if `dc.context.activity.locale` not specified. Defaults to a value of `en-us`. * @param choiceDefaults (Optional) Overrides the dictionary of Bot Framework SDK-supported _choiceDefaults (for prompt localization). * Must be passed in to each ConfirmPrompt that needs the custom choice defaults. */ 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] = { 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* () { // Determine locale const locale = this.determineCulture(context.activity); // Format prompt to send let prompt; const choices = (this.style === prompt_1.ListStyle.suggestedAction ? choices_1.ChoiceFactory.toChoices(options.choices) : options.choices) || []; const channelId = context.activity.channelId; const choiceOptions = this.choiceOptions || this.choiceDefaults[locale]; const choiceStyle = options.style === 0 ? 0 : options.style || this.style; if (isRetry && options.retryPrompt) { prompt = this.appendChoices(options.retryPrompt, channelId, choices, choiceStyle, choiceOptions); } else { prompt = this.appendChoices(options.prompt, channelId, choices, choiceStyle, 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 choices = (this.style === prompt_1.ListStyle.suggestedAction ? choices_1.ChoiceFactory.toChoices(options.choices) : options.choices) || []; const opt = this.recognizerOptions || {}; opt.locale = this.determineCulture(activity, opt); const results = choices_1.recognizeChoices(utterance, choices, opt); if (Array.isArray(results) && results.length > 0) { result.succeeded = true; result.value = results[0].resolution; } return result; }); } /** * @private */ determineCulture(activity, opt) { const optLocale = opt && opt.locale ? opt.locale : null; let culture = promptCultureModels_1.PromptCultureModels.mapToNearestLanguage(activity.locale || optLocale || this.defaultLocale || promptCultureModels_1.PromptCultureModels.English.locale); if (!(culture && this.choiceDefaults[culture])) { culture = promptCultureModels_1.PromptCultureModels.English.locale; } return culture; } } exports.ChoicePrompt = ChoicePrompt; //# sourceMappingURL=choicePrompt.js.map