Creating Chat Bot using 100% ASP.NET Core 2.0 Web API and Microsoft Bot Framework

Bot Application with BotBuilder SDK

To create a chat bot application using .NET Framework, you might already know this type of Bot Application template when launching new project in Visual Studio 2017.

The problem is, the NuGet packages and the template project which is from the BotBuilder SDK currently only support .NET Framework 4.6 and above as stated on the page and not yet supporting .NET Core related. Here is the message from the docs page:

Well, since the Bot Builder SDK is an Open-Source stuff, we can visit the source code from GitHub itself and see what Microsoft and the communities is up to to with regards to supporting .NET Core 2.0 related.

BotBuilder SDK on GitHub

We are in luck. Apparently they are doing some changes and adding some stuff for the .NET Core 2.0 as well. There is a branch called NETCore2 on the GitHub. And all I have to do is use whatever available first and try to stitch the missing stuff. So, first things first, clone the BotBuilder SDK. Open your PowerShell, Windows Command Prompt or Linux BASH terminal and execute this commands:

Here is what you might see in the terminal:

Clone BotBuilder SDK in PowerShell

Once you settle the cloning, we can create the ASP.NET Core 2.0 Web API.

Remember: Bot API is just a Web API

Yes, it is just a normal Web API that you can create using ASP.NET Core 2 Web API template project. So, let’s do this. Open Visual Studio 2017 and create a new project. Make sure you choose the Web or .NET Core that has ASP.NET Core Web Application template.

Create New ASP.NET Core 2.0 Project

Then, choose Web API with ASP.NET Core 2.0 option.

Select Web API with ASP.NET Core 2.0 option

And we are done for adding the Web API. Next thing is to bring in the required BotBuilder SDK projects. I usually like to create a Solution Folder and put it there so we can easily navigate to the codes when needed. In my case, I create a BotBuilder solution folder and copy some of the projects from the ‘BotBuilder‘ folder where you already cloned it from GitHub previously. If you haven’t know how to do this, just Right-Click the Solution on the project explorer, choose Add… then select New Solution Folder. Here are the steps:

  1. Create a Solution Folder named ‘BotBuilder
  2. Right-Click the ‘BotBuilder‘ folder, choose Add… and then select Existing Project. Browse the ‘mybot‘ folder until you find Microsoft.Bot.Connector.AspNetCore2 project to add it.
  3. Also Right-Click the ‘BotBuilder‘ folder again, choose Add… and then select Existing Project. Browse the ‘mybot‘ folder until you find Microsoft.Bot.Connector.NetCore project to add it.
  4. Lastly Right-Click the ‘BotBuilder‘ folder again, choose Add… and then select Existing Project. Browse the ‘mybot‘ folder until you find Microsoft.Bot.Connector.Shared project to add it.
  5. Finally, just to make sure everything is okay, Right-Click the ‘BotBuilder‘ folder again and select Build to compile the codes. If it returns an error you might want to check what is the error first.

Here is what it looks like once you have added all the projects.

Add the Code

Now, let’s add codes for using the BotBuilder in the Web API. First thing, add the Bot Connector references to the Web API. Right-Click the Dependencies:

Adding References

And select the two projects, that is Microsoft.Bot.Connector.AspNetCore2 and Microsoft.Bot.Connector.NetCore that has shown as part of the solution.

And we can start adding codes. Firstly, on the Web API project in the Controllers folder delete the default template controller which is the ValuesControllers. Open the Startup.cs file and add the required services for the bot to work.

As you can see we need to add the MicrosoftAppCredentials.MicrosoftAppIdKey and MicrosoftAppCredentials.MicrosoftAppPasswordKey on the application settings (we will get the values later on). Open the appsettings.json file and add the required properties, "MicrosoftAppId" and "MicrosoftAppPassword".

The Message Controller

Once we have setup the middleware and configurations, we can now add the Message Controller for the bot. On the Controllers folder, add a new class named MessagesControllers.cs. And copy and paste this codes to the file:

And we are done. Compile all of the codes and we are set to deploy to Azure. If you want to test locally first using Bot Emulator, you can do this by running the project. Before you run the project, remove the Authorize(Roles ="Bot") from the Post method. This is to prevent the bot for authenticating to check the app and password identification. ASP.NET Core 2 has different of using authentication. The authentication middleware has a handler that handles for the app ID and password checking. So, it will only be one middleware for authentication, however you can set filters or policies through the handler. I’m not going to deep dive the middleware handler here right now, but if you are interested, you can see the source code at Microsoft.Bot.Connector.AspNetCore2 project.

Getting the Bot Application Id and Password

Once you are set with the solution, we can now create the Bot Application ID and deploy the application to Azure App Service. First things first, go to this website:

If you haven’t have the account, you need to sign in first, I use my Microsoft Account ( Once you logged in, go to ‘My Bots‘ page and clickCreate a Bot‘ button on the right. Click Create and make sure to choose ‘Register an existing bot built using Bot Builder SDK‘, see below for guidance.

Register an existing bot built using Bot Builder SDK

Fill in the blanks for the bot display name, etc. We can come again to this page anytime once we create the bot application. The most important is to create the bot application Id and get the password. Scroll down a bit to the Configuration section and click the Create Microsoft App ID and Password button.

Follow the instructions until you get the App ID and Password. Make sure you  save it on Notepad. Finish the Bot Registration. For the Messaging Endpoint, you can just fill anything first there until you deploy to Azure App Service and get the URL pointing to the API.

Deploying to Azure App Service

To deploy to Azure using Visual Studio is just a Right-Click away. Before we do that, open again the appsettings.json file and fill in the Microsoft App ID and the Password key. Save and compile. And on the ‘mybot‘ project, Right-Click and choose Published. Choose Microsoft Azure App Services and follow the instruction. You are done deploying here. Took me 2 minutes to deploy for the first time.

Once it has been deployed, you can get the endpoint URL, example Put this URL endpoint back to the Messaging Endpoint on the Configuration section when you create the Bot Application ID previously. And you are set to fire up the bot.

Source Code


Happy coding and chatting… Enjoy!

Please follow my Twitter: @rizamarhaban