botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
253 lines • 11.9 kB
JavaScript
"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.ConfirmInput = void 0;
const choiceSet_1 = require("./choiceSet");
const inputDialog_1 = require("./inputDialog");
const adaptive_expressions_1 = require("adaptive-expressions");
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
const Recognizers = __importStar(require("@microsoft/recognizers-text-choice"));
const choiceOptionsSet_1 = require("./choiceOptionsSet");
/**
* Declarative input control that will gather yes/no confirmation input from a set of choices.
*/
class ConfirmInput extends inputDialog_1.InputDialog {
constructor() {
super(...arguments);
/**
* Style of the "yes" and "no" choices rendered to the user when prompting.
*
* @remarks
* Defaults to `ListStyle.auto`.
*/
this.style = new adaptive_expressions_1.EnumExpression(botbuilder_dialogs_1.ListStyle.auto);
/**
* Additional options passed to the `ChoiceFactory` and used to tweak the style of choices
* rendered to the user.
*/
this.choiceOptions = new adaptive_expressions_1.ObjectExpression();
/**
* Custom list of choices to send for the prompt.
*/
this.confirmChoices = new adaptive_expressions_1.ObjectExpression();
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'defaultLocale':
return new adaptive_expressions_1.StringExpressionConverter();
case 'style':
return new adaptive_expressions_1.EnumExpressionConverter(botbuilder_dialogs_1.ListStyle);
case 'choiceOptions':
return new adaptive_expressions_1.ObjectExpressionConverter();
case 'confirmChoices':
return new adaptive_expressions_1.ObjectExpressionConverter();
case 'outputFormat':
return new adaptive_expressions_1.StringExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* @protected
* @returns A `string` representing the compute Id.
*/
onComputeId() {
return `ConfirmInput[${this.prompt && this.prompt.toString()}]`;
}
/**
* @protected
* Called when input has been received.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @returns [InputState](xref:botbuilder-dialogs-adaptive.InputState) which reflects whether input was recognized as valid or not.
*/
onRecognizeInput(dc) {
return __awaiter(this, void 0, void 0, function* () {
// Recognize input if needed
let input = dc.state.getValue(inputDialog_1.InputDialog.VALUE_PROPERTY);
if (typeof input !== 'boolean') {
// Find locale to use
const locale = this.determineCulture(dc);
// Recognize input
const results = Recognizers.recognizeBoolean(input, locale);
if (results.length > 0 && results[0].resolution) {
input = results[0].resolution.value;
dc.state.setValue(inputDialog_1.InputDialog.VALUE_PROPERTY, !!input);
if (this.outputFormat) {
const value = this.outputFormat.getValue(dc.state);
dc.state.setValue(inputDialog_1.InputDialog.VALUE_PROPERTY, value);
}
return inputDialog_1.InputState.valid;
}
else {
// Fallback to trying the choice recognizer
const defaults = ConfirmInput.defaultChoiceOptions[locale].choices;
const confirmChoices = yield this.getConfirmChoices(dc, defaults);
const choices = botbuilder_dialogs_1.ChoiceFactory.toChoices(confirmChoices);
const results = (0, botbuilder_dialogs_1.recognizeChoices)(input, choices);
if (results.length > 0) {
input = results[0].resolution.index == 0;
dc.state.setValue(inputDialog_1.InputDialog.VALUE_PROPERTY, input);
}
else {
return inputDialog_1.InputState.unrecognized;
}
}
}
return inputDialog_1.InputState.valid;
});
}
/**
* @protected
* Method which renders the prompt to the user given the current input state.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param state Dialog [InputState](xref:botbuilder-dialogs-adaptive.InputState).
* @returns An [Activity](xref:botframework-schema.Activity) `Promise` representing the asynchronous operation.
*/
onRenderPrompt(dc, state) {
const _super = Object.create(null, {
onRenderPrompt: { get: () => super.onRenderPrompt }
});
return __awaiter(this, void 0, void 0, function* () {
// Determine locale
const locale = this.determineCulture(dc);
const defaults = ConfirmInput.defaultChoiceOptions[locale].choices;
// Format choices
const confirmChoices = yield this.getConfirmChoices(dc, defaults);
const choices = botbuilder_dialogs_1.ChoiceFactory.toChoices(confirmChoices);
// Format prompt to send
const prompt = yield _super.onRenderPrompt.call(this, dc, state);
const channelId = dc.context.activity.channelId;
const opts = yield this.getChoiceOptions(dc, locale);
const choiceOptions = opts !== null && opts !== void 0 ? opts : ConfirmInput.defaultChoiceOptions[locale].options;
const style = this.style.getValue(dc.state);
return Promise.resolve(this.appendChoices(prompt, channelId, choices, style, choiceOptions));
});
}
getChoiceOptions(dc, locale) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.choiceOptions) {
return ConfirmInput.defaultChoiceOptions[locale].options;
}
if (this.choiceOptions.expressionText != null &&
this.choiceOptions.expressionText.trimStart().startsWith('${')) {
// use TemplateInterface to bind (aka LG)
const choiceOptionsSet = new choiceOptionsSet_1.ChoiceOptionsSet(this.choiceOptions.expressionText);
return choiceOptionsSet.bind(dc);
}
else {
// use Expression to bind
return this.choiceOptions.getValue(dc.state);
}
});
}
determineCulture(dc) {
var _a, _b;
/**
* @deprecated Note: Default locale will be considered for deprecation as part of 4.13.
*/
const candidateLocale = (_a = dc.getLocale()) !== null && _a !== void 0 ? _a : (_b = this.defaultLocale) === null || _b === void 0 ? void 0 : _b.getValue(dc.state);
let culture = botbuilder_dialogs_1.PromptCultureModels.mapToNearestLanguage(candidateLocale);
if (!(culture && Object.hasOwnProperty.call(ConfirmInput.defaultChoiceOptions, culture))) {
culture = botbuilder_dialogs_1.PromptCultureModels.English.locale;
}
return culture;
}
getConfirmChoices(dc, defaults) {
return __awaiter(this, void 0, void 0, function* () {
let confirmChoices;
if (this.confirmChoices != null) {
if (this.confirmChoices.expressionText != null &&
this.confirmChoices.expressionText.trimLeft().startsWith('${')) {
// use TemplateInterface to bind (aka LG)
const choiceSet = new choiceSet_1.ChoiceSet(this.confirmChoices.expressionText);
confirmChoices = yield choiceSet.bind(dc, dc.state);
}
else {
// use Expression to bind
confirmChoices = this.confirmChoices.getValue(dc.state);
}
}
if (confirmChoices != null) {
return Promise.resolve(confirmChoices);
}
else {
return Promise.resolve(new choiceSet_1.ChoiceSet(defaults));
}
});
}
}
exports.ConfirmInput = ConfirmInput;
ConfirmInput.$kind = 'Microsoft.ConfirmInput';
/**
* Default options for rendering the choices to the user based on locale.
*/
ConfirmInput.defaultChoiceOptions = {
'es-es': {
choices: ['Sí', 'No'],
options: { inlineSeparator: ', ', inlineOr: ' o ', inlineOrMore: ', o ', includeNumbers: true },
},
'nl-nl': {
choices: ['Ja', 'Nee'],
options: { inlineSeparator: ', ', inlineOr: ' of ', inlineOrMore: ', of ', includeNumbers: true },
},
'en-us': {
choices: ['Yes', 'No'],
options: { inlineSeparator: ', ', inlineOr: ' or ', inlineOrMore: ', or ', includeNumbers: true },
},
'fr-fr': {
choices: ['Oui', 'Non'],
options: { inlineSeparator: ', ', inlineOr: ' ou ', inlineOrMore: ', ou ', includeNumbers: true },
},
'de-de': {
choices: ['Ja', 'Nein'],
options: { inlineSeparator: ', ', inlineOr: ' oder ', inlineOrMore: ', oder ', includeNumbers: true },
},
'ja-jp': {
choices: ['はい', 'いいえ'],
options: { inlineSeparator: '、 ', inlineOr: ' または ', inlineOrMore: '、 または ', includeNumbers: true },
},
'pt-br': {
choices: ['Sim', 'Não'],
options: { inlineSeparator: ', ', inlineOr: ' ou ', inlineOrMore: ', ou ', includeNumbers: true },
},
'zh-cn': {
choices: ['是的', '不'],
options: { inlineSeparator: ', ', inlineOr: ' 要么 ', inlineOrMore: ', 要么 ', includeNumbers: true },
},
};
//# sourceMappingURL=confirmInput.js.map