1. Home
  2. Training Library
  3. Microsoft Azure
  4. Courses
  5. Creating a Chatbot Using the Azure Bot Framework SDK

Creating a Bot with Visual Studio

Start course
Overview
Difficulty
Intermediate
Duration
52m
Students
110
Ratings
4.3/5
starstarstarstarstar-half
Description

This course shows you the fundamentals of how to design, create, and test chatbots using the Azure Bot Framework SDK. You’ll learn about the Bot Framework SDK, the key components that form a chatbot, and how to use templates in Visual Studio to create chatbots.

We'll help you understand the importance of Turn Context and State Management and how these are used in chatbots with the Bot Framework SDK. We'll also cover the dialog library, different types of dialogs, and how you can use these to model conversational logic in your chatbots.

The course wraps up by showing you how to get your chatbot published in Azure and test your chatbot using the Azure Portal.

Learning Objectives

  • Using the Bot Framework SDK to create a chatbot
  • Implementing Dialogs and State Management
  • Testing chatbots using the Bot Framework Emulator
  • Deploying a chatbot to Azure
  • Testing a chatbot in Azure

Intended Audience

This course is intended for developers and software architects who want to learn more about the Bot Framework SDK and how it can be used to create conversational AI solutions in Microsoft Azure.

Prerequisites

Intermediate knowledge of C# is required for this course. You’ll also need to be familiar with Azure concepts such as App Service and Azure Key Vault, and be comfortable using Microsoft Visual Studio Community edition.

Transcript

Let's see what's involved when creating Bots with Visual Studio. In this lecture, we'll look at what's involved in building Chat Bots. We'll look at the higher level processes that are typically involved. We'll see some of the Bot Framework Templates that are available. We'll look at what's involved when using the Echo Bot template and we'll take a closer look at Activity Handlers. And finally, we'll look at the demo of the Echo Bot in action with an Activity Handler.

Let's look at a typical process for building bots. First, we'll look at the key steps, plan, build, test, publish, and connect. The first thing you want to do when building your chat bot, is to map the solution out. Consider what types of tasks your chat bot will perform. Who will it need to serve for example? You'll also want to consider what types of processes your chat bot should contain. For example, will your chat bot have to make a call to our web API to fetch external data for processing?

Another important decision to make during the planning phase is to decide which channels you'll make your chat bot available on. Will your chat bot be hosted just on a website or do you want to expose your conversational AI over the smart device? After considering what your chat bot will do, the processes it may contain, and where you want to surface your solution. You can then start to think about how you may want to build it. Your chat bot is effectively a web service with a conversational interface that communicates with the bot framework service.

The bot framework SDK, has support for C-sharp, Java, Python and JavaScript. You can use any of these languages to create the dialogues and conversational logic that you need your chat bot to contain. You may also choose to add natural language processing to let your bot understand natural language and recognize human intent. You can further augment the intelligence of your chat bot by introducing Azure Cognitive Services. For example, use a sentiment analysis and opinion mining capabilities of Azure Cognitive Services to better understand the underlying emotion being expressed by a person interacting with your chat bot. 

Your chat bot is a software application and will consist of various components working together. You'll likely have multiple dialogues or integrations with cloud services and custom code. Just like any other software application, you'll need to test your chat bot before you publish it. Whilst building your chat bot, you can test it locally by using the bot Freemark Emulator. The Emulator is a stand-alone outlet you can launch in your local machine whilst the development instance of your chat bot is running.

Use the debugging capabilities to help you better understand your chat Bots behavior, and then spec the messages that make sense. Test your chat bot on the web. This will involve activating the web chat channel on the Azure portal. This is a good way to give people outside of your development team, access to your chat bot. For example, test analysts. Another way for you to ensure your bot is robust is to rate automated unit tests against your custom code.

One thing to keep in mind is that you may need to introduce a third-party library such as Moq, when rating unit tests against bot framework specific SDK classes. After you've built and tested your chat bot, the next part of the process involves publishing it and making it available in Microsoft Azure. You'll then want to consider the channels that you want to activate it on. Publishing your chat bot gives it a public address on the internet for the devices or applications to connect to.

With your chat bot published, you can now activate channels such as Facebook Messenger, WhatsApp by Microsoft teams and the Azure portal. Take time to evaluate how your chat bot performs in each of these channels. Use data collected in the Azure portal to identify the performance of your chat bot or to help you identify the most, and least popular dialogues. This is also a good time to get feedback from users.

Let's look at what's involved in creating the Echo Bot. The bot framework SDKs ships with templates that help you accelerate the development of your chat bot. Support for C-sharp, JavaScript, and Python is available. Each template provides you with a default end point to send incoming activities to the default adapter. Templates also contain a chat bot class. This encapsulates all necessary logic for you to run the template.

At the time of this course, there are four main templates to help get you started with building chat Bots. The Empty Bot, the Echo Bot, the Core Bot and the Enterprise Bot. The Empty Bot and Echo Bot templates will help you understand the fundamentals, components, and the events that will occur within a chat bot. The Core and Enterprise Bot templates contain more cord, dialogues, and integrations with cloud services, such as the natural language processing service LUIS.

