Skip to main content

Microsoft Bot Framework Basics: Building Intelligent Bots (Part 1)

Scope
The following article introduces the concept of bots, demonstrates the basics of the Microsoft Bot framework and showcase how to build your first bot using the Bot Framework and C#.

Introduction

Conversational Applications or bots is a hot topic right now, is it really useful or it's just a hype? To answer this question, let's look at scenarios where a bots are useful. Messaging Applications.
According to Business Insider, text Messaging is the most used smartphone feature and for the first time in history, people are using more messaging apps than social networks.


What if you could interact with your application via this interface? It is possible via bots! Moreover, bots can adapt and work on most devices and platforms like desktop, mobile, or social media like Facebook, WhatsApp or Skype. This will give your app a totally new selling point by interacting with your user using his favorite communication method and using his favorite platform!

Automating Repetitive Tasks


In scenarios where there's repetitive tasks such as a call center, bots can easily be used since there's lots of 'generic' answers. The flow as described below would be to handle the generic and most common requests and when there's a more difficult query, the bot transfers the call to a human.

Getting Started

One of the tools to quickly build bots is the Microsoft Bot Framework. It is an Open source Bot Builder SDK which allows you to build simple to sophisticated dialogs; Cognitive Services enable your bot to see, hear, interpret and interact in more human ways. To get started with the Microsoft Bot Framework, you must first download the Bot Builder SDK here  

Create the project


Follow steps below to set up the bot framework connector SDK .NET template in the Visual Studio 2015.
1. Install Visual Studio 2015 (latest version)
2. Download bot application template from this download link here
3. Save the zip file to your Visual Studio 2015 templates directory which is traditionally in "%USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#\". For example, "C:\Users\USERXX\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#"



In the Web.Config file, make sure the keys are as follows for debugging and run the project. These will need to be changed when deploying the bot.

<appSettings>
    <!-- update these with your BotId, Microsoft App Id and your Microsoft App Password-->
    <add key="BotId" value="YourBotId" />
    <add key="MicrosoftAppId" value="" />
    <add key="MicrosoftAppPassword" value="" />
</appSettings>

Download & Configure the Bot Emulator


Download the emulator here  . Once the emulator is installed, copy the link of the running application in your browser and paste it in the emulator, leave the ID and Password blank and click on connect. The URL in the emulator must be in the format http://xxx:port/api/messages


Once you click on connect, when you check the Log, it should return status 200, meaning that the connection has been established.

Microsoft Bot Framework Basics

Let's go back to the Bot Framework project to examine the code. The class here is the MessageController, this is where all the communication between the bot and the user will be handled.

All the input received from the user will go through the Post method where the type of the activity will be identified which will decide how to reply the user.
The main types of Activity are:
1. Message - A simple communication between a user <-> bot2. ConversationUpdate - Your bot was added to a conversation or other conversation metadata changed
3. ContactRelationUpdate - The bot was added to or removed from a user's contact list
4. Typing - The user or bot on the other end of the conversation is typing
5. Ping - An activity sent to test the security of a bot.
6. DeleteUserData - A user has requested for the bot to delete any profile / user data
Therefore, in the post method, you will check the ActivityType. If it is of type message, the bot will send a reply to the user and output the length of the text that he sent.


if (activity.Type == ActivityTypes.Message)
{
     //default
     var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
     var reply = activity.CreateReply();
     reply.Text = "You typed " + activity.Text;
     await connector.Conversations.ReplyToActivityAsync(reply);
}
Let's amend the code to send a welcome message to the user when he joins the conversation and triggers aConversationUpdate activity.

else if (message.Type == ActivityTypes.ConversationUpdate)
{
    var connector = new ConnectorClient(new Uri(message.ServiceUrl));
    var response = message.CreateReply();
    response.Text = "Hi there! I'm chervine's bot! How can I help you today? ";
    await connector.Conversations.ReplyToActivityAsync(response);   
}
Now, let's run the code and see the results in the emulator.

As expected, when a user joins the conversation, the bot sends a welcome message and when the user sends a message, the bots replies with the number of characters in the input message.

Conclusion

The article demonstrated how to get started with Bot Framework and how to build your first bot. In the next article, the use of LUIS to add language understanding capability to the bot will be demonstrated. 

References

  1. Bot Framework - Activities 
  2. Messaging Apps are now bigger than Social Network – Business Insider 

Comments

Popular posts from this blog

Creating and Querying Microsoft Azure DocumentDB

DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.

This article is available available on theMicrosoft Technet Wiki. This article was highlighted in theTop Contributor awardson the 12th of October 2014. This article was highlighted in the TNWiki Article Spotlight. This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.
This wiki shall introduce you to this new service.

Setting up a Microsoft Azure DocumentDBGo to the new Microsoft Azure Portal. https://portal.azure.com/ 


 Click on New > DocumentDB


Enter A Database ID and hit Create!



Query Unstructured Data From SQL Server Using PolyBase

Scope The following article demonstrates how unstructured data and relational data can be queried, joined and processed in a single query using PolyBase, a new feature in SQL Server 2016. Pre-RequisitesIntroduction to Big Data Analytics Using Microsoft Azure Big Data Analytics Using Hive on Microsoft Azure Analyze Twitter Data With Hive in Azure HDInsight Running Hadoop on Linux using Azure HDInsight  Introduction Traditionally, Big Data is processed using Apache Hadoop which is totally fine. But what if the result of this needs to be linked to the traditional Relation Database? For example, assume that from the analysis of tons of application logs, marketing needs to contact some customs that faced problems in an application following a failure in the application.
This problem is solved with PolyBase. PolyBase allows you to use Transact-SQL (T-SQL) statements to access data stored in Hadoop or Azure Blob Storage and query it in an ad-hoc fashion. It also lets you query semi-structure…

Creating and Deploying Microsoft Azure WebJobs

Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.
This article focuses on the basics of WebJobs before demonstrating an example where it can be used.

This article is also available on the Mirosoft TechNet Wiki.
This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


Introduction
What is Microsoft Azure WebJobs?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.

What Scheduling Options is supported by Microsoft Azure WebJobs? Azure WebJobs can run Continuously, On Demand or on a Schedule.
In what language/scripts are WebJobs written?
Azure WebJobs can be created using the following scripts:  .cmd, .bat, .exe (using windows cmd).ps1 (using powershell).sh (using bash).php (using php).py (using python).js (using node)In this article, the use of c# command line app shall be demonstrated.
Cr…