UNPKG

botbuilder-dialogs

Version:

A dialog stack based conversation manager for Microsoft BotBuilder.

190 lines 9.26 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.ActivityPrompt = void 0; /** * @module botbuilder-dialogs */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const botbuilder_core_1 = require("botbuilder-core"); const dialog_1 = require("../dialog"); /** * Waits for an activity to be received. * * @remarks * This prompt requires a validator be passed in and is useful when waiting for non-message * activities like an event to be received. The validator can ignore received events until the * expected activity is received. */ class ActivityPrompt extends dialog_1.Dialog { /** * Creates a new ActivityPrompt instance. * * @param dialogId Unique ID of the dialog within its parent `DialogSet` or `ComponentDialog`. * @param validator Validator that will be called each time a new activity is received. */ constructor(dialogId, validator) { super(dialogId); this.validator = validator; } /** * Called when a prompt dialog is pushed onto the dialog stack and is being activated. * * @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current * turn of the conversation. * @param options [PromptOptions](xref:botbuilder-dialogs.PromptOptions), additional * information to pass to the prompt being started. * @returns A `Promise` representing the asynchronous operation. * @remarks * If the promise is successful, the result indicates whether the prompt is still * active after the turn has been processed by the prompt. */ beginDialog(dc, options) { return __awaiter(this, void 0, void 0, function* () { // Ensure prompts have input hint set const opt = Object.assign({}, options); if (opt.prompt && typeof opt.prompt === 'object' && typeof opt.prompt.inputHint !== 'string') { opt.prompt.inputHint = botbuilder_core_1.InputHints.ExpectingInput; } if (opt.retryPrompt && typeof opt.retryPrompt === 'object' && typeof opt.retryPrompt.inputHint !== 'string') { opt.retryPrompt.inputHint = botbuilder_core_1.InputHints.ExpectingInput; } // Initialize prompt state const state = dc.activeDialog.state; state.options = opt; state.state = {}; // Send initial prompt yield this.onPrompt(dc.context, state.state, state.options, false); return dialog_1.Dialog.EndOfTurn; }); } /** * Called when a prompt dialog is the active dialog and the user replied with a new activity. * * @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current * turn of conversation. * @returns A `Promise` representing the asynchronous operation. * @remarks * If the promise is successful, the result indicates whether the dialog is still * active after the turn has been processed by the dialog. * The prompt generally continues to receive the user's replies until it accepts the * user's reply as valid input for the prompt. */ continueDialog(dc) { return __awaiter(this, void 0, void 0, function* () { // Perform base recognition const state = dc.activeDialog.state; const recognized = yield this.onRecognize(dc.context, state.state, state.options); if (state.state['attemptCount'] === undefined) { state.state['attemptCount'] = 0; } // Validate the return value // - Unlike the other prompts a validator is required for an ActivityPrompt so we don't // need to check for its existence before calling it. const isValid = yield this.validator({ context: dc.context, recognized: recognized, state: state.state, options: state.options, attemptCount: ++state.state['attemptCount'], }); // Return recognized value or re-prompt if (isValid) { return yield dc.endDialog(recognized.value); } else { if (dc.context.activity.type === botbuilder_core_1.ActivityTypes.Message && !dc.context.responded) { yield this.onPrompt(dc.context, state.state, state.options, true); } return dialog_1.Dialog.EndOfTurn; } }); } /** * Called when a prompt dialog resumes being the active dialog on the dialog stack, such as * when the previous active dialog on the stack completes. * * @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn * of the conversation. * @param _reason [DialogReason](xref:botbuilder-dialogs.DialogReason), an enum indicating why * the dialog resumed. * @param _result Optional. Value returned from the previous dialog on the stack. * The type of the value returned is dependent on the previous dialog. * @returns A `Promise` representing the asynchronous operation. */ resumeDialog(dc, _reason, _result) { return __awaiter(this, void 0, void 0, function* () { // Prompts are typically leaf nodes on the stack but the dev is free to push other dialogs // on top of the stack which will result in the prompt receiving an unexpected call to // resumeDialog() when the pushed on dialog ends. // To avoid the prompt prematurely ending we need to implement this method and // simply re-prompt the user. yield this.repromptDialog(dc.context, dc.activeDialog); return dialog_1.Dialog.EndOfTurn; }); } /** * Called when a prompt dialog has been requested to re-prompt the user for input. * * @param context [TurnContext](xref:botbuilder-core.TurnContext), context for the current * turn of conversation with the user. * @param instance [DialogInstance](xref:botbuilder-dialogs.DialogInstance), the instance * of the dialog on the stack. * @returns A `Promise` representing the asynchronous operation. */ repromptDialog(context, instance) { return __awaiter(this, void 0, void 0, function* () { const state = instance.state; yield this.onPrompt(context, state.state, state.options, false); }); } /** * When overridden in a derived class, prompts the user for input. * * @param context [TurnContext](xref:botbuilder-core.TurnContext), context for the current * turn of conversation with the user. * @param state Contains state for the current instance of the prompt on the dialog stack. * @param options A [PromptOptions](xref:botbuilder-dialogs.PromptOptions) object constructed * from the options initially provided in the call to Prompt. * @param isRetry A boolean representing if the prompt is a retry. * @returns A `Promise` representing the asynchronous operation. */ onPrompt(context, state, options, isRetry) { return __awaiter(this, void 0, void 0, function* () { if (isRetry && options.retryPrompt) { yield context.sendActivity(options.retryPrompt, undefined, botbuilder_core_1.InputHints.ExpectingInput); } else if (options.prompt) { yield context.sendActivity(options.prompt, undefined, botbuilder_core_1.InputHints.ExpectingInput); } }); } /** * When overridden in a derived class, attempts to recognize the incoming [Activity](xref:botframework-schema.Activity). * * @param context [TurnContext](xref:botbuilder-core.TurnContext), context for the current * turn of conversation with the user. * @param _state Contains state for the current instance of the prompt on the dialog stack. * @param _options A [PromptOptions](xref:botbuilder-dialogs.PromptOptions) object constructed * from the options initially provided in the call to Prompt. * @returns A `Promise` representing the asynchronous operation. */ onRecognize(context, _state, _options) { return __awaiter(this, void 0, void 0, function* () { return { succeeded: true, value: context.activity }; }); } } exports.ActivityPrompt = ActivityPrompt; //# sourceMappingURL=activityPrompt.js.map