Emotion App Using Bot Framework

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.

  1. Here, we are integrating Cognitive Service Emotion API to Bot framework.
  2. 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.
  1. using System.Collections.Generic;  
  2. using System.Text;  
  3. using System.Net.Http.Headers;  
  4. using Newtonsoft.Json;  
  1. public async Task<HttpResponseMessage> Post([FromBody]Activity activity)  
  2.         {  
  3.             var connector = new ConnectorClient(new Uri(activity.ServiceUrl));  
  4.   
  5.             if (activity.Type == ActivityTypes.Message)  
  6.             {  
  7.                 const string apiKey = "<YOUR API KEY FROM MICROSOFT.COM/COGNITIVE>";  
  8.                 const string queryUri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";  
  9.   
  10.                 var client = new HttpClient  
  11.                 {  
  12.                     DefaultRequestHeaders = {  
  13.                 {"Ocp-Apim-Subscription-Key", apiKey},  
  14.                 {"Accept""application/json"}  
  15.             }  
  16.                 };  
  17.                 var sentimentInput = new BatchInput  
  18.                 {  
  19.                     Documents = new List<DocumentInput> {  
  20.                 new DocumentInput {  
  21.                     Id = 1,  
  22.                     Text = activity.Text,  
  23.                 }  
  24.             }  
  25.                 };  
  26.                 var json = JsonConvert.SerializeObject(sentimentInput);  
  27.                 var sentimentPost = await client.PostAsync(queryUri, new StringContent(json, Encoding.UTF8, "application/json"));  
  28.                 var sentimentRawResponse = await sentimentPost.Content.ReadAsStringAsync();  
  29.                 var sentimentJsonResponse = JsonConvert.DeserializeObject<BatchResult>(sentimentRawResponse);  
  30.                 var sentimentScore = sentimentJsonResponse?.documents?.FirstOrDefault()?.Score ?? 0;  
  31.   
  32.                 string message;  
  33.                 if (sentimentScore > 0.7)  
  34.                 {  
  35.                     message = $"That's great to hear!";  
  36.                 }  
  37.                 else if (sentimentScore < 0.3)  
  38.                 {  
  39.                     message = $"I'm sorry to hear that...";  
  40.                 }  
  41.                 else      
  42.                 {  
  43.                     message = $"I see...";  
  44.                 }  
  45.                 var reply = activity.CreateReply(message);  
  46.                 await connector.Conversations.ReplyToActivityAsync(reply);  
  47.             }  
  48.             else  
  49.             {  
  50.                 //add code to handle errors, or non-messaging activities  
  51.             }  
  52.             var response = Request.CreateResponse(HttpStatusCode.OK);  
  53.             return response;  
  54.         }  

Create a new code file called TextAnalyticsCall.cs and add the following code.

  1. public class BatchInput  
  2.     {  
  3.         internal List<DocumentInput> Documents;  
  4.   
  5.         public List<DocumentInput> documents { getset; }  
  6.     }  
  7.     public class DocumentInput  
  8.     {  
  9.         internal int Id;  
  10.   
  11.         public double id { getset; }  
  12.         public string Text { getinternal set; }  
  13.         public string text { getset; }  
  14.     }  
  15.   
  16.     // Classes to store the result from the sentiment analysis  
  17.     public class BatchResult  
  18.     {  
  19.         public List<DocumentResult> documents { getset; }  
  20.     }  
  21.     public class DocumentResult  
  22.     {  
  23.         public double score { getset; }  
  24.         public string id { getset; }  
  25.         public int Score { getinternal set; }  
  26.     }  

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.


Similar Articles