botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
85 lines • 4.22 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.ContinueConversationLater = void 0;
const adaptive_expressions_1 = require("adaptive-expressions");
const botbuilder_1 = require("botbuilder");
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
/**
* Action which schedules the current conversation to be continued at a later time.
*/
class ContinueConversationLater extends botbuilder_dialogs_1.Dialog {
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'disabled':
return new adaptive_expressions_1.BoolExpressionConverter();
case 'date':
return new adaptive_expressions_1.StringExpressionConverter();
case 'value':
return new adaptive_expressions_1.ValueExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* Called when the [Dialog](xref:botbuilder-dialogs.Dialog) is started and pushed onto the dialog stack.
*
* @param {DialogContext} dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param {object} _options Optional. Initial information to pass to the dialog.
* @returns {Promise<DialogTurnResult>} 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 dc.endDialog();
}
const dateString = this.date.getValue(dc.state);
const date = Date.parse(dateString);
if (!date || isNaN(date)) {
throw new Error('Date is invalid');
}
if (date <= Date.now()) {
throw new Error('Date must be in the future');
}
// create ContinuationActivity from the conversation reference.
const reference = botbuilder_1.TurnContext.getConversationReference(dc.context.activity);
const activity = botbuilder_1.TurnContext.applyConversationReference({
type: botbuilder_1.ActivityTypes.Event,
name: botbuilder_1.ActivityEventNames.ContinueConversation,
relatesTo: reference,
}, reference, true);
activity.value = this.value && this.value.getValue(dc.state);
const visibility = (date - Date.now()) / 1000;
const ttl = visibility + 2 * 60;
const queueStorage = dc.context.turnState.get(botbuilder_dialogs_1.DialogTurnStateConstants.queueStorage);
if (!queueStorage) {
throw new Error('Unable to locate QueueStorage in HostContext');
}
const receipt = yield queueStorage.queueActivity(activity, visibility, ttl);
// return the receipt as the result.
return dc.endDialog(receipt);
});
}
/**
* @protected
* @returns {string} A `string` representing the compute Id.
*/
onComputeId() {
return `ContinueConversationLater[${this.date && this.date.toString()}s]`;
}
}
exports.ContinueConversationLater = ContinueConversationLater;
ContinueConversationLater.$kind = 'Microsoft.ContinueConversationLater';
//# sourceMappingURL=continueConversationLater.js.map