Use the Core and Enterprise bot to help you understand best practices and accelerate your development process. Each of these templates are made available to you when creating a new project. That's what we can see here. Selecting the Echo Bot project will create all the necessarily files, needed to run a simple chat bot. We can see an example of an Echo Bot template here. Here, we can see the bot class, Echo Bot, and here we can see that two key events in the Activity Handler are being overridden.

Here we can see the default end point the Echo Bot template creates. the bot controller is where incoming messages to the bot are received. The adapter then sends this message onto the chat bot. You run a chat bot like any regular application in your development environment. The Echo Bot, and all other templates that share with the bot framework, STK, launch a welcome page to let you know that your chat bot is running. The welcome message also contains some useful instructions that let you know how to connect to your chat bot. With your chat bot template running you can then use the emulator to connect to the end point on your local machine. When the connection is established, you can enter that with your chat bot and test the functionality.

Let's take a closer look at Activity Handlers. The Activity Handler lets you organize the conversational logic for your chat bot. Different types of activity, or message, that your chat bot receives, will cause the Activity Handler to read different types of conversational events. For example, you made the site to implement code in the Activity Handle that sends a farewell message when the user ends a conversation. Other conversational events you can listen to include, when a message is received, when a person joins a conversation, or when a reaction is received from a user.

Use these events to build rich conversational experiences in your chat bot. You can find a list of all the event types you can subscribe to at the following URL. The method OnTurnAsync within the Activity Handler is an important method to call out. This is one of the first methods that runs for any activity tape that was received by a chat bot. It can be a useful place for you to handle the loading or saving of state data for variables. You may also want to use this method to introduce a logging feature, to record each message your chat bot receives.

Messages that pass through the OnTurnAsync method go on to call one of the other Activity Handlers based on the type of activity that is being received. We can see an example of the OnTurnAsync method within the activity high and low beam override in here. In this example, we're simply taking the conversation ID from the incoming activity and concatenating the activity text. The output string is the incentive of the debugger. It's important to note that when overriding the OnTurnAsync method, that you must also call the base implementation. That's what we can see here, doing this will ensure that other activity handles are called.

It's time to look at a demo. In this demo we'll extend the Echo Bot template. We'll create a method that returns an appropriate response based on the user input. This method will be placed within the activity hands-on event, OnMessageActivityAsync. So, we're in visual studio and here we're looking at the default Echo Bot template. We can see that we've got the bot class inheriting from the Activity Handler and we're overrating two of the key methods. OnMessageActivityAsync and OnMembersAddedAsync.

What we have to do is intercept the message from the person who entered text with the chat bot. And we will do that by adding some logic into the method OnMessageActivityAsync. So the first thing that we have to do, is to take away the Echo statement, because we don't really want to echo back that information to the person. So at this point, the incoming message from the person will still be sent back.

We want to add a method that will take that incoming messages as our parameter and output a response. So we've got one here in the clipboard. Let's make the process: message accept, and then coming string based on the value, if it's "hello" or "bye" or something completely different, one of three responses will be sent back to the user. It will either be greetings, "Was it something I said" or, "Sorry, I don't understand that, I'm still learning". So what we'll do is we'll call that method here and we will call, we will call, reply text, we'll set that equal to the incoming... Message, and the activity. And then down at line 40, what we'll see is the reply text, from the output of process message being sent back to the user.

So at this point, we've updated the OnMessageActivityAsync method to include a call to this private method process message. We can run this chat bot by simply clicking on "run". At which point the welcome page for this chat bot will be displayed. and then what we can do, is to start up the emulator. So we've got the emulator running here. We'll just make that a little bit smaller, and we get the welcome message for this chat bot. And we can see over here, this is the welcome message, down at line 47, this particular method, OnMembersAddedAsync, gets run when you first connect to the chat bot, that's why we get that message.

So we can test our new function, which is ProcessMessage by typing in "Hello". And we get the response of greetings. We can also say "bye", we get the other message "was it something I said?" and we can try something completely different and see. "What can you do?" And we get, "sorry, I don't understand. I'm still learning". So what we've seen there is being able to intercept the incoming message from my user, handle it using a custom function, and then sending back an appropriate response.

Now, this is a very simple chat bot, but it does show you some of the key concepts. If you want to handle more complex queries or interactions. You would have to start looking towards something like LUIS, which is the natural language understanding service that Microsoft has.

About the Author

Jamie Maguire is a Software Architect, Developer, Microsoft MVP (AI), and lifelong tech enthusiast with over 20 years of professional experience.

Jamie is passionate about using AI technologies to help advance systems in a wide range of organizations. 

He has collaborated on many projects including working with Twitter, National Geographic, and the University of Michigan. Jamie is a keen contributor to the technology community and has gained global recognition for articles he has written and software he has built. 

He is a STEM Ambassador and Code Club volunteer, inspiring interest at grassroots level. Jamie shares his story and expertise at speaking events, on social media, and through podcast interviews. 

He has co-authored a book with 16 fellow MVPs demonstrating how Microsoft AI can be used in the real world and regularly publishes material to encourage and promote the use of AI and .NET technologies.