Introduction to Bot Framework
At Build 2016, Microsoft released bot framework to develop intelligent applications. In modern application development, there are a lot of services available as communication channels, such as Skype, Facebook Messenger, Telegram, and other such applications. For example, Discuss is a service company which provides comment plugin for developers to add in the websites or in their blogs.
During the release, I thought how Microsoft bot Framework Service will work. Now, I realize that bot framework has many advantages. Services may communicate with each other without knowing their states or API or event, without knowing the destination. In Windows 10 Operating System, Cortana is the best feature (from my point of view) and Skype with Cortana integration plays a major role in gathering information, such as news, weather, tips, and tricks etc.
Here, we are using communication channels, such as Email, GroupMe, Skype, Slack as an infrastructure for developing real-time applications.
The examples for real-time applications are -
- Hotel Booking System
- Online food booking system
- Taxi booking system.
Following are some of the applications with large data centers, such as Facebook, Twitter, Mail services etc.
Following are some of the benefits of bots.
- Connecting with cross-platform services.
- Your customer is your business.
- Go Server-less.
To build bot applications, we can use .NET SDK and Node.js SDK. These SDKs provide features for building applications with dialogues and built-in prompts that make an application more interactive. These applications are very easy to use and interactive with real-time applications. REST API are used rather than .NET SDK and Node.js SDK by the users who are developing applications.
Each and every bot provides some sense. This may be incorporated with LUIS for Natural Language understanding. For more information regarding bot intelligence, click here.
Bot connector
Bot connector acts as a mediator between bots and channels. Bots are infrastructure services . This service provides a connection between various channels, such as Skype, Email, GroupMe, Slack etc. Here, we are going to develop an application using Visual Studio in C# project template. So, we are familiar with C#. Then, it is going to be very easy. This developed application is hosted by using Microsoft Azure web app.
Whole bot architecture is based on REST, so there are no constraints regarding application development framework and hosting framework. Bots are configured using end points which are accessible only by using bot emulator in which output is retrieved.
Bot Emulator
Bot framework application output is rendered only by using Emulator. For connecting an application, we need a port number and URL which is available in a web browser. Our application is deployed only in bot emulator, not in a web browser. So, don’t forget to install Bot framework. The installation procedure is very simple and easy to use. To download bot emulator, click here.
Bot Builder
Bot builder is a factor which comes with several classes, functions, and extension methods. If you are using Visual Studio, you will be able to download using NuGet Package Manager. To download bot builder, click here.
Emotion App using Bot Framework
Requirements
- Visual Studio 2015 update 1 or higher.
- Bot Emulator
Download Bot Template
Now, it's time to download bot emulator. For this, click here.
Once the download completes, save
the file at the following location.
%USERPROFILE%\Documents\VisualStudio2015\Templates\ProjectTemplates\Visual
C#
Open Visual Studio.
Click New >> Visual C# >> Bot Application. Then, provide the name of the application and click Create button. It takes some time to create an application.
Before coding, copy the Emotion API key from Cognitive Service official site. It’s a free account. You will be able to get two keys, as shown below. Click here to get Emotion API key.
Here, we are developing an application for displaying an emotion using Cognitive Emotion API. It displays output using Bot framework.
- Here, we are integrating Cognitive Service Emotion API to Bot framework.
- Shows emotion, such as "That’s great to hear, I am sorry to hear, I see an output."
Now, add the following code in MessagesController.cs and add the following.
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Http.Headers;
- using Newtonsoft.Json;
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
-
- if (activity.Type == ActivityTypes.Message)
- {
- const string apiKey = "<YOUR API KEY FROM MICROSOFT.COM/COGNITIVE>";
- const string queryUri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";
-
- var client = new HttpClient
- {
- DefaultRequestHeaders = {
- {"Ocp-Apim-Subscription-Key", apiKey},
- {"Accept", "application/json"}
- }
- };
- var sentimentInput = new BatchInput
- {
- Documents = new List<DocumentInput> {
- new DocumentInput {
- Id = 1,
- Text = activity.Text,
- }
- }
- };
- var json = JsonConvert.SerializeObject(sentimentInput);
- var sentimentPost = await client.PostAsync(queryUri, new StringContent(json, Encoding.UTF8, "application/json"));
- var sentimentRawResponse = await sentimentPost.Content.ReadAsStringAsync();
- var sentimentJsonResponse = JsonConvert.DeserializeObject<BatchResult>(sentimentRawResponse);
- var sentimentScore = sentimentJsonResponse?.documents?.FirstOrDefault()?.Score ?? 0;
-
- string message;
- if (sentimentScore > 0.7)
- {
- message = $"That's great to hear!";
- }
- else if (sentimentScore < 0.3)
- {
- message = $"I'm sorry to hear that...";
- }
- else
- {
- message = $"I see...";
- }
- var reply = activity.CreateReply(message);
- await connector.Conversations.ReplyToActivityAsync(reply);
- }
- else
- {
-
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
Create a new code file called TextAnalyticsCall.cs and add the following code.
- public class BatchInput
- {
- internal List<DocumentInput> Documents;
-
- public List<DocumentInput> documents { get; set; }
- }
- public class DocumentInput
- {
- internal int Id;
-
- public double id { get; set; }
- public string Text { get; internal set; }
- public string text { get; set; }
- }
-
-
- public class BatchResult
- {
- public List<DocumentResult> documents { get; set; }
- }
- public class DocumentResult
- {
- public double score { get; set; }
- public string id { get; set; }
- public int Score { get; internal set; }
- }
Click F5 button to deploy an application. Once the application is deployed, we will be navigated to the web browser to render output.
Now, open Bot Emulator. You need the following.
The URI for your bot application localhost: <port> and add the following path "/api/messages" to your URL using bot application.
Don’t fill Microsoft App ID and password. click enter to connect.
Finally, the output is displayed, as given below.
Download Emotion application from Github. To download, click here.
Summary
In this article, we discussed about bot framework and the process of developing an emotion app. I hope you enjoyed reading this article. Thanks for reading.