botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
81 lines • 3.75 kB
JavaScript
;
/**
* @module botbuilder-dialogs-adaptive
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.OnIntent = void 0;
const adaptive_expressions_1 = require("adaptive-expressions");
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
const onDialogEvent_1 = require("./onDialogEvent");
const adaptiveEvents_1 = require("../adaptiveEvents");
const actionChangeType_1 = require("../actionChangeType");
/**
* Actions triggered when an Activity has been received and the recognized intents and entities match specified list of intent and entity filters.
*/
class OnIntent extends onDialogEvent_1.OnDialogEvent {
/**
* Creates a new `OnIntent` instance.
*
* @param intent (Optional) Intent to match on.
* @param entities (Optional) Entities which must be recognized for this rule to trigger.
* @param actions (Optional) The actions to add to the plan when the rule constraints are met.
* @param condition (Optional) The condition which needs to be met for the actions to be executed.
*/
constructor(intent, entities = [], actions = [], condition) {
super(adaptiveEvents_1.AdaptiveEvents.recognizedIntent, actions, condition);
this.intent = intent;
this.entities = entities;
}
/**
* Create the expression for this condition.
*
* @returns [Expression](xref:adaptive-expressions.Expression) used to evaluate this rule.
*/
createExpression() {
if (!this.intent) {
throw new Error('Intent cannot be null.');
}
const trimmedIntent = this.intent.startsWith('#') ? this.intent.substring(1) : this.intent;
let intentExpression = adaptive_expressions_1.Expression.parse(`${botbuilder_dialogs_1.TurnPath.recognized}.intent == '${trimmedIntent}'`);
if (this.entities.length > 0) {
intentExpression = adaptive_expressions_1.Expression.andExpression(intentExpression, adaptive_expressions_1.Expression.andExpression(...this.entities.map((entity) => {
if (entity.startsWith('@') || entity.startsWith(botbuilder_dialogs_1.TurnPath.recognized)) {
return adaptive_expressions_1.Expression.parse(`exists(${entity})`);
}
return adaptive_expressions_1.Expression.parse(`exists(@${entity})`);
})));
}
return adaptive_expressions_1.Expression.andExpression(intentExpression, super.createExpression());
}
/**
* @protected
* Called when a change list is created.
* @param actionContext [ActionContext](xref:botbuilder-dialogs-adaptive.ActionContext) to use for evaluation.
* @param dialogOptions Optional. Object with dialog options.
* @returns An [ActionChangeList](xref:botbuilder-dialogs-adaptive.ActionChangeList) with the list of actions.
*/
onCreateChangeList(actionContext, dialogOptions) {
const recognizerResult = actionContext.state.getValue(`${botbuilder_dialogs_1.TurnPath.dialogEvent}.value`);
if (recognizerResult) {
const actionState = {
dialogId: this.actionScope.id,
options: dialogOptions,
dialogStack: [],
};
const changeList = {
changeType: actionChangeType_1.ActionChangeType.insertActions,
actions: [actionState],
turn: {},
};
return changeList;
}
return super.onCreateChangeList(actionContext, dialogOptions);
}
}
exports.OnIntent = OnIntent;
OnIntent.$kind = 'Microsoft.OnIntent';
//# sourceMappingURL=onIntent.js.map