UNPKG

botbuilder-dialogs-adaptive

Version:

Rule system for the Microsoft BotBuilder dialog system.

115 lines 5.04 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.SendActivity = 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 adaptive_expressions_1 = require("adaptive-expressions"); const telemetryLoggerConstants_1 = require("../telemetryLoggerConstants"); const botbuilder_dialogs_1 = require("botbuilder-dialogs"); /** * Send an activity back to the user. */ class SendActivity extends botbuilder_dialogs_1.Dialog { /** * Creates a new [SendActivity](xref:botbuilder-dialogs-adaptive.SendActivity) instance. * * @param activity [Activity](xref:botframework-schema.Activity) or message text to send the user. */ constructor(activity) { super(); 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 '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` 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(`SendActivity: no activity assigned for action '${this.id}'.`) throw new Error('SendActivity: no activity assigned for action.'); } // Send activity and return result const data = Object.assign(dc.state, { utterance: dc.context.activity.text || '', }, 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.SendActivityResultEvent, }, }); let result; if (activityResult.type !== botbuilder_1.ActivityTypes.Message || activityResult.text || activityResult.speak || (activityResult.attachments && activityResult.attachments.length > 0) || activityResult.suggestedActions || activityResult.channelData) { result = yield dc.context.sendActivity(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 `SendActivity[${botbuilder_1.StringUtils.ellipsis(this.activity.template.trim(), 30)}]`; } return `SendActivity[${botbuilder_1.StringUtils.ellipsis(this.activity && this.activity.toString().trim(), 30)}]`; } } exports.SendActivity = SendActivity; SendActivity.$kind = 'Microsoft.SendActivity'; //# sourceMappingURL=sendActivity.js.map