/// <summary> /// Given a conversation request, extracts and validates required parameters. /// </summary> /// <param name="req">Conversation request</param> /// <param name="temp">Hottest or coldest temperature indicator</param> /// <param name="year">Year between 1929 and 2017</param> /// <param name="countryCode2">Country code</param> /// <param name="countryName">Country name</param> /// <param name="fipsCountry"></param> /// <returns></returns> private static string ExtractAndValidateParameters(ConvRequest req, out string temp, out string year, out string countryCode2, out string countryName, out string fipsCountry) { temp = req.Parameters["temp-extreme"].ToLowerInvariant(); var datesJson = JObject.Parse(req.Parameters["date-period"]); year = new string(datesJson["startdate"].Value <string>().Take(4).ToArray()); var countryJson = JObject.Parse(req.Parameters["country"]); countryCode2 = countryJson["alpha-2"].Value <string>(); countryName = countryJson["name"].Value <string>(); fipsCountry = FipsIsoCountryMap.Map(countryCode2.ToUpperInvariant()); if (year.Length != 4 || !int.TryParse(year, out int yearInt) || yearInt < 1929 || yearInt > 2017) { return("Sorry, the year specified is invalid or out of range."); } if (countryCode2 == null || countryName == null || countryCode2.Length != 2 || countryName.Length == 0) { return("Sorry, I can't understand that country."); } if (fipsCountry == null) { return("Sorry, cannot recognize country: '{countryName}'"); } if (temp != "hottest" && temp != "coldest") { return("Sorry, I don't know if you want the hottest or coldest temperature."); } return(null); }
/// <summary> /// Given a conversation request, extracts and validates required parameters. /// </summary> /// <param name="req">Conversation request</param> /// <param name="hottestOrColdest">Hottest or coldest temperature indicator</param> /// <param name="year">Year between 1929 and 2017</param> /// <param name="countryName">Country name</param> /// <param name="fipsCountry"></param> /// <returns></returns> private static string ExtractAndValidateParameters(WebhookRequest req, out string hottestOrColdest, out string year, out string countryName, out string fipsCountry) { var fields = req.QueryResult.Parameters.Fields; hottestOrColdest = fields["hottest-or-coldest"].StringValue.ToLowerInvariant(); // Extract the day from DialogFlow date var datePeriod = fields["date"].StructValue; var startDate = datePeriod.Fields["startDate"].StringValue; var dateTime = DateTime.Parse(startDate); year = dateTime.ToString("yyyy"); var country = fields["country"].StructValue; var countryCode = country.Fields["alpha-2"].StringValue; countryName = country.Fields["name"].StringValue; fipsCountry = FipsIsoCountryMap.Map(countryCode.ToUpperInvariant()); if (!int.TryParse(year, out int yearInt) || yearInt < 1929 || yearInt > 2017) { return("Sorry, the year specified is invalid or out of range."); } if (fipsCountry == null) { return("Sorry, cannot recognize country: '{countryName}'"); } return(null); }
/// <summary> /// Given a conversation request, extracts and validates required parameters. /// </summary> /// <param name="req">Conversation request</param> /// <param name="temp">Hottest or coldest temperature indicator</param> /// <param name="year">Year between 1929 and 2017</param> /// <param name="countryCode2">Country code</param> /// <param name="countryName">Country name</param> /// <param name="fipsCountry"></param> /// <returns></returns> private static string ExtractAndValidateParameters(WebhookRequest req, out string temp, out string year, out string countryCode2, out string countryName, out string fipsCountry) { var fields = req.QueryResult.Parameters.Fields; temp = fields["temp-extreme"].StringValue.ToLowerInvariant(); var datePeriod = fields["date-period"].StructValue; var startDate = datePeriod.Fields["startDate"].StringValue; year = new string(startDate.Take(4).ToArray()); var country = fields["country"].StructValue; countryCode2 = country.Fields["alpha-2"].StringValue; countryName = country.Fields["name"].StringValue; fipsCountry = FipsIsoCountryMap.Map(countryCode2.ToUpperInvariant()); if (year.Length != 4 || !int.TryParse(year, out int yearInt) || yearInt < 1929 || yearInt > 2017) { return("Sorry, the year specified is invalid or out of range."); } if (countryCode2 == null || countryName == null || countryCode2.Length != 2 || countryName.Length == 0) { return("Sorry, I can't understand that country."); } if (fipsCountry == null) { return("Sorry, cannot recognize country: '{countryName}'"); } if (temp != "hottest" && temp != "coldest") { return("Sorry, I don't know if you want the hottest or coldest temperature."); } return(null); }