private async Task <LocationSet> GetLocationsAsync(string url) { using (var client = new HttpClient()) { url = url + $"&language={userLocale}"; var response = await client.GetStringAsync(url); var apiResponse = JsonConvert.DeserializeObject <SearchResultSet>(response); var results = new LocationSet(); if (apiResponse != null && apiResponse.Results != null) { results.EstimatedTotal = apiResponse.Results.Count; results.Locations = new List <Location>(); foreach (var r in apiResponse.Results) { results.Locations.Add(r.ToLocation()); } } return(results); } }
protected async Task <DialogTurnResult> GetPointOfInterestLocations(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken)) { try { // Defensive for scenarios where locale isn't correctly set var country = "US"; try { var cultureInfo = new RegionInfo(sc.Context.Activity.Locale); country = cultureInfo.TwoLetterISORegionName; } catch (Exception) { // Default to everything if we can't restrict the country } var state = await Accessor.GetAsync(sc.Context); var service = ServiceManager.InitMapsService(GetAzureMapsKey(), sc.Context.Activity.Locale ?? "en-us"); var locationSet = new LocationSet(); state.CheckForValidCurrentCoordinates(); if (string.IsNullOrEmpty(state.SearchText) && string.IsNullOrEmpty(state.SearchAddress)) { // No entities identified, find nearby locations locationSet = await service.GetLocationsNearby(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude); await GetPointOfInterestLocationViewCards(sc, locationSet); } else if (!string.IsNullOrEmpty(state.SearchText)) { // Fuzzy search locationSet = await service.GetLocationsByFuzzyQueryAsync(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude, state.SearchText, country); await GetPointOfInterestLocationViewCards(sc, locationSet); } else if (!string.IsNullOrEmpty(state.SearchAddress)) { // Query search locationSet = await service.GetLocationsByFuzzyQueryAsync(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude, state.SearchAddress, country); await GetPointOfInterestLocationViewCards(sc, locationSet); } if (locationSet?.Locations?.ToList().Count == 1) { return(await sc.PromptAsync(Action.ConfirmPrompt, new PromptOptions { Prompt = sc.Context.Activity.CreateReply(POISharedResponses.PromptToGetRoute, ResponseBuilder) })); } state.ClearLuisResults(); return(await sc.EndDialogAsync(true)); } catch { await HandleDialogException(sc); throw; } }
// Helpers public async Task GetPointOfInterestLocationViewCards(DialogContext sc, LocationSet locationSet) { var locations = locationSet.Locations; var state = await _accessors.PointOfInterestSkillState.GetAsync(sc.Context); var cardsData = new List <LocationCardModelData>(); var service = _serviceManager.InitMapsService(_services.AzureMapsKey); if (locations != null) { state.FoundLocations = locations.ToList(); foreach (var location in locations) { var imageUrl = service.GetLocationMapImageUrl(location); LocationCardModelData locationCardModel = new LocationCardModelData() { ImageUrl = imageUrl, LocationName = location.Name, Address = location.Address.FormattedAddress, }; cardsData.Add(locationCardModel); } if (cardsData.Count() > 1) { if (sc.ActiveDialog.Id.Equals(Action.FindAlongRoute) && state.ActiveRoute != null) { var replyMessage = sc.Context.Activity.CreateAdaptiveCardGroupReply(PointOfInterestBotResponses.MultipleLocationsFoundAlongActiveRoute, "Dialogs/Shared/Resources/Cards/PointOfInterestViewCard.json", AttachmentLayoutTypes.Carousel, cardsData, _responseBuilder); await sc.Context.SendActivityAsync(replyMessage); } else { var replyMessage = sc.Context.Activity.CreateAdaptiveCardGroupReply(PointOfInterestBotResponses.MultipleLocationsFound, "Dialogs/Shared/Resources/Cards/PointOfInterestViewCard.json", AttachmentLayoutTypes.Carousel, cardsData, _responseBuilder); await sc.Context.SendActivityAsync(replyMessage); } } else { state.ActiveLocation = state.FoundLocations.Single(); if (sc.ActiveDialog.Id.Equals(Action.FindAlongRoute) && state.ActiveRoute != null) { var replyMessage = sc.Context.Activity.CreateAdaptiveCardReply(PointOfInterestBotResponses.SingleLocationFoundAlongActiveRoute, "Dialogs/Shared/Resources/Cards/PointOfInterestViewCard.json", cardsData.SingleOrDefault(), _responseBuilder); await sc.Context.SendActivityAsync(replyMessage); } else { var replyMessage = sc.Context.Activity.CreateAdaptiveCardReply(PointOfInterestBotResponses.SingleLocationFound, "Dialogs/Shared/Resources/Cards/PointOfInterestViewCard.json", cardsData.SingleOrDefault(), _responseBuilder); await sc.Context.SendActivityAsync(replyMessage); } } } else { var replyMessage = sc.Context.Activity.CreateReply(PointOfInterestBotResponses.NoLocationsFound, _responseBuilder); await sc.Context.SendActivityAsync(replyMessage); } }
/// <summary> /// Call Maps Service to run a fuzzy search from current coordinates based on entities retrieved by bot. /// </summary> public async Task <DialogTurnResult> GetPointOfInterestLocations(WaterfallStepContext sc, CancellationToken cancellationToken = default(CancellationToken)) { try { string country = "US"; // Defensive for scenarios where locale isn't correctly set try { var cultureInfo = new RegionInfo(sc.Context.Activity.Locale); country = cultureInfo.TwoLetterISORegionName; } catch (Exception) { // Default to everything if we can't restrict the country } var state = await _accessors.PointOfInterestSkillState.GetAsync(sc.Context); var service = _serviceManager.InitMapsService(_services.AzureMapsKey); var locationSet = new LocationSet(); if (string.IsNullOrEmpty(state.SearchText) && string.IsNullOrEmpty(state.SearchAddress)) { // No entities identified, find nearby locations locationSet = await service.GetLocationsNearby(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude); await GetPointOfInterestLocationViewCards(sc, locationSet); } else if (!string.IsNullOrEmpty(state.SearchText)) { // Fuzzy search locationSet = await service.GetLocationsByFuzzyQueryAsync(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude, state.SearchText, country); await GetPointOfInterestLocationViewCards(sc, locationSet); } else if (!string.IsNullOrEmpty(state.SearchAddress)) { // Query search locationSet = await service.GetLocationsByFuzzyQueryAsync(state.CurrentCoordinates.Latitude, state.CurrentCoordinates.Longitude, state.SearchAddress, country); await GetPointOfInterestLocationViewCards(sc, locationSet); } if (locationSet?.Locations?.ToList().Count == 1) { return(await sc.PromptAsync(Action.ConfirmPrompt, new PromptOptions { Prompt = sc.Context.Activity.CreateReply(PointOfInterestBotResponses.PromptToGetRoute, _responseBuilder) })); } return(await sc.EndDialogAsync(true)); } catch (Exception e) { TelemetryClient tc = new TelemetryClient(); tc.TrackException(e); await sc.Context.SendActivityAsync(sc.Context.Activity.CreateReply(PointOfInterestBotResponses.PointOfInterestErrorMessage, _responseBuilder)); var state = await _accessors.PointOfInterestSkillState.GetAsync(sc.Context); state.Clear(); await _accessors.PointOfInterestSkillState.SetAsync(sc.Context, state); return(await sc.CancelAllDialogsAsync()); } }