public void NewMessage(string msg) { //verify sender user var senderName = Session["user"].ToString(); var sender = db.Users.Where(u => u.Username == senderName).FirstOrDefault(); //if it comes to this point means user session expired or something if (sender == null) { return; } //create msg obj Message newMessage = new Message() { Message1 = msg, Sender = sender.Id, TimeSent = DateTime.Now }; //send to all clients var context = GlobalHost.ConnectionManager.GetHubContext <chatHub>(); context.Clients.All.message(senderName, msg, newMessage.TimeSent.ToString("d/MMM HH:mm:ss")); //check it was a command for the bot const string stockBotCommandInit = "/stock="; if (msg.StartsWith(stockBotCommandInit)) { //get only the string after /stock= var stockCode = msg.RemoveFirstInstance(stockBotCommandInit); //send to api and get response StockBot bot = new StockBot(stockCode); var stockVal = bot.GetStock(); var header = "🤖 Stock bot"; var footer = DateTime.Now.ToString("d/MMM HH:mm:ss") + " | quote requested by " + senderName; if (!stockVal.Success) { stockVal.Content = "Error: " + stockVal.Content; } //send message to clients context.Clients.All.message(header, stockVal.Content, footer); } else { //add to db (only if i was not a command for the bot) db.Messages.Add(newMessage); db.SaveChanges(); } }
public void GetStockBadUrl() { StockBot bot = new StockBot(); string stockCode = "NOTEXISTS"; var mockMsgController = new Mock <IMessageController>(); var StockResponse = bot.GetStock(stockCode, "http://www.microsoft.com/", stockCodeToken, HttpMethod.Get, true, mockMsgController.Object, testEmail).Result; Assert.NotNull(StockResponse.Error); }
public void GetStockFail() { StockBot bot = new StockBot(); string stockCode = "NotExists"; var mockMsgController = new Mock <IMessageController>(); var StockResponse = bot.GetStock(stockCode, botUrl, stockCodeToken, HttpMethod.Get, true, mockMsgController.Object, testEmail).Result; Assert.True(StockResponse.StockValue == string.Empty); }