botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
113 lines • 5.04 kB
JavaScript
;
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.UpdateActivity = void 0;
/**
* @module botbuilder-dialogs-adaptive
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const botbuilder_1 = require("botbuilder");
const templates_1 = require("../templates");
const converters_1 = require("../converters");
const telemetryLoggerConstants_1 = require("../telemetryLoggerConstants");
const adaptive_expressions_1 = require("adaptive-expressions");
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
/**
* Update an activity with replacement.
*/
class UpdateActivity extends botbuilder_dialogs_1.Dialog {
/**
* Initializes a new instance of the [UpdateActivity](xref:botbuilder-dialogs-adaptive.UpdateActivity) class.
*
* @param activityId Optional. The expression which resolves to the activityId to update.
* @param activity Optional. Template for the [Activity](xref:botframework-schema.Activity).
*/
constructor(activityId, activity) {
super();
if (activityId) {
this.activityId = new adaptive_expressions_1.StringExpression(activityId);
}
if (activity) {
if (typeof activity === 'string') {
this.activity = new templates_1.ActivityTemplate(activity);
}
else {
this.activity = new templates_1.StaticActivityTemplate(activity);
}
}
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'activity':
return new converters_1.ActivityTemplateConverter();
case 'activityId':
return new adaptive_expressions_1.StringExpressionConverter();
case 'disabled':
return new adaptive_expressions_1.BoolExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* Starts a new [Dialog](xref:botbuilder-dialogs.Dialog) and pushes it onto the dialog stack.
*
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param options Optional. Initial information to pass to the dialog.
* @returns A `Promise` representing the asynchronous operation.
*/
beginDialog(dc, options) {
return __awaiter(this, void 0, void 0, function* () {
if (this.disabled && this.disabled.getValue(dc.state)) {
return yield dc.endDialog();
}
if (!this.activity) {
throw new Error('UpdateActivity: no activity assigned for action.');
}
const data = Object.assign({
utterance: dc.context.activity.text || '',
}, dc.state, options);
const activityResult = yield this.activity.bind(dc, data);
this.telemetryClient.trackEvent({
name: telemetryLoggerConstants_1.TelemetryLoggerConstants.GeneratorResultEvent,
properties: {
template: this.activity,
result: activityResult || '',
context: telemetryLoggerConstants_1.TelemetryLoggerConstants.UpdateActivityResultEvent,
},
});
const value = this.activityId.getValue(dc.state);
activityResult.id = value.toString();
const result = yield dc.context.updateActivity(activityResult);
return yield dc.endDialog(result);
});
}
/**
* @protected
* Builds the compute Id for the [Dialog](xref:botbuilder-dialogs.Dialog).
* @returns A `string` representing the compute Id.
*/
onComputeId() {
if (this.activity instanceof templates_1.ActivityTemplate) {
return `UpdateActivity[${botbuilder_1.StringUtils.ellipsis(this.activity.template.trim(), 30)}]`;
}
return `UpdateActivity[${botbuilder_1.StringUtils.ellipsis(this.activity && this.activity.toString().trim(), 30)}]`;
}
}
exports.UpdateActivity = UpdateActivity;
UpdateActivity.$kind = 'Microsoft.UpdateActivity';
//# sourceMappingURL=updateActivity.js.map