- Free Azure Trial account
- Visual Studio 2017 or 2015 Community (update all extensions)
- Visual Studio Bot Application Template
Creating the Bot Project
The Bot Application template .zip file (see Prerequisites) should be in your Project Templates folder. Typically -
C:\Users[YOURUSERNAME]]\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#
Creating a new bot is as easy as creating any Visual Studio project. Click the File menu option, choose New, then Project.
Creating the Entity Framework Project
Next, right click on the solution and choose Add -> New Project.
Find the Class Library (.NET Framework) template, name the project, and click OK.
Once the two projects are created, the bot project needs to reference the data project. This is accomplished by right clicking on the bot project’s References node, choosing Add Reference, selecting Projects, clicking the checkbox beside the data project, and clicking the OK button.
Right click on the solution again and choose Manage NuGet Packages for Solution. Click the Browse link and search for Bot.Builder. Select it, choose the latest version, then on the rich hand menu select the Bot project and click Install.
Once Bot.Builder is updated, AutoMapper also needs to be installed. This is used to map the Bot.Builder Activities to the Activities saved in the database. Choose Browse in the NuGet package manager, enter automapper in the search box, select it, check the box next to the bot project, and click Install.
The Entity Framework is the last NuGet package that needs to be added. It should be added to both projects. Search for it, select it, click the checkboxes beside the project names and click Install.
Creating the Azure Sql Database
In the Azure Portal (https://portal.azure.com) click the New Plus button, and enter SQL Database in the filter field. Selecting the SQL Database option will display the creation panel. Click the Create button.
Fill in all the fields, and click Create again.
If you are using Azure Sql Server (as this example is), you might need to add your IP address to the firewall. It can be found on the SQL Database Overview blade.
Database Connection String
Once the Azure Sql Database has been provisioned, retrieve the connection string by navigating to the Properties page and clicking the Show database connection string link.
Copy the connection string and add it to both the <configuration> section in the App.config of the .Data project and the web.config of the .Bot project. This example uses Azure Sql connection string in ADO.NET, but any sql server instance compatible with the Entity Framework will suffice. Azure also provides support for JDBC, ODBC, and PHP.
Entity and Data Context
This example uses the Entity Framework code first method to create the Activity Logger table. To prepare the project for updating the database, create a new class in the data project named Activity and add the fields that will be logged.
Note: Only a small subset of the fields present on the Bot Framework Activity class are included in the sample.
Next, add a class called ConversationDataContext per the sample below.
Notice the connection string passed to the base DbContext constructor matches the name of the conneciton string added earlier to App.config and web.config of the two projects.
Next, open the NuGet Package Manager Console. This is found under the Tools menu.
In the Default project: dropdown, choose the data project.
Type enable-migrations at the PM> prompt and hit Enter. If everything has been setup correctly, the package manager will state “Code First Migrations enabled for project [YourProjectName]”. There should also be a new folder created in the project named “Migrations”. The folder will have a Configuration class.
Next, type add-migration InitialSetup at the PM>, and hit Enter. This will create the migration file that constructs the Activities table.
Finally, type update-database at the PM> and hit enter. This command will run the InitialSetup migration against the database defined in the conneciton string, and create the Activities table.
Open the Bot project’s Global.asax.cs file, and add the following code to the Application_Start() method:
This enables easily creating the Data.Activity objects from IMessageActivity objects within the EntityFrameworkActivityLogger
Entity Framework Activity Logger
Create a class in the Bot project called EntityFrameworkActivityLogger. It should inherit from the Microsoft.Bot.Builder.History.IActivityLogger interface.
Finally, go back to the Global.asax.cs file and add the following:
Eric Dahlvang from the Bot Framework Team