Integrating Machine Learning APIs in Microsoft Bot framework Apps

bot, is a software application that runs automated tasks over the Internet. Typically, bots perform tasks that are both simple and structurally repetitive, at a much higher rate than would be possible for a human alone. The largest use of bots is in searching, in which an automated script fetches, analyzes and files information from web servers at many times the speed of a human.

In this blog we will guide the reader in creating a bot that will enable you to quickly do data analytics. e.g. to begin with steps for doing text based sentiment analysis.

Solution being developed using following Microsoft Technologies:
1. Microsoft Bot Framework
2. Machine Learning APIs for Sentiment analysis

Here are the steps:
Microsoft Bot framework application

1. Add a Bot template in Visual Studio 2015 Community edition as per instructions given from this link

2. Create a bot template based Visual Studio C# solution using the downloaded template. add and update the following code to the template created code to update the messages:

public class MessagesController : ApiController{
/// POST: api/Messages
/// Receive a message from a user and reply to it
public async Task Post([FromBody]Message message) {
if (message.Type == "Message") {
// calculate something for us to return
int length = (message.Text ?? string.Empty).Length;
string sentimentResponse = GetSentimentResponse.GetSentiment(message.Text);
return message.CreateReplyMessage($"Your message {length}. " + sentimentResponse);
else {
return HandleSystemMessage(message);

3. Using bot emulator ensure that bot is responding to messages as per the code. Now that the bot is created we will integrate it in Skype so that we can use an existing application that makes our bot immediately usable Refer to this link for registering the bot with Skype.

Machine Learning APIs for Sentiment analysis

4. Next step is to integrate the Microsoft Machine API’s check this link for details:

5. The user text (e.g. entered in Skype) is used for sentiment analysis as shown in the code below:
public static String GetSentiment(string message) {
try {
string myComment = message.Replace(' ', '+');
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

string key = "AccountKey:" + "";
byte[] creds = System.Text.Encoding.UTF8.GetBytes(key);
client.DefaultRequestHeaders.Add( "Authorization", "Basic "+ System.Text.Encoding.UTF8.GetString(creds));
HttpResponseMessage response = client.GetAsync("" + myComment).Result;
if (response.IsSuccessStatusCode) {
var data = response.Content.ReadAsStringAsync().Result;
var product = JsonConvert.DeserializeObject(data);
return $"Your sentiment is {product.Score}. Have a good time";
else message = response.ReasonPhrase;
catch (Exception ex) {
return message;

6. This code demonstrates how we can integrated Machine Learning API using the Microsoft bot framework

Many More such use case can be integrated similarly e.g. SMS being sent for some specific activity in a Industrial establishment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s