using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; using System.Web; using LUIS_AP.CognitiveModels; using LUIS_AP.Enums; using LUIS_AP.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; namespace LUIS_AP.Controllers { public class HomeController : Controller { private IConfiguration AppSettings { get; set; } public HomeController(IConfiguration appSettings) { AppSettings = appSettings; } public IActionResult Index() { return View(); } [HttpPost] public JsonResult SendMessage([FromBody] ChatModel model) { var luisResponse = CallForInterpretation(model).Result; return Json(luisResponse); } public async Task<ChatModel> CallForInterpretation(ChatModel model) { var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); queryString["verbose"] = "true"; queryString["timezoneOffset"] = "0"; queryString["subscription-key"] = AppSettings["LuisAPIKey"]; queryString["q"] = model.Utterance; var endpointUri = $"https://{AppSettings["LuisAPIHostName"]}/luis/v2.0/apps/{AppSettings["LuisAppId"]}?{queryString}"; var response = await client.GetAsync(endpointUri); var jsonResponse = await response.Content.ReadAsStringAsync(); model.JsonRawResponse = jsonResponse; try { model.SentimentAnalysis = GetPropertyValue<SentimentAnalysis>("sentimentAnalysis", jsonResponse); model.TopScoringIntent = GetPropertyValue<TopScoringIntent>("topScoringIntent", jsonResponse); model.Entities = GetPropertyValue<List<EntityItem>>("entities", jsonResponse); } catch (Exception) { model.SentimentAnalysis = new SentimentAnalysis(); model.TopScoringIntent = new TopScoringIntent(); } model = ProcessBotResponse(model); return model; } private ChatModel ProcessBotResponse(ChatModel model) { model.BotResponseId = Guid.NewGuid().ToString(); switch (model.IntentEnum) { case Intent.Greeting: model.BotResponse = $"Hello there {model.CurrentUserName}."; break; case Intent.QuestionWellBeing: model.BotResponse = "I'm doing well. Thank you."; break; case Intent.WISEgrantsTechnicalIssue: model.BotResponse = model.HasEntities ? $"It sounds like you're experiencing a technical issue in WISEgrants with the {model.TopEntity} screen. Please start a Help Ticket for further resolution via the following URL: https://dpi.wi.gov/wisedata/help/request" : "It sounds like you're experiencing a technical issue in WISEgrants. Please start a Help Ticket for further resolution via the following URL: https://dpi.wi.gov/wisedata/help/request"; break; case Intent.CheckWeather: model.BotResponse = "I haven't been coded to access weather data yet. I'm afraid I can't reliably tell you the weather."; break; default: switch (model.SentimentEnum) { case Sentiment.Negative: model.BotResponse = "I'm not sure how to respond. But I know I'm not very fond of the negativity in your tone."; break; case Sentiment.Positive: model.BotResponse = "I'm not sure how to respond. But I do know you sound very positive about what you're saying."; break; default: model.BotResponse = "I'm not sure how to respond."; break; } break; } return model; } private T GetPropertyValue<T>(string propertyName, string json) { var result = default(T); using (var stringReader = new StringReader(json)) using (var jsonReader = new JsonTextReader(stringReader)) { while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.PropertyName && (string)jsonReader.Value == propertyName) { jsonReader.Read(); var serializer = new JsonSerializer(); try { result = serializer.Deserialize<T>(jsonReader); return result; } catch (Exception) { result = default(T); return result; } } } } return result; } } }