This is the final post in my blog post series, Building a Kentico E-Commerce Chat Bot. At long last I have finally had time to do a little code refactoring, wrap up the solution into a nice GitHub repository, and ensure that a test page to host the bot works here at Mcbeev.com.
First: Let's Try It Out
The chat bot should show up immediately on the right hand column of the page. The demo from Microsoft grabs focus on page load, so I have it at the top to avoid any crazy scrolling situations.
You can try the bot right away. Some instructions are below for how to get started. There are two intents available to use this chatbot. You can ask it to either check the order history for a customer or find an order tracking number for a given order number.
You can start the conversation simply by saying "Hi". From there you can use the following commands:
1. Enter the following: I'd like to check my order history.
For email address: firstname.lastname@example.org
For billing zipcode: 49401
2. Enter the following: I'd like to find my order tracking number.
For order number: 144
Ok, now let's explain how this all works.
In the last few parts of the series we focused on the configuration and setup of all of the puzzle pieces of the solution. There is one more step to enable the usage of the bot in the wild, which is somewhat optional, but I thought I would mention it. And that step is to add which channel you want to use the bot in. This is really one of the cool things about chatbots, you can use them on many different channels, all with the same code. I have enabled this bot on three channels, Slack, Skype, and Web. You can do this at the Bot Framework page. You simply add/enable which channel you want if it is not already there.
How It Works: The Code
Now I am going to assume you have all that setup correctly and focus on the code. The key to code is really understanding the three main parts, Run.csx, RootDialog.csx, and KenticoManager.csx.
Pro Tip: A .CSX file is a C# Script file. It was introduced with the Roslyn Compiler, and it is not full on C#. It's what you get with the Azure Bot Framework. If you want to run normal C# instead, there is a set of templates for that for Visual Studio, but my project does not use those templates.
Let's start with Run.csx. This file is really the "main" function in your project. It's where program flow starts. As you can see below, the core of what it does is work with the ActivityTypes that are passed by the framework to understand if the program should be creating a new conversation with a new user, or passing messages to an existing conversation. It is basically the brains of the operation.
If the program knows it is a new conversation, it creates the proper flow and passes control to the RootDialog.csx file. Below is a snippet from that file that shows how the LUIS intent is mapped to a Dialog. In our case a Dialog is the interface that controls what questions you want to ask the person that the bot is interacting with. Basically it is how you collect information from a user in the bot frame work. The key here is that you match the LuisIntent attribute to your intents that you have defined in LUIS (remember you did this back on step 4 of the blog post). For example, in the walk through we created an intent called Order History, and in my code sample I have defined the attribute as [LuisIntent("OrderHistory")].
Once we have the information collected via the Dialog process, we can then pass it to the Kentico REST API of the Kentico site that contains our e-commerce data. This is where the KenticoManager.csx file comes in.
Of course, there is a full set of configuration and settings required to make this all work besides those three files. But they are the most important ones.
If you are building your own bot you will want to make sure to update the various keys and configurations to your LUIS account and Kentico site. To do this, you would want to edit the main appsettings.json file and update the LuisAppId and LuisAPIKey keys with your own. plus update the various Kentico specific settings for Site Url, Username, and Password before it all works. However, remember that the Application Settings in your Azure Bot Service can override the ones in the file (yes... I lost two hours on this one...).
Full Source Code / Repository
I have created a GitHub repository that contains the full source code to the chatbot. It can be found at https://github.com/mcbeev/KenticoEcommerceBot. Feel free to grab a copy and make it better or use it in your work.
A quick note about running the sample project locally, you can't just click the play button and start debugging it. You have to open a developer command prompt to the location where the project is, and more specifiicaly, where the file debughost.cmd is. Debug Host is what will run the CSX files, and leverage the Azure Functions CLI to run the bot. You can then use the Bot Framework emulator to test your project. Once this is all setup, a break point can be placed as Visual Studio will attach to the Debug Host. Full instructions can be found here at the Bot Framework documentation. Yes this is kind of a pain in the ass, but once it is setup. It's pretty awesome.
Well I hope you have enjoyed this blog post series on Building A Kentico E-Commerce Chat Bot. I know this has been one of my favorites to work through. I really enjoy working with new technologies like this. There is actually a lot more that I could write about the topic, like debugging the solution locally through the Bot Framework Console editor, and running Azure functions locally through func.exe. If there is a lot of interest in this I could create a follow up post, just let me know through the comments.
If you made it all the way through the blog post series, thanks for reading!