|
| 1 | +# Bot Builder Fluent Dialog |
| 2 | + |
| 3 | +A Microsoft BotBuilder dialog implementation using event sourcing. |
| 4 | + |
| 5 | +- [Installing](#installing) |
| 6 | +- [Basic Use](#use) |
| 7 | +- [Learn More](#learn-more) |
| 8 | +- [Documentation](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) |
| 9 | +- [Class Reference](https://docs.microsoft.com/en-us/javascript/api/botbuilder-dialogs/) |
| 10 | +- [GitHub Repo](https://github.com/Microsoft/botbuilder-js) |
| 11 | +- [Report Issues](https://github.com/Microsoft/botbuilder-js/issues) |
| 12 | + |
| 13 | +## Installing |
| 14 | +To add the latest version of this package to your bot: |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install --save botbuilder-dialogs-fluent |
| 18 | +``` |
| 19 | + |
| 20 | +#### How to Use Daily Builds |
| 21 | +If you want to play with the very latest versions of botbuilder, you can opt in to working with the daily builds. This is not meant to be used in a production environment and is for advanced development. Quality will vary and you should only use daily builds for exploratory purposes. |
| 22 | + |
| 23 | +To get access to the daily builds of this library, configure npm to use the MyGet feed before installing. |
| 24 | + |
| 25 | +```bash |
| 26 | +npm config set registry https://botbuilder.myget.org/F/botbuilder-v4-js-daily/npm/ |
| 27 | +``` |
| 28 | + |
| 29 | +To reset the registry in order to get the latest published version, run: |
| 30 | +```bash |
| 31 | +npm config set registry https://registry.npmjs.org/ |
| 32 | +``` |
| 33 | + |
| 34 | +## What's included? |
| 35 | + |
| 36 | +This module includes a dialog implementation using an approach similar to a durable function. The FluentDialog uses event sourcing to enable arbitrarily complex user interactions in a seemingly uninterrupted execution flow. |
| 37 | +Behind the scenes, the yield operator in the dialog flow function yields control of the execution thread back to a dialog flow dispatcher. The dispatcher then commits any new actions that the dialog flow function scheduled (such as starting a child dialog, receiving an activity or making an async call) to storage. |
| 38 | +The transparent commit action updates the execution history of the dialog flow by appending all new events into the dialog state, much like an append-only log. |
| 39 | +Once the history is updated, the dialog ends its turn and, when it is later resumed, the dispatcher re-executes the entire function from the start to rebuild the local state. |
| 40 | +During the replay, if the code tries to begin a child dialog (or do any async work), the dispatcher consults the execution history, replays that result and the function code continues to run. |
| 41 | +The replay continues until the function code is finished or until it yields a new suspension task. |
| 42 | + |
| 43 | +## Use |
| 44 | + |
| 45 | +After adding the module to your application, modify your app's code to import the multi-turn dialog management capabilities. Near your other `import` and `require` statements, add: |
| 46 | + |
| 47 | +```javascript |
| 48 | +// Import some of the capabities from the module. |
| 49 | +const { DialogSet, TextPrompt, ConfirmPrompt } = require("botbuilder-dialogs"); |
| 50 | +const { FluentDialog } = require("botbuilder-dialogs-fluent"); |
| 51 | +``` |
| 52 | + |
| 53 | +Then, create one or more `DialogSet` objects to manage the dialogs used in your bot. |
| 54 | +A DialogSet is used to collect and execute dialogs. A bot may have more than one |
| 55 | +DialogSet, which can be used to group dialogs logically and avoid name collisions. |
| 56 | + |
| 57 | +Then, create one or more dialogs and add them to the DialogSet. Use the WaterfallDialog |
| 58 | +class to construct dialogs defined by a series of functions for sending and receiving input |
| 59 | +that will be executed in order. |
| 60 | + |
| 61 | +More sophisticated multi-dialog sets can be created using the `ComponentDialog` class, which |
| 62 | +contains a DialogSet, is itself also a dialog that can be triggered like any other. By building on top ComponentDialog, |
| 63 | +developer can bundle multiple dialogs into a single unit which can then be packaged, distributed and reused. |
| 64 | + |
| 65 | +```javascript |
| 66 | +// Set up a storage system that will capture the conversation state. |
| 67 | +const storage = new MemoryStorage(); |
| 68 | +const convoState = new ConversationState(storage); |
| 69 | + |
| 70 | +// Define a property associated with the conversation state. |
| 71 | +const dialogState = convoState.createProperty('dialogState'); |
| 72 | + |
| 73 | +// Initialize a DialogSet, passing in a property used to capture state. |
| 74 | +const dialogs = new DialogSet(dialogState); |
| 75 | + |
| 76 | +// Each dialog is identified by a unique name used to invoke the dialog later. |
| 77 | +const MAIN_DIALOG = 'MAIN_DIALOG'; |
| 78 | +const TEXT_PROMPT = 'TEXT_PROMPT' |
| 79 | +const CONFIRM_PROMPT = 'CONFIRM_PROMPT' |
| 80 | + |
| 81 | +// Implement the dialog flow function |
| 82 | +function *dialogFlow(context) { |
| 83 | + |
| 84 | + let response = yield context.prompt(DIALOG_PROMPT, 'say something'); |
| 85 | + yield context.sendActivity(`you said: ${response}`); |
| 86 | + |
| 87 | + let shouldContinue = yield context.prompt(CONFIRM_PROMPT, 'play another round?', ['yes', 'no']) |
| 88 | + if (shouldContinue) { |
| 89 | + yield context.restart(); |
| 90 | + } |
| 91 | + |
| 92 | + yield context.sendActivity('good bye!'); |
| 93 | +} |
| 94 | + |
| 95 | +// Add a dialog. Use the included FluentDialog type, initialized with the dialog flow function |
| 96 | +dialogs.add(new FluentDialog(MAIN_DIALOG, dialogFlow)); |
| 97 | +dialogs.add(new TextPrompt(DIALOG_PROMPT)); |
| 98 | +dialogs.add(new ConfirmPrompt(CONFIRM_PROMPT)); |
| 99 | + |
| 100 | +``` |
| 101 | + |
| 102 | +Finally, from somewhere in your bot's code, invoke your dialog by name: |
| 103 | + |
| 104 | +```javascript |
| 105 | +// Receive and process incoming events into TurnContext objects in the normal way |
| 106 | +adapter.processActivity(req, res, async (turnContext) => { |
| 107 | + // Create a DialogContext object from the incoming TurnContext |
| 108 | + const dc = await dialogs.createContext(turnContext); |
| 109 | + |
| 110 | + // ...evaluate message and do other bot logic... |
| 111 | + |
| 112 | + // If the bot hasn't yet responded, try to continue any active dialog |
| 113 | + if (!turnContext.responded) { |
| 114 | + const results = await dc.continueDialog(); |
| 115 | + if (results.status === DialogTurnStatus.empty) { |
| 116 | + await dialogContext.beginDialog(MAIN_DIALOG); |
| 117 | + } |
| 118 | + } |
| 119 | +}); |
| 120 | +``` |
| 121 | + |
| 122 | +## Examples |
| 123 | + |
| 124 | +See this module in action in these example apps: |
| 125 | + |
| 126 | +# Learn More |
| 127 | + |
| 128 | +[Prompts](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=javascript) This module contains several types of built-in prompt that can be used to create dialogs that capture and validate specific data types like dates, numbers and multiple-choice answers. |
| 129 | + |
| 130 | +[DialogSet](https://docs.microsoft.com/en-us/javascript/api/botbuilder-dialogs/dialogset) DialogSet is a container for multiple dialogs. Once added to a DialogSet, dialogs can be called and interlinked. |
| 131 | + |
| 132 | +[ComponentDialog](https://docs.microsoft.com/en-us/javascript/api/botbuilder-dialogs/componentdialog) ComponentDialogs are containers that encapsulate multiple sub-dialogs, but can be invoked like normal dialogs. This is useful for re-usable dialogs, or creating multiple dialogs with similarly named sub-dialogs that would otherwise collide. |
0 commit comments