Real Time Bot Project Using Microsoft Bot Framework


In my previous article "Creating a Simple Bot Application using Microsoft Bot Framework", I  explained how can we create a very simple Bot application using Microsoft Bot Framework.

In this article I will create a very simple real time Bot Application using Microsoft Bot Framework, that is Stock Bot. In this application I will use Yahoo's Finance API. I will pass a stock symbol as a message and I will get the current stock value of that particular company symbol.
Let's start step by step:

Step 1: Firstly, create a Bot Application,
Step 2: Create a class in your project with any name. I am giving Name as Yahoo Bot Application and writing the following code inside it.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Threading.Tasks;  
  6. using System.Web;  
  8. namespace StockBot2  
  9. {  
  10.     public class YahooBot  
  11.     {  
  12.         public static async Task<double?> GetStockRateAsync(string StockSymbol)  
  13.         {  
  14.             try  
  15.             {  
  16.                 string ServiceURL = $"{StockSymbol}&f=sl1d1nd";  
  17.                 string ResultInCSV;  
  18.                 using (WebClient client = new WebClient())  
  19.                 {  
  20.                     ResultInCSV = await client.DownloadStringTaskAsync(ServiceURL).ConfigureAwait(false);  
  21.                 }  
  22.                 var FirstLine = ResultInCSV.Split('\n')[0];  
  23.                 var Price = FirstLine.Split(',')[1];  
  24.                 if (Price != null && Price.Length >= 0)  
  25.                 {  
  26.                     double result;  
  27.                     if (double.TryParse(Price, out result))  
  28.                     {  
  29.                         return result;  
  30.                     }  
  31.                 }  
  32.                 return null;  
  33.             }  
  34.             catch (WebException ex)  
  35.             {  
  36.                 //handle your exception here  
  37.                 throw ex;  
  38.             }  
  39.         }  
  40.     }  
  41. }  
In the above code you can see a function that is GetStockRateAsync, which takes a StockSymbol as a parameter. I am calling Yahoo Finance API, where I will pass that symbol and that will return a CSV file of stock market rate, and I am splitting CSV File by comma because CSV File means Comma Separated Value. So, I am splitting by comma(,) and getting current price of that stock. Then I am converting it into double because of course our stock rate will be in double and returning current price.
Step 3: Now in MessagesController create a class that will call this above function., so I am creating  a GetStock function in My MessageController class.
GetStock function's Code
  1. private async Task<string> GetStock(string StockSymbol)  
  2. {  
  3.     double? dblStockValue = await YahooBot.GetStockRateAsync(StockSymbol);  
  4.     if(dblStockValue==null)  
  5.     {  
  6.         return string.Format("This \"{0}\" is not an valid stock symbol",StockSymbol);  
  7.     }  
  8.     else  
  9.     {  
  10.         return string.Format("Stock : {0}\n Price : {1}",StockSymbol,dblStockValue);  
  11.     }  
  13. }  
In above code you can see I am calling my GetStockRateAsync function that will return a nullable double. I am checking whether that stock value is null or not.
According to stock value I am returning appropriate string.
Step 4: Calling GetStock function from Post action of MessagesController.
Post Action's Code in MessagesController
  1. public async Task<Message> Post([FromBody]Message message)  
  2. {  
  3.     if (message.Type == "Message")  
  4.     {  
  5.         string StockRateString = await GetStock(message.Text);  
  7.         // return our reply to the user  
  8.         return message.CreateReplyMessage(StockRateString);  
  9.     }  
  10.     else  
  11.     {  
  12.         return HandleSystemMessage(message);  
  13.     }  
  14. }  
In above code you can see when a user will send a message that will be nothing but a symbol, GetStock function will be called and will return an appropriate message as a reply message.
Step 5: Run the project and simulate in Bot Framework Simulator.

Initial Simulator Window

If I send invalid input stock symbol

For Microsoft stock:

For Google stock:

For Apple Stock:

Conclusion: In this article we have created a very simple bot application that will take stock symbol as an Input and send an auto reply message of that particular stock rate.
Read more articles on Machine Learning