Ejemplo n.º 1
0
        //GET: api/Call

        /// <summary>
        /// Gets query as a string (response from LUIS) and intent type then sends the data to the ICB API
        /// </summary>
        /// <returns>ResponceData object</returns>
        private async Task <KPIApiResponse> GetFromApiAsync(string actualQuery, string intent)
        {
            HttpClient client = new HttpClient();

            string url = Credentials.API_URL;

            if (intent == "MachineRequestData")
            {
                url += "Machine/Last";
            }
            else
            {
                url += "KPI/Last";
            }

            client.DefaultRequestHeaders.Add(Credentials.API_HEADER, Credentials.API_ID);
            HttpResponseMessage message = await client.PostAsync(url, new StringContent(actualQuery, Encoding.UTF8, "application/json"));

            if (!message.IsSuccessStatusCode)
            {
                throw new Exception($"Request from ICB API failed with status code {message.StatusCode}");
            }

            string jsonResponce = await message.Content.ReadAsStringAsync();

            if (jsonResponce == "[]")
            {
                throw new Exception("No existing data for the query!");
            }

            KPIApiResponse topIntent = JObject.Parse(jsonResponce.Trim('"', '"').Trim('[', ']')).ToObject <KPIApiResponse>();

            return(topIntent);
        }
Ejemplo n.º 2
0
        /// <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);
        }