Example #1
0
        protected async Task GetRouteDirectionsViewCards(DialogContext sc, RouteDirections routeDirections)
        {
            var routes = routeDirections.Routes;
            var state  = await Accessor.GetAsync(sc.Context);

            var cardData = new List <RouteDirectionsModelCardData>();
            var routeId  = 0;

            if (routes != null)
            {
                state.FoundRoutes = routes.ToList();

                var destination = state.Destination;

                foreach (var route in routes)
                {
                    var travelTimeSpan  = TimeSpan.FromSeconds(route.Summary.TravelTimeInSeconds);
                    var trafficTimeSpan = TimeSpan.FromSeconds(route.Summary.TrafficDelayInSeconds);

                    // Set card data with formatted time strings and distance converted to miles
                    var routeDirectionsModel = new RouteDirectionsModelCardData()
                    {
                        Name             = destination.Name,
                        Street           = destination.Street,
                        City             = destination.City,
                        AvailableDetails = destination.AvailableDetails,
                        Hours            = destination.Hours,
                        ImageUrl         = destination.ImageUrl,
                        TravelTime       = GetShortTravelTimespanString(travelTimeSpan),
                        DelayStatus      = GetFormattedTrafficDelayString(trafficTimeSpan),
                        Distance         = $"{(route.Summary.LengthInMeters / 1609.344).ToString("N1")} {PointOfInterestSharedStrings.MILES_ABBREVIATION}",
                        ETA              = route.Summary.ArrivalTime.ToShortTimeString(),
                        TravelTimeSpeak  = GetFormattedTravelTimeSpanString(travelTimeSpan),
                        TravelDelaySpeak = GetFormattedTrafficDelayString(trafficTimeSpan)
                    };

                    cardData.Add(routeDirectionsModel);
                    routeId++;
                }

                if (cardData.Count() > 1)
                {
                    var cards = new List <Card>();
                    foreach (var data in cardData)
                    {
                        cards.Add(new Card("RouteDirectionsViewCard", data));
                    }

                    var replyMessage = ResponseManager.GetCardResponse(POISharedResponses.MultipleRoutesFound, cards);
                    replyMessage.Speak = ResponseUtility.BuildSpeechFriendlyPoIResponse(replyMessage);
                    await sc.Context.SendActivityAsync(replyMessage);
                }
                else
                {
                    var card         = new Card("RouteDirectionsViewCard", cardData.SingleOrDefault());
                    var replyMessage = ResponseManager.GetCardResponse(POISharedResponses.SingleRouteFound, card);
                    replyMessage.Speak = ResponseUtility.BuildSpeechFriendlyPoIResponse(replyMessage);
                    await sc.Context.SendActivityAsync(replyMessage);
                }
            }
            else
            {
                var replyMessage = ResponseManager.GetResponse(POISharedResponses.NoLocationsFound);
                await sc.Context.SendActivityAsync(replyMessage);
            }
        }
Example #2
0
        protected async Task <List <PointOfInterestModel> > GetPointOfInterestLocationCards(DialogContext sc, List <PointOfInterestModel> pointOfInterestList)
        {
            var state = await Accessor.GetAsync(sc.Context);

            var service        = ServiceManager.InitMapsService(Services);
            var addressService = ServiceManager.InitAddressMapsService(Services);

            if (pointOfInterestList != null && pointOfInterestList.Count > 0)
            {
                for (int i = 0; i < pointOfInterestList.Count; i++)
                {
                    if (sc.ActiveDialog.Id.Equals(Actions.CheckForCurrentLocation))
                    {
                        pointOfInterestList[i] = await addressService.GetPointOfInterestDetailsAsync(pointOfInterestList[i]);
                    }
                    else
                    {
                        pointOfInterestList[i] = await service.GetPointOfInterestDetailsAsync(pointOfInterestList[i]);
                    }

                    // Increase by one to avoid zero based options to the user which are confusing
                    pointOfInterestList[i].Index = i + 1;

                    if (string.IsNullOrEmpty(pointOfInterestList[i].ImageUrl))
                    {
                        pointOfInterestList[i].ImageUrl = GetCardImageUri(FallbackPointOfInterestImageFileName);
                    }

                    if (string.IsNullOrEmpty(pointOfInterestList[i].Name))
                    {
                        pointOfInterestList[i].Name = pointOfInterestList[i].Street;
                    }
                }

                state.LastFoundPointOfInterests = pointOfInterestList;

                if (pointOfInterestList.Count() > 1)
                {
                    var templateId = POISharedResponses.MultipleLocationsFound;
                    var cards      = new List <Card>();

                    foreach (var pointOfInterest in pointOfInterestList)
                    {
                        cards.Add(new Card("PointOfInterestViewCard", pointOfInterest));
                    }

                    var replyMessage = ResponseManager.GetCardResponse(templateId, cards);

                    replyMessage.Speak = ResponseUtility.BuildSpeechFriendlyPoIResponse(replyMessage);

                    await sc.Context.SendActivityAsync(replyMessage);
                }
                else
                {
                    var templateId = POISharedResponses.SingleLocationFound;

                    var card         = new Card("PointOfInterestViewCard", state.LastFoundPointOfInterests[0]);
                    var replyMessage = ResponseManager.GetCardResponse(templateId, card);
                    replyMessage.Speak = ResponseUtility.BuildSpeechFriendlyPoIResponse(replyMessage);

                    await sc.Context.SendActivityAsync(replyMessage);
                }
            }
            else
            {
                var replyMessage = ResponseManager.GetResponse(POISharedResponses.NoLocationsFound);
                await sc.Context.SendActivityAsync(replyMessage);
            }

            return(pointOfInterestList);
        }