This topic applies to SDK v3 release. You can find the documentation for the latest version of the SDK v4 here. Dialogs are very powerful and flexible, but handling a guided conversation such as ordering a sandwich can require a lot of effort. At each point in the conversation, there are many possibilities of what will happen next.
|Published (Last):||7 October 2010|
|PDF File Size:||15.82 Mb|
|ePub File Size:||14.99 Mb|
|Price:||Free* [*Free Regsitration Required]|
In this article we'll convert the v3 ContosoHelpdeskChatBot into a v4 bot without converting the project type. It will remain a. NET Framework project. This conversion is broken down into these steps:. The result of this conversion is the. To migrate to a. NET Core v4 bot in new project, see Migrate a. NET v3 bot to a.
NET Core v4 bot. However, SDK v4 is a refactoring of the previous version of the SDK to allow developers more flexibility and control over their bots. Major changes in the SDK include:. For more information about specific changes, see differences between the v3 and v4. Update Microsoft. Azure and Microsoft. WebApi to the latest stable version. This will update the Microsoft.
Builder and Microsoft. Connector packages as well, since they are dependencies. Delete the Microsoft. History package. This is not part of the v4 SDK. This is a community library for building v4 dialogs from v3 Formflow definition files. It has Microsoft. Dialogs as one of its dependencies, so this is also installed for us. If your project targets. NET Framework 4.
Formflow is a. NET Standard 2. For more information, see. NET implementation support. If you build at this point, you will get compiler errors.
You can ignore these. Once we're finished with our conversion, we'll have working code. Some of the scaffolding has changed, and we have to set up parts of the state management infrastructure ourselves in v4. For instance, v4 uses a bot adapter to handle authentication and forward activities to your bot code, and we have declare our state properties up front. We'll create a state property for DialogState , which we now need for dialog support in v4.
We'll use dependency injection to get necessary information to the controller and bot code. Replace the BotConfig. UpdateConversationContainer method with this BotConfig. Register method, where we'll register objects we need to support dependency injection.
This bot uses neither user nor private conversation state, so we create only the conversation state management object. This is where the bot starts a turn in v4, so this need to change a lot. Except for the bot's turn handler itself, most of this can be thought of as boilerplate.
Remove the [BotAuthentication] attribute from the class. In v4, the bot's adapter will handle authentication. Add these fields and a constructor to initialize them. NET and Autofac use dependency injection to get the parameters values. We registered adapter and bot objects in Global. Replace the body of the Post method. We use the adapter to call our bot's message loop turn handler. In v4, the turn handler or message loop logic is primarily in a bot file.
We're deriving from ActivityHandler , which defines handlers for common types of activities. Derive DialogBot from ActivityHandler , and add a generic parameter for the dialog. Again, ASP. Override OnMessageActivityAsync to invoke our main dialog. We'll define the Run extension method shortly. Override OnTurnAsync to save our conversation state at the end of the turn. In v4, we have to do this explicitly to write state out to the persistence layer.
OnTurnAsync method calls specific activity handler methods, based on the type of activity received, so we save state after the call to the base method. We're creating an extension method to consolidate the code needed to run a bare component dialog from our bot.
Create a DialogExtensions. We'll make many changes to the original dialogs to migrate them to the v4 SDK. Don't worry about the compiler errors for now. These will resolve once we've finished the conversion. In the interest of not modifying the original code more than necessary, there will remain some compiler warnings after we've finished the migration.
Control flow and messaging within dialogs are no longer handled the same way, so we'll need to revise this as we convert each dialog. In this bot, the root dialog prompts the user for a choice from a set of options, and then starts a child dialog based on that choice. This then loops for the lifetime of the conversation.
We need to convert HelpdeskOptions options from a list of strings to a list of choices. This will be used with a choice prompt, which will accept the choice number in the list , the choice value, or any of the choice's synonyms as valid input. We can delete the StartAsync method. When a component dialog begins, it automatically starts its initial dialog.
In this case, that's the waterfall dialog we defined in the constructor. That also automatically starts at its first step. We will delete the MessageReceivedAsync and ShowOptions methods, and replace them with the first step of our waterfall. These two methods greeted the user and asked them to choose one of the available options. We can replace OnOptionSelected with the second step of our waterfall.
We still start a child dialog based on the user's input. Finally, replace the old ResumeAfterOptionDialog method with the last step of our waterfall. The install app dialog performs a few logical tasks, which we'll set up as a 4-step waterfall dialog.
How you factor existing code into waterfall steps is a logical exercise for each dialog. For each step, the original method the code came from is noted.
We can replace appNameAsync and multipleAppsAsync with the second step of our waterfall. We'll capture that portion of the logic in the next step of the waterfall. To simulate the database call, we mock up getAppsAsync to query a static list, instead of the database. In v3, this dialog greeted the user, started the Formflow dialog, and then saved the result off to a database. This translates easily into a two-step waterfall.
Update the using statements. Note that this dialog includes a v3 Formflow dialog. In v4 we can use the community Formflow library. We can remove the instance property for LocalAdmin , as the result will be available in dialog state.
Add a constructor and initialize the component's dialog set. The Formflow dialog is created in the same way. We're just adding it to the dialog set of our component in the constructor. We can replace StartAsync with the first step of our waterfall. We already created the Formflow in the constructor, and the other two statements translate to this. We have to get the return value from the step context, instead of from an instance property.
BuildLocalAdminForm remains largely the same, except we don't have the Formflow update the instance property. In v3, this dialog greeted the user, authorized the user with a pass code, failed out or started the Formflow dialog, and then reset the password.
Migrate a .NET v3 bot to a .NET Framework v4 bot
Basic features of FormFlow