public static async System.Threading.Tasks.Task <string> MessageHandler(string inputStr) { string response = String.Empty; // Convert input string to FaqLuis Model LuisObject faqLuis = await LuisService.ParseFaqInput(inputStr); if (faqLuis.topScoringIntent != null) { switch (faqLuis.topScoringIntent.intent) { case "FaqQuery": response = await FaqService.FaqQueryProcessor(faqLuis.topScoringIntent); break; case "Assistance": response = ResponseSet.GetRandomResponse(ResponseSet.Assistance); break; case "DirectAddress": response = await DirectAddressHandler(inputStr); break; case "Farewell": response = ResponseSet.GetRandomResponse(ResponseSet.Farewells); break; case "Politeness": response = ResponseSet.GetRandomResponse(ResponseSet.Emojis); break; //- Could not place request default: response = ResponseSet.GetRandomResponse(ResponseSet.Nones); break; } } return(response); }
public static async Task <LuisObject> ParseFaqInput(string inputStr) { LuisObject _Data = null; string response = String.Empty; string strEscaped = Uri.EscapeDataString(inputStr); using (var client = new HttpClient()) { string uri = String.Format("https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/...q={0}", strEscaped); HttpResponseMessage msg = await client.GetAsync(uri); if (msg.IsSuccessStatusCode) { string jsonResponse = await msg.Content.ReadAsStringAsync(); _Data = JsonConvert.DeserializeObject <LuisObject>(jsonResponse); } return(_Data); } }
public async Task <LuisObject> QueryAsync(string query) { try { using (HttpClient client = new HttpClient()) { string response = await client.GetStringAsync(new Uri($"https://api.projectoxford.ai/luis/v1/application?id={LuisAppID}&subscription-key={LuisSubscriptionKey}&q={query}")); LuisObject luis = JsonConvert.DeserializeObject <LuisObject>(response); if (luis != null) { return(luis); } } return(null); } catch (Exception) { return(null); } }
public static async Task <LuisObject> QueryAsync(string query) { try { using (HttpClient client = new HttpClient()) { string response = await client.GetStringAsync(new Uri($"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/{LuisAppID}?subscription-key={LuisSubscriptionKey}&verbose=true&timezoneOffset=0&q={query}")); LuisObject luis = JsonConvert.DeserializeObject <LuisObject>(response); if (luis != null) { return(luis); } } return(null); } catch (Exception) { return(null); } }
async Task <List <string> > EnviaMensaje(string query) { LuisObject luis = await LuisService.QueryAsync(query); List <string> mensajes = new List <string>(); if (luis == null) { mensajes.Add("Error: LUIS SERVICE NOT FOUND"); return(mensajes); } if (luis.intents.Count() == 0) { mensajes.Add("Error: LUIS SERVICE INTENTS NOT FOUND"); return(mensajes); } switch (luis.intents[0]?.intent) { case "BusquedaNoticias": if (luis.entities.Count() == 0) { mensajes.Add("Error: LUIS SERVICE ENTITIES NOT FOUND"); return(mensajes); } var entityType = luis.entities[0].type; var searchQueryString = ""; switch (entityType) { case "TemaNoticias": searchQueryString = $"searchQuery={luis.entities[0].entity}"; break; case "CategoriaNoticias": searchQueryString = $"searchQuery={luis.entities[0].entity}"; break; default: searchQueryString = $"searchQuery={luis.entities[0].entity}"; break; } var lista = await Servicios.BingService.QueryAsync(searchQueryString); foreach (var item in lista) { mensajes.Add(item.Title); } return(mensajes); break; default: mensajes.Add("Sorry, I did not understand you. Try again, please :-)"); return(mensajes); } }
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static async Task <string> DirectAddressHandler(string inputStr) { string response = String.Empty; // Convert input string to FaqLuis Model LuisObject directAddressLuis = await LuisService.ParseDirectAddressInput(inputStr); if (directAddressLuis.topScoringIntent != null) { switch (directAddressLuis.topScoringIntent.intent) { //- Questions abot how the bot is doing case "Feeling": response = ResponseSet.SegwayFromResponse(ResponseSet.Feeling); break; //- The bot's past or family case "Origin": response = ResponseSet.SegwayFromResponse(ResponseSet.Origin); break; //- Age, name, stuff you'd find on a Driver License case "Personal": response = ResponseSet.SegwayFromResponse(ResponseSet.Personal); break; //- Responses to confessions of love case "Affectionate": response = ResponseSet.SegwayFromResponse(ResponseSet.Affectionate); break; //- What the bot likes to do off the clock case "Hobbies": response = ResponseSet.SegwayFromResponse(ResponseSet.Hobbies); break; //- Speculation case "Future": response = ResponseSet.SegwayFromResponse(ResponseSet.Future); break; //- Hostility expressed toward the bot case "Adversarial": response = ResponseSet.SegwayFromResponse(ResponseSet.Adversarial); break; //- Questions about other bots and companies case "Robots": response = ResponseSet.SegwayFromResponse(ResponseSet.Robots); break; //- Questions bout the bot's gender case "Gender": response = ResponseSet.SegwayFromResponse(ResponseSet.Gender); break; //- Questions about faith and spirituality case "Religion": response = ResponseSet.SegwayFromResponse(ResponseSet.Religion); break; //- Could not place default: response = ResponseSet.SegwayFromResponse(ResponseSet.DirectAddress); break; } } return(response); }
async Task <string> EnviaMensaje(string query) { OpenWeatherService openWeatherService = new OpenWeatherService(); string city, time, condition; LuisService luisService = new LuisService(); LuisObject luis = await luisService.QueryAsync(query); if (luis == null) { return("Error: LUIS SERVICE NOT FOUND"); } if (luis.intents.Count() == 0) { return("Error: LUIS SERVICE INTENTS NOT FOUND"); } switch (luis.intents[0]?.intent) { case "Weather": if (luis.entities.Count() == 0) { return("Error: LUIS SERVICE ENTITIES NOT FOUND"); } city = luis.entities.Where(ent => ent.type == "City").FirstOrDefault()?.entity; time = luis.entities.Where(ent => ent.type == "Time").FirstOrDefault()?.entity; if (city == null) { return("Error: Please specify the city."); } if (time == null) { time = DateTime.Now.ToShortDateString(); //Default time is now.. } DateTime requestedDt = DateTime.Now; switch (time) { case "yesterday": requestedDt.AddDays(-1); break; case "tomorrow": requestedDt.AddDays(1); break; case "next week": requestedDt.AddDays(7); break; case "last week": requestedDt.AddDays(-7); break; } string replyBase; if ((requestedDt - DateTime.Now).Days > 0) { var forecast = await openWeatherService.GetForecastData(city, requestedDt); List lastDayWeather = forecast.list.Last(); string description = lastDayWeather.weather.FirstOrDefault()?.description; string lowAt = Math.Round(lastDayWeather.temp.min) + "°"; string highAt = Math.Round(lastDayWeather.temp.max) + "°"; string cityName = forecast.city.name + ", " + forecast.city.country; DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(lastDayWeather.dt).ToLocalTime(); DateTime date = dtDateTime; replyBase = string.Format(OpenWeatherService.ForecastMessage, date.ToString("dddd, MMMM, yyyy"), description, cityName, lowAt, highAt); } else { var weather = await openWeatherService.GetWeatherData(city); string description = weather.weather.FirstOrDefault()?.description; string lowAt = weather.main.temp_min + ""; string highAt = weather.main.temp_min + ""; string cityName = ""; cityName = weather.name + ", " + weather.sys.country; replyBase = string.Format(OpenWeatherService.WeatherMessage, description, cityName, lowAt, highAt); } return(replyBase); case "Condition": city = luis.entities.Where(ent => ent.type == "City").FirstOrDefault()?.entity; condition = luis.entities.Where(ent => ent.type == "Condition").FirstOrDefault()?.entity; if (city == null) { return("Error: Please specify the city."); } var weatherForecast = await openWeatherService.GetWeatherData(city); string descriptionF = weatherForecast.weather.FirstOrDefault()?.description; string status = weatherForecast.weather.FirstOrDefault()?.main; string cityNameF = weatherForecast.name + ", " + weatherForecast.sys.country; descriptionF = descriptionF.Replace("nice", "clear|sun|bright|fine|partially cloudy").Replace("good", "clear|sun|bright|fine").Replace("bad", "rain|snow|cloud").Replace("cold", "snow|hail|sleet|blizzard").Replace("day", "").Replace("night", "").Replace("morning", "").Replace("afternoon", ""); string message = (condition.ToLower().StartsWith(status.ToLower()) || descriptionF.Contains(condition)) ? string.Format(OpenWeatherService.YesMessage, descriptionF, city) : string.Format(OpenWeatherService.NoMessage, descriptionF, city); return(message); default: return("Sorry, I did not understand you. Try again, please :-)"); } return("---"); }