UNPKG

botbuilder-dialogs

Version:

A dialog stack based conversation manager for Microsoft BotBuilder.

329 lines (328 loc) 16 kB
/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import { Activity, TurnContext, TurnContextStateCollection } from 'botbuilder-core'; import { Choice } from './choices'; import { Dialog, DialogInstance, DialogTurnResult } from './dialog'; import { DialogSet } from './dialogSet'; import { PromptOptions } from './prompts'; import { DialogStateManager } from './memory'; import { DialogManager } from './dialogManager'; /** * Contains dialog state, information about the state of the dialog stack, for a specific [DialogSet](xref:botbuilder-dialogs.DialogSet). * * @remarks * State is read from and saved to storage each turn, and state cache for the turn is managed through * the [TurnContext](xref:botbuilder-core.TurnContext). * * For more information, see the articles on * [Managing state](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-state) and * [Dialogs library](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-dialog). */ export interface DialogState { /** * Contains state information for each [Dialog](xref:botbuilder-dialogs.Dialog) on the stack. */ dialogStack: DialogInstance[]; } /** * The context for the current dialog turn with respect to a specific [DialogSet](xref:botbuilder-dialogs.DialogSet). * * @remarks * This includes the turn context, information about the dialog set, and the state of the dialog stack. * * From code outside of a dialog in the set, use [DialogSet.createContext](xref:botbuilder-dialogs.DialogSet.createContext) * to create the dialog context. Then use the methods of the dialog context to manage the progression of dialogs in the set. * * When you implement a dialog, the dialog context is a parameter available to the various methods you override or implement. * * For example: * ```JavaScript * const dc = await dialogs.createContext(turnContext); * const result = await dc.continueDialog(); * ``` */ export declare class DialogContext { /** * Creates an new instance of the [DialogContext](xref:botbuilder-dialogs.DialogContext) class. * * @param dialogs The [DialogSet](xref:botbuilder-dialogs.DialogSet) for which to create the dialog context. * @param contextOrDC The [TurnContext](xref:botbuilder-core.TurnContext) object for the current turn of the bot. * @param state The state object to use to read and write [DialogState](xref:botbuilder-dialogs.DialogState) to storage. * @remarks * Passing in a [DialogContext](xref:botbuilder-dialogs.DialogContext) instance will clone the dialog context. */ constructor(dialogs: DialogSet, contextOrDC: TurnContext, state: DialogState); /** * Creates an new instance of the [DialogContext](xref:botbuilder-dialogs.DialogContext) class. * * @param dialogs The [DialogSet](xref:botbuilder-dialogs.DialogSet) for which to create the dialog context. * @param contextOrDC The [DialogContext](xref:botbuilder-dialogs.DialogContext) object for the current turn of the bot. * @param state The state object to use to read and write [DialogState](xref:botbuilder-dialogs.DialogState) to storage. * @remarks * Passing in a [DialogContext](xref:botbuilder-dialogs.DialogContext) instance will clone the dialog context. */ constructor(dialogs: DialogSet, contextOrDC: DialogContext, state: DialogState); /** * Gets the dialogs that can be called directly from this context. */ dialogs: DialogSet; /** * Gets the context object for the turn. */ context: TurnContext; /** * Gets the current dialog stack. */ stack: DialogInstance[]; /** * The parent dialog context for this dialog context, or `undefined` if this context doesn't have a parent. * * @remarks * When it attempts to start a dialog, the dialog context searches for the [Dialog.id](xref:botbuilder-dialogs.Dialog.id) * in its [dialogs](xref:botbuilder-dialogs.DialogContext.dialogs). If the dialog to start is not found * in this dialog context, it searches in its parent dialog context, and so on. */ parent: DialogContext | undefined; /* * @returns Dialog context for child if the active dialog is a container. */ readonly child: DialogContext | undefined; /* * @returns The state information for the dialog on the top of the dialog stack, or `undefined` if * the stack is empty. */ readonly activeDialog: DialogInstance | undefined; /** * Gets the DialogStateManager which manages view of all memory scopes. */ state: DialogStateManager; /** * Gets the services collection which is contextual to this dialog context. */ services: TurnContextStateCollection; /* * @deprecated This property serves no function. * @returns The current dialog manager instance. This property is deprecated. */ readonly dialogManager: DialogManager; /** * Obtain the CultureInfo in DialogContext. * * @returns a locale string. */ getLocale(): string; /** * Starts a dialog instance and pushes it onto the dialog stack. * Creates a new instance of the dialog and pushes it onto the stack. * * @param dialogId ID of the dialog to start. * @param options Optional. Arguments to pass into the dialog when it starts. * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result. * @remarks * If there's already an active dialog on the stack, that dialog will be paused until * it is again the top dialog on the stack. * * The [status](xref:botbuilder-dialogs.DialogTurnResult.status) of returned object describes * the status of the dialog stack after this method completes. * * This method throws an exception if the requested dialog can't be found in this dialog context * or any of its ancestors. * * For example: * ```JavaScript * const result = await dc.beginDialog('greeting', { name: user.name }); * ``` * * **See also** * - [endDialog](xref:botbuilder-dialogs.DialogContext.endDialog) * - [prompt](xref:botbuilder-dialogs.DialogContext.prompt) * - [replaceDialog](xref:botbuilder-dialogs.DialogContext.replaceDialog) * - [Dialog.beginDialog](xref:botbuilder-dialogs.Dialog.beginDialog) */ beginDialog(dialogId: string, options?: object): Promise<DialogTurnResult>; /** * Cancels all dialogs on the dialog stack, and clears stack. * * @param cancelParents Optional. If `true` all parent dialogs will be cancelled as well. * @param eventName Optional. Name of a custom event to raise as dialogs are cancelled. This defaults to [cancelDialog](xref:botbuilder-dialogs.DialogEvents.cancelDialog). * @param eventValue Optional. Value to pass along with custom cancellation event. * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result. * @remarks * This calls each dialog's [Dialog.endDialog](xref:botbuilder-dialogs.Dialog.endDialog) method before * removing the dialog from the stack. * * If there were any dialogs on the stack initially, the [status](xref:botbuilder-dialogs.DialogTurnResult.status) * of the return value is [cancelled](xref:botbuilder-dialogs.DialogTurnStatus.cancelled); otherwise, it's * [empty](xref:botbuilder-dialogs.DialogTurnStatus.empty). * * This example clears a dialog stack, `dc`, before starting a 'bookFlight' dialog. * ```JavaScript * await dc.cancelAllDialogs(); * return await dc.beginDialog('bookFlight'); * ``` * * **See also** * - [endDialog](xref:botbuilder-dialogs.DialogContext.endDialog) */ cancelAllDialogs(cancelParents?: boolean, eventName?: string, eventValue?: any): Promise<DialogTurnResult>; /** * Searches for a dialog with a given ID. * * @param dialogId ID of the dialog to search for. * @returns The dialog for the provided ID. * @remarks * If the dialog to start is not found in the [DialogSet](xref:botbuilder-dialogs.DialogSet) associated * with this dialog context, it attempts to find the dialog in its parent dialog context. * * **See also** * - [dialogs](xref:botbuilder-dialogs.DialogContext.dialogs) * - [parent](xref:botbuilder-dialogs.DialogContext.parent) */ findDialog(dialogId: string): Dialog | undefined; /** * Helper function to simplify formatting the options for calling a prompt dialog. * * @param dialogId ID of the prompt dialog to start. * @param promptOrOptions The text of the initial prompt to send the user, * the activity to send as the initial prompt, or * the object with which to format the prompt dialog. * @param choices Optional. Array of choices for the user to choose from, * for use with a [ChoicePrompt](xref:botbuilder-dialogs.ChoicePrompt). * * @remarks * This helper method formats the object to use as the `options` parameter, and then calls * [beginDialog](xref:botbuilder-dialogs.DialogContext.beginDialog) to start the specified prompt dialog. * * ```JavaScript * return await dc.prompt('confirmPrompt', `Are you sure you'd like to quit?`); * ``` */ prompt(dialogId: string, promptOrOptions: string | Partial<Activity> | PromptOptions): Promise<DialogTurnResult>; /** * Helper function to simplify formatting the options for calling a prompt dialog. * * @param dialogId ID of the prompt dialog to start. * @param promptOrOptions The text of the initial prompt to send the user, * the [Activity](xref:botframework-schema.Activity) to send as the initial prompt, or * the object with which to format the prompt dialog. * @param choices Optional. Array of choices for the user to choose from, * for use with a [ChoicePrompt](xref:botbuilder-dialogs.ChoicePrompt). * @remarks * This helper method formats the object to use as the `options` parameter, and then calls * [beginDialog](xref:botbuilder-dialogs.DialogContext.beginDialog) to start the specified prompt dialog. * * ```JavaScript * return await dc.prompt('confirmPrompt', `Are you sure you'd like to quit?`); * ``` */ prompt(dialogId: string, promptOrOptions: string | Partial<Activity> | PromptOptions, choices: (string | Choice)[]): Promise<DialogTurnResult>; /** * Continues execution of the active dialog, if there is one, by passing this dialog context to its * [Dialog.continueDialog](xref:botbuilder-dialogs.Dialog.continueDialog) method. * * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result. * @remarks * After the call completes, you can check the turn context's [responded](xref:botbuilder-core.TurnContext.responded) * property to determine if the dialog sent a reply to the user. * * The [status](xref:botbuilder-dialogs.DialogTurnResult.status) of returned object describes * the status of the dialog stack after this method completes. * * Typically, you would call this from within your bot's turn handler. * * For example: * ```JavaScript * const result = await dc.continueDialog(); * if (result.status == DialogTurnStatus.empty && dc.context.activity.type == ActivityTypes.message) { * // Send fallback message * await dc.context.sendActivity(`I'm sorry. I didn't understand.`); * } * ``` */ continueDialog(): Promise<DialogTurnResult>; /** * Ends a dialog and pops it off the stack. Returns an optional result to the dialog's parent. * * @param result Optional. A result to pass to the parent logic. This might be the next dialog * on the stack, or if this was the last dialog on the stack, a parent dialog context or * the bot's turn handler. * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result. * @remarks * The _parent_ dialog is the next dialog on the dialog stack, if there is one. This method * calls the parent's [Dialog.resumeDialog](xref:botbuilder-dialogs.Dialog.resumeDialog) method, * passing the result returned by the ending dialog. If there is no parent dialog, the turn ends * and the result is available to the bot through the returned object's * [result](xref:botbuilder-dialogs.DialogTurnResult.result) property. * * The [status](xref:botbuilder-dialogs.DialogTurnResult.status) of returned object describes * the status of the dialog stack after this method completes. * * Typically, you would call this from within the logic for a specific dialog to signal back to * the dialog context that the dialog has completed, the dialog should be removed from the stack, * and the parent dialog should resume. * * For example: * ```JavaScript * return await dc.endDialog(returnValue); * ``` * * **See also** * - [beginDialog](xref:botbuilder-dialogs.DialogContext.beginDialog) * - [replaceDialog](xref:botbuilder-dialogs.DialogContext.replaceDialog) * - [Dialog.endDialog](xref:botbuilder-dialogs.Dialog.endDialog) */ endDialog(result?: any): Promise<DialogTurnResult>; /** * Ends the active dialog and starts a new dialog in its place. * * @param dialogId ID of the dialog to start. * @param options Optional. Arguments to pass into the new dialog when it starts. * @returns {Promise<DialogTurnResult>} a promise resolving to the dialog turn result. * @remarks * This is particularly useful for creating a loop or redirecting to another dialog. * * The [status](xref:botbuilder-dialogs.DialogTurnResult.status) of returned object describes * the status of the dialog stack after this method completes. * * This method is similar to ending the current dialog and immediately beginning the new one. * However, the parent dialog is neither resumed nor otherwise notified. * * **See also** * - [beginDialog](xref:botbuilder-dialogs.DialogContext.beginDialog) * - [endDialog](xref:botbuilder-dialogs.DialogContext.endDialog) */ replaceDialog(dialogId: string, options?: object): Promise<DialogTurnResult>; /** * Requests the active dialog to re-prompt the user for input. * * @remarks * This calls the active dialog's [repromptDialog](xref:botbuilder-dialogs.Dialog.repromptDialog) method. * * For example: * ```JavaScript * await dc.repromptDialog(); * ``` */ repromptDialog(): Promise<void>; /** * Searches for a dialog with a given ID. * * @remarks * Emits a named event for the current dialog, or someone who started it, to handle. * @param name Name of the event to raise. * @param value Optional. Value to send along with the event. * @param bubble Optional. Flag to control whether the event should be bubbled to its parent if not handled locally. Defaults to a value of `true`. * @param fromLeaf Optional. Whether the event is emitted from a leaf node. * @returns `true` if the event was handled. */ emitEvent(name: string, value?: any, bubble?: boolean, fromLeaf?: boolean): Promise<boolean>; /** * @private * @param reason * @param result */ private endActiveDialog; } //# sourceMappingURL=dialogContext.d.ts.map