/// <summary> /// Gets user request, sends it to LUIS, gets data from ICB API and returns the final answer /// </summary> /// <param name="userRequestMessage">user message</param> // <returns>API response message</returns> public async Task <string> ProcessText(string userRequestMessage) { LuisResponse luisResponseData; TopScoringIntent processedIntent = new TopScoringIntent(); KPIApiResponse responseFromAPI = new KPIApiResponse(); string queryForAPI; try { luisResponseData = (await this.GetFromLuisAsync(userRequestMessage)); queryForAPI = ConstructQueryHelper(luisResponseData); processedIntent = luisResponseData.TopScoringIntent; responseFromAPI = await this.GetFromApiAsync(queryForAPI, processedIntent.Intent); } catch (Exception ex) { if (ex is NullReferenceException) { return("Can't proccess the query, ICB API can't find data for the query!"); } return(ex.Message); } string entity; string entityID; string partNumber = string.Empty; string responseValue = responseFromAPI.Value; if (processedIntent.Intent == "MachineRequestData") { MachineRequestData machineRequestObj = JsonConvert.DeserializeObject <MachineRequestData>(queryForAPI); entity = machineRequestObj.Type; entityID = machineRequestObj.SensorID; } else { KPIRequestDataWithPart kpiRequestObj = JsonConvert.DeserializeObject <KPIRequestDataWithPart>(queryForAPI); entity = kpiRequestObj.KpiType; entityID = kpiRequestObj.WorkOrder; if (kpiRequestObj.Part != string.Empty && kpiRequestObj.Part != null) { partNumber = kpiRequestObj.Part; } } string result = string.Format("The {0} of {1}{2} is {3}.", entity, entityID, (partNumber != string.Empty) ? (" of part " + partNumber) : "", responseValue ); return(result); }
/// <summary> /// Gets JSON (response data from LUIS) object and returns query as a string /// </summary> /// <param name="data">the response data from LUIS</param> /// <returns></returns> private string ConstructQueryHelper(LuisResponse data) { TopScoringIntent processedIntent = data.TopScoringIntent; string actualQuery = string.Empty; switch (processedIntent.Intent)//compose the query { case "None": throw new Exception("Please paraphrase your question, couldn't understand what you meant!"); case "MachineRequestData": List <Entity> machineEntities = data.Entities; MachineRequestData machineRequest = new MachineRequestData(); machineRequest.SensorID = machineEntities.Find(e => e.Type == "MachineID").entity; machineRequest.Type = machineEntities.Find(e => e.Type == "MachineRequestType").resolution.values[0]; actualQuery = JsonConvert.SerializeObject(machineRequest); break; default: List <Entity> kpiEntities = data.Entities; KPIRequestDataWithoutPart kpiRequestWithoutPart = new KPIRequestDataWithoutPart(); int buf; kpiRequestWithoutPart.KpiType = processedIntent.Intent; kpiRequestWithoutPart.WorkOrder = kpiEntities.Find(e => e.Type == "KPIworkOrderID" && int.TryParse(e.entity, out buf)).entity; //get part entity CompositeEntity kpiOrderPart = data.CompositeEntities.Find(e => (e.ParentType == "KPIrequestDataPart" && int.TryParse(e.Value, out buf))); //filter request types without part if (processedIntent.nonOrderQuery() && kpiOrderPart != null) { string kpiOrderPartValue = kpiOrderPart.Value; if (kpiOrderPart != null) { string serializedKPIWithoutPart = JsonConvert.SerializeObject(kpiRequestWithoutPart); KPIRequestDataWithPart kpiRequestWithPart = JsonConvert.DeserializeObject <KPIRequestDataWithPart>(serializedKPIWithoutPart); kpiRequestWithPart.Part = kpiOrderPartValue; actualQuery = JsonConvert.SerializeObject(kpiRequestWithPart); break; } else { //couldn't find part value throw new NullReferenceException("Couldn't find part value"); } } actualQuery = JsonConvert.SerializeObject(kpiRequestWithoutPart); break; } return(actualQuery); }