public async Task <ServiceModel> GetOpportunities(OpportunitiesRequestModel model) { var uriBase = ConfigurationManager.AppSettings["opportunityAPI"]; var uri = new Uri(uriBase + "?" + GetQueryString(model)); var result = await Client.GetAsync(uri); var stringbuilder = new StringBuilder(); var content = await result.Content.ReadAsStringAsync(); var obj = JArray.Parse(content); stringbuilder.AppendLine(model.Message); foreach (var o in obj) { stringbuilder.Append( $" * {o["Subject"]} from {o["BusinessAccount"]} for {Convert.ToDouble(o["Total"]).ToString("c0")}, estimated by {o["Estimation"]}\r\n"); } var debugInfo = new StringBuilder(); debugInfo.AppendLine($"DEBUG: Opportunities API RequestOpportunitiesRequestModel: \r\n{JsonConvert.SerializeObject(model)} \r\n"); debugInfo.AppendLine($"DEBUG: Opportunities API Url: *{uri}*\r\n"); return(new ServiceModel { Message = stringbuilder.ToString(), DebugMessage = debugInfo.ToString() }); }
public async Task OppsByTotal(IDialogContext context, LuisResult result) { var money = Regex.Replace(result.Entities.FirstOrDefault(e => e.Type == "builtin.money").Entity, "[^0-9.]", ""); var greaterThan = true; var item = result.Entities.FirstOrDefault(e => e.Type == "lessThan"); if (item?.Entity != null) { greaterThan = false; } var model = new OpportunitiesRequestModel { Intent = OpportunitiesIntents.OppsByTotal, Amount = Convert.ToDouble(money), GreaterOrLessThan = greaterThan }; model.Message = $"Here are the opportunties **_{(greaterThan ? "greater" : "less")} than {model.Amount.ToString("C0")}_**"; var serviceCall = await service.GetOpportunities(model); await context.PostAsync(serviceCall.Message); await WriteDebug(context, "DEBUG OppsByTotal, Luis Model:\r\n" + JsonConvert.SerializeObject(result), serviceCall.DebugMessage); context.Wait(MessageReceived); }
public async Task <HttpResponseMessage> Get([FromUri] OpportunitiesRequestModel model) { var cache = new MemoryCacher(); var list = (List <Dictionary <string, string> >)cache.GetValue(cacheKey); if (list == null) { list = new List <Dictionary <string, string> >(); var opportunities = new Opportunities(); var result = await opportunities.Get(); var items = result; for (var i = 1; i < items.Length; i++) { var dict = new Dictionary <string, string>(); for (var j = 0; j < items[i].Length; j++) { dict.Add(items[0][j], items[i][j]); } list.Add(dict); } cache.Add(cacheKey, list, DateTimeOffset.UtcNow.AddHours(1)); } var filteredList = list; if (model.Intent == OpportunitiesIntents.OppsByAccountName) { filteredList = list.Where(d => CompareAreEqual(d["BusinessAccount"], model.AccountName)).ToList(); } if (model.Intent == OpportunitiesIntents.OppsByTotal) { filteredList = list.Where(d => CompareIsGgreater(d["Total"], model.Amount, model.GreaterOrLessThan)).ToList(); } return(new HttpResponseMessage() { Content = new ObjectContent(typeof(List <Dictionary <string, string> >), filteredList, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue("application/json")) }); }
public async Task OppsByAccountName(IDialogContext context, LuisResult result) { var model = new OpportunitiesRequestModel { Intent = OpportunitiesIntents.OppsByAccountName, AccountName = result.Entities.FirstOrDefault(e => e.Type == "accountName").Entity }; model.Message = "Here are the opportunities for **_" + model.AccountName.ToUpperInvariant() + "_** \r\n"; var serviceCall = await service.GetOpportunities(model); await context.PostAsync(serviceCall.Message); await WriteDebug(context, "DEBUG OppsByAccountName, Luis Model:\r\n" + JsonConvert.SerializeObject(result), serviceCall.DebugMessage); context.Wait(MessageReceived); }