Introduction

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;
  7. namespace StockBot2
  8. {
  9. public class YahooBot
  10. {
  11. public static async Task<double?> GetStockRateAsync(string StockSymbol)
  12. {
  13. try
  14. {
  15. string ServiceURL = $"http://finance.yahoo.com/d/quotes.csv?s={StockSymbol}&f=sl1d1nd";
  16. string ResultInCSV;
  17. using (WebClient client = new WebClient())
  18. {
  19. ResultInCSV = await client.DownloadStringTaskAsync(ServiceURL).ConfigureAwait(false);
  20. }
  21. var FirstLine = ResultInCSV.Split('\n')[0];
  22. var Price = FirstLine.Split(',')[1];
  23. if (Price != null && Price.Length >= 0)
  24. {
  25. double result;
  26. if (double.TryParse(Price, out result))
  27. {
  28. return result;
  29. }
  30. }
  31. return null;
  32. }
  33. catch (WebException ex)
  34. {
  35. //handle your exception here
  36. throw ex;
  37. }
  38. }
  39. }
  40. }
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. }
  12. }
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);
  6. // return our reply to the user
  7. return message.CreateReplyMessage(StockRateString);
  8. }
  9. else
  10. {
  11. return HandleSystemMessage(message);
  12. }
  13. }
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.
Output:

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: