Have you ever come across a bot assisting you in ordering pizza? or a bot suggesting you some great food options ? or even a bot that would schedule appointments and can help you out with your numerous questions? If answer to any of these questions is ‘ YES ’ , why not build a chatbot that would act as agent or your virtual friend.
Chatbots are everywhere and the way they are serving customers promptly makes it even more effective when compared to human counterparts. As many people prefer texting over voice , this is a best time to get your hand dirty by building a chatbot and dive into the world of ever growing conversation platform . Once you have developed a chatbot you can then host it on various chat channels and increase your revenue by serving customers in no time.
There are numerous platforms where you can start building bots and one such platform is Microsoft Bot Framework.
Why Microsoft Bot Framework?
Bot Framework provides its developers with extensive SDK for developing complex conversations and it’s integration with cognitive services in Microsoft Azure helps you in leveraging state of art machine learning services which empowers your bot to understand user voice, utterances, read text from images and much more..
Bot Builder Template:
Bot Framework provides us with a basic template that helps developers to build bot’s more quickly. This template comes with reference for essential libraries so that developers don’t have to tweak into basics to start building a bot every time.
You can click on the link and install the template for visual studio 2017 here
Create Your First Bot Sample Project:
Let’s now open Visual Studio 2017 or later.
Click on File > New Project > now search for bot in the top right corner .
Select EchoBot and name your bot with a desired name and click on “ok”
Template Window in Visual Studio 2017
Once your project gets created you will see list of files as below
Solution Explorer in Visual Studio 2017
Now once our project is ready, we are almost ready to code our first bot but before we proceed any further we need to know few terminologies that are used in Bot Framework.
Bot Builder v4 SDK basics
Turn:
In a conversation, people often speak one-at-a-time, taking turns speaking. With a bot, it generally reacts to user input. Within the Bot Framework SDK, a turn consists of the user’s incoming activity to the bot and any activity the bot sends back to the user as an immediate response. You can think of a turn as the processing associated with the arrival of a given activity.
Turn Context
The turn context is one of the most important abstractions in the SDK. Not only does it carry the inbound activity(message from user to bot) to all the middleware components and the application logic but it also provides the mechanism whereby the middleware components and the application logic can send outbound activities (message from bot to user)
Activity
Every message in an conversation between bot & user is called activity. Activity is important as it holds much information about channel being used to talk to bot, user information, bot information, service URL, time of message and much more.
In this article we mainly deal with Text property of Activity as it contains utterance given by user to the bot.
We are finally done with theory, Let’s go back to our code.
Code Explanation:
Let’s open EchoBotBot.cs file. If you notice this class inherits IBot Interface and is responsible for handling incoming activities.In here we have a method named OnTurnAsync which accepts turnContext and cancellationToken . This method is most important as it helps you create dialogs and then call child dialogs(we aren’t going to discuss about dialogs but it’s good to keep a note of it.)
Now let’s slow down! We need not to go around with all code present in the method as of now but I will explain you the overview operations of this method. This method performs 3 operations
Check’s if the Activity is of type Message.
Get’s and Set’s State Accessors.
Create a Response appending user utterance and respond to user.
We will be mostly focused on 1st and 3rd operation in this method. If activity is type of message then we print the user message present in turnContext.Activity.Text property by creating a string and send it in turnContext.SendActivityAsync method to reply back to user(comments are self-explanatory). Below are the lines that perform this operation as discussed.
You can run the bot application by hitting F5 or by clicking on Green Arrow as shown below
Once we’ve started process of debugging, visual studio hosts our bot on IIS in our local machine and we will be redirected to a page in browser to a local endpoint in my case localhost:3978, where 3978 is port number (Please make a note of port number as it will be used later)
As our bot is now hosted on local machine , let’s start debugging our bot .
In order to debug locally we need to install Microsoft Bot Framework Emulator which can be downloaded here(at this time v4.2.1 is latest).
Open bot framework Emulator and then click on File>New Bot Configuration and provide below options:
Bot Name: (name of the bot) — any desired name you wish.
Endpoint : (http://localhost:{Your Port Number}/api/messages)
We need to add ‘/api/messages ’ as it is the endpoint where our bot locally listens to.
In order to test our bot, let’s send a message to our bot. This message is termed as an activity of type message and turnContext object keeps a track of this activity in OnTurnAsync method. So we can access our message using Text property of Activity .
We are done! Congratulations you have successfully created a echo bot using Microsoft Bot Framework.