IntroductionIn 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.
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- using System.Web;
-
- namespace StockBot2
- {
- public class YahooBot
- {
- public static async Task<double?> GetStockRateAsync(string StockSymbol)
- {
- try
- {
- string ServiceURL = $"http://finance.yahoo.com/d/quotes.csv?s={StockSymbol}&f=sl1d1nd";
- string ResultInCSV;
- using (WebClient client = new WebClient())
- {
- ResultInCSV = await client.DownloadStringTaskAsync(ServiceURL).ConfigureAwait(false);
- }
- var FirstLine = ResultInCSV.Split('\n')[0];
- var Price = FirstLine.Split(',')[1];
- if (Price != null && Price.Length >= 0)
- {
- double result;
- if (double.TryParse(Price, out result))
- {
- return result;
- }
- }
- return null;
- }
- catch (WebException ex)
- {
-
- throw ex;
- }
- }
- }
- }
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
- private async Task<string> GetStock(string StockSymbol)
- {
- double? dblStockValue = await YahooBot.GetStockRateAsync(StockSymbol);
- if(dblStockValue==null)
- {
- return string.Format("This \"{0}\" is not an valid stock symbol",StockSymbol);
- }
- else
- {
- return string.Format("Stock : {0}\n Price : {1}",StockSymbol,dblStockValue);
- }
-
- }
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
- public async Task<Message> Post([FromBody]Message message)
- {
- if (message.Type == "Message")
- {
- string StockRateString = await GetStock(message.Text);
-
-
- return message.CreateReplyMessage(StockRateString);
- }
- else
- {
- return HandleSystemMessage(message);
- }
- }
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: