예제 #1
0
        /// <summary>
        /// Creates locations hero cards.
        /// </summary>
        /// <param name="locations">List of the locations.</param>
        /// <param name="alwaysShowNumericPrefix">Indicates whether a list containing exactly one location should have a '1.' prefix in its label.</param>
        /// <param name="locationNames">List of strings that can be used as names or labels for the locations.</param>
        /// <returns>The locations card as a list.</returns>
        private IEnumerable <HeroCard> CreateHeroCards(IList <Location> locations, bool alwaysShowNumericPrefix = false, IList <string> locationNames = null)
        {
            var cards      = new List <HeroCard>();
            var geoService = new BingGeoSpatialService(ConfigurationManager.AppSettings["BingAPIKey"]);
            int i          = 1;

            foreach (var location in locations)
            {
                string nameString     = locationNames == null ? string.Empty : $"{locationNames[i - 1]}: ";
                string locationString = $"{nameString}{location.Address.FormattedAddress}";
                string address        = locations.Count > 1 ? $"{i}. {locationString}" : locationString;

                var heroCard = new HeroCard
                {
                    Subtitle = address
                };

                var image = new CardImage(url: geoService.GetLocationMapImageUrl(location, i));

                heroCard.Images = new[] { image };

                cards.Add(heroCard);

                i++;
            }

            return(cards);
        }
예제 #2
0
        public static async Task <HeroCard> GenerateLocationCard()
        {
            var address         = "Corso del Lavoro e della Scienza 3 - 38122 Trento";
            var resourceManager = new LocationResourceManager();
            var mapsAPI         = WebConfigurationManager.AppSettings["BingMapsMasterKey"];
            var carBuilder      = new LocationCardBuilder(mapsAPI, resourceManager);
            var bingService     = new BingGeoSpatialService(mapsAPI);
            var locationSet     = await bingService.GetLocationsByQueryAsync(address);

            var addressUrl = "https://www.google.it/maps/place/" + address;
            var card       = carBuilder.CreateHeroCards(locationSet.Locations).ToList().First();

            card.Title = "Info";

            string text = String.Empty;

            text += $@"Telefono: +39 0461270311" + "\n\n";
            text += $@"Email: [email protected]" + "\n\n";
            text += $@"Indirizzo: {address}";

            card.Text    = text;
            card.Buttons = new[] { new CardAction(ActionTypes.OpenUrl, @"Mappa", value: addressUrl),
                                   new CardAction(ActionTypes.OpenUrl, @"Contatti", value: "http://www.muse.it/it/contatti/Pagine/default.aspx"),
                                   new CardAction(ActionTypes.OpenUrl, @"Orari e Tariffe", value: "http://www.muse.it/it/visita/orari-tariffe/Pagine/Home.aspx") };
            return(card);
        }
예제 #3
0
        public async Task <bool> CheckAddressAsync()
        {
            BingGeoSpatialService service = new BingGeoSpatialService();
            var result = await service.GetLocationsByQueryAsync(ConfigData.bingApiKey, this.Address);

            if ((result.Locations != null) && (result.Locations.Count > 0) && (result.Locations[0].Point.HasCoordinates))
            {
                Lat = result.Locations[0].Point.Coordinates[0];
                Lon = result.Locations[0].Point.Coordinates[1];
                return(true);
            }
            return(false);
        }
예제 #4
0
        private async Task GetMapLocation(IDialogContext context, string location)
        {
            var geoService  = new BingGeoSpatialService(ConfigurationManager.AppSettings["BingAPIKey"]);
            var locationSet = await geoService.GetLocationsByQueryAsync(location);

            var foundLocations = locationSet?.Locations;

            var locationsCardReply = context.MakeMessage();

            locationsCardReply.Attachments      = CreateHeroCards(foundLocations).Select(C => C.ToAttachment()).ToList();
            locationsCardReply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            await context.PostAsync(locationsCardReply);
        }
예제 #5
0
        /// <summary>
        /// Creates locations hero cards.
        /// </summary>
        /// <param name="locations">List of the locations.</param>
        /// <param name="alwaysShowNumericPrefix">Indicates whether a list containing exactly one location should have a '1.' prefix in its label.</param>
        /// <param name="locationNames">List of strings that can be used as names or labels for the locations.</param>
        /// <returns>The locations card as a list.</returns>
        public IEnumerable <HeroCard> CreateHeroCards(IList <Bing.Location> locations, bool alwaysShowNumericPrefix = false, IList <string> locationNames = null)
        {
            var cards = new List <HeroCard>();

            int i = 1;

            foreach (var location in locations)
            {
                try
                {
                    string nameString     = locationNames == null ? string.Empty : $"{locationNames[i - 1]}: ";
                    string locationString = $"{nameString}{location.GetFormattedAddress(resourceManager.AddressSeparator)}";
                    string address        = alwaysShowNumericPrefix || locations.Count > 1 ? $"{i}. {locationString}" : locationString;

                    var heroCard = new HeroCard
                    {
                        Subtitle = address
                    };

                    if (location.Point != null)
                    {
                        IGeoSpatialService geoService;

                        if (useAzureMaps)
                        {
                            geoService = new AzureMapsSpatialService(apiKey);
                        }
                        else
                        {
                            geoService = new BingGeoSpatialService(apiKey);
                        }

                        var image =
                            new CardImage(
                                url: geoService.GetLocationMapImageUrl(location, i));

                        heroCard.Images = new[] { image };
                    }

                    cards.Add(heroCard);

                    i++;
                }
                catch (Exception)
                { }
            }

            return(cards);
        }
        public LocationDialog(
            string apiKey,
            string prompt,
            BotState state,
            string dialogId         = DefaultLocationDialogId,
            bool skipPrompt         = false,
            bool useAzureMaps       = true,
            LocationOptions options = LocationOptions.None,
            LocationRequiredFields requiredFields   = LocationRequiredFields.None,
            LocationResourceManager resourceManager = null) : base(dialogId)
        {
            resourceManager = resourceManager ?? new LocationResourceManager();

            if (!options.HasFlag(LocationOptions.SkipFavorites) && state == null)
            {
                throw new ArgumentNullException(nameof(state),
                                                "If LocationOptions.SkipFavorites is not used then BotState object must be " +
                                                "provided to allow for storing / retrieval of favorites");
            }

            var favoriteLocations = state.CreateProperty <List <FavoriteLocation> >($"{nameof(LocationDialog)}.Favorites");
            var favoritesManager  = new FavoritesManager(favoriteLocations);

            IGeoSpatialService geoSpatialService;

            if (useAzureMaps)
            {
                geoSpatialService = new AzureMapsSpatialService(apiKey);
            }
            else
            {
                geoSpatialService = new BingGeoSpatialService(apiKey);
            }

            InitialDialogId = dialogId;

            AddDialog(new ChoicePrompt(PromptDialogIds.Choice));
            AddDialog(new TextPrompt(PromptDialogIds.Text));
            AddDialog(new ConfirmPrompt(PromptDialogIds.Confirm));

            AddDialog(new WaterfallDialog(InitialDialogId, new WaterfallStep[]
            {
                async(dc, cancellationToken) =>
                {
                    if (options.HasFlag(LocationOptions.SkipFavorites) ||
                        !favoritesManager.GetFavorites(dc.Context).Result.Any())
                    {
                        var isFacebookChannel = StringComparer.OrdinalIgnoreCase.Equals(
                            dc.Context.Activity.ChannelId, "facebook");

                        if (options.HasFlag(LocationOptions.UseNativeControl) && isFacebookChannel)
                        {
                            return(await dc.BeginDialogAsync(DialogIds.LocationRetrieverFacebookDialog));
                        }

                        return(await dc.BeginDialogAsync(DialogIds.LocationRetrieverRichDialog));
                    }

                    return(await dc.BeginDialogAsync(DialogIds.HeroStartCardDialog));
                },
                async(dc, cancellationToken) =>
                {
                    var selectedLocation = (Bing.Location)dc.Result;
                    dc.Values[StepContextKeys.SelectedLocation] = selectedLocation;

                    if (options.HasFlag(LocationOptions.SkipFinalConfirmation))
                    {
                        return(await dc.NextAsync());
                    }

                    await dc.PromptAsync(PromptDialogIds.Confirm,
                                         new PromptOptions()
                    {
                        Prompt = new Activity
                        {
                            Type = ActivityTypes.Message,
                            Text = string.Format(resourceManager.ConfirmationAsk,
                                                 selectedLocation.GetFormattedAddress(resourceManager.AddressSeparator))
                        },
                        RetryPrompt = new Activity
                        {
                            Type = ActivityTypes.Message,
                            Text = resourceManager.ConfirmationInvalidResponse
                        }
                    });

                    return(EndOfTurn);
                },
                async(dc, cancellationToken) =>
                {
                    if (dc.Result is bool result && !result)
                    {
                        await dc.Context.SendActivityAsync(resourceManager.ResetPrompt);
                        return(await dc.ReplaceDialogAsync(InitialDialogId));
                    }

                    if (!options.HasFlag(LocationOptions.SkipFavorites))
                    {
                        return(await dc.BeginDialogAsync(DialogIds.AddToFavoritesDialog,
                                                         new AddToFavoritesDialogOptions()
                        {
                            Location = (Bing.Location)dc.Values[StepContextKeys.SelectedLocation]
                        }
                                                         ));
                    }
                    else
                    {
                        await dc.NextAsync();
                    }

                    return(EndOfTurn);
                },
                async(dc, cancellationToken) =>
                {
                    var selectedLocation = (Bing.Location)dc.Values[StepContextKeys.SelectedLocation];
                    return(await dc.EndDialogAsync(CreatePlace(selectedLocation)));
                }
            }));
        public LocationDialog(
            string apiKey,
            string prompt,
            bool skipPrompt         = false,
            bool useAzureMaps       = true,
            LocationOptions options = LocationOptions.None,
            LocationRequiredFields requiredFields   = LocationRequiredFields.None,
            LocationResourceManager resourceManager = null) : base(MainDialogId)
        {
            resourceManager = resourceManager ?? new LocationResourceManager();
            var favoritesManager = new FavoritesManager();

            IGeoSpatialService geoSpatialService;

            if (useAzureMaps)
            {
                geoSpatialService = new AzureMapsSpatialService(apiKey);
            }
            else
            {
                geoSpatialService = new BingGeoSpatialService(apiKey);
            }

            Dialogs.Add(Inputs.Choice, new ChoicePrompt(Culture.English));
            Dialogs.Add(Inputs.Text, new TextPrompt());
            Dialogs.Add(Inputs.Confirm, new ConfirmPrompt(Culture.English));

            Dialogs.Add(MainDialogId, new WaterfallStep[]
            {
                async(dc, args, next) =>
                {
                    if (options.HasFlag(LocationOptions.SkipFavorites) ||
                        !favoritesManager.GetFavorites(dc.Context).Result.Any())
                    {
                        var isFacebookChannel = StringComparer.OrdinalIgnoreCase.Equals(
                            dc.Context.Activity.ChannelId, "facebook");

                        if (options.HasFlag(LocationOptions.UseNativeControl) && isFacebookChannel)
                        {
                            await dc.Begin(DialogIds.LocationRetrieverFacebookDialog);
                        }
                        else
                        {
                            await dc.Begin(DialogIds.LocationRetrieverRichDialog);
                        }
                    }
                    else
                    {
                        await dc.Begin(DialogIds.HeroStartCardDialog);
                    }
                },
                async(dc, args, next) =>
                {
                    Bing.Location selectedLocation = (Bing.Location)args[Outputs.SelectedLocation];
                    dc.ActiveDialog.State[Outputs.SelectedLocation] = selectedLocation;

                    if (options.HasFlag(LocationOptions.SkipFinalConfirmation))
                    {
                        await next();
                    }
                    else
                    {
                        await dc.Prompt(Inputs.Confirm,
                                        string.Format(resourceManager.ConfirmationAsk,
                                                      selectedLocation.GetFormattedAddress(resourceManager.AddressSeparator)),
                                        new PromptOptions()
                        {
                            RetryPromptString = resourceManager.ConfirmationInvalidResponse
                        });
                    }
                },
                async(dc, args, next) =>
                {
                    if (args is ConfirmResult result && !result.Confirmation)
                    {
                        await dc.Context.SendActivity(resourceManager.ResetPrompt);
                        await dc.Replace(MainDialogId);
                    }
예제 #8
0
        public IDialog <LocationDialogResponse> CreateDialog(BranchType branch, Location location = null, string locationName = null, bool skipDialogPrompt = false)
        {
            bool isFacebookChannel = StringComparer.OrdinalIgnoreCase.Equals(this.channelId, "facebook");

            if (branch == BranchType.LocationRetriever)
            {
                if (this.options.HasFlag(LocationOptions.UseNativeControl) && isFacebookChannel)
                {
                    return(new FacebookNativeLocationRetrieverDialog(
                               this.prompt,
                               this.geoSpatialService,
                               this.options,
                               this.requiredFields,
                               this.resourceManager));
                }

                IGeoSpatialService geoService;

                if (useAzureMaps)
                {
                    geoService = new AzureMapsSpatialService(this.apiKey);
                }
                else
                {
                    geoService = new BingGeoSpatialService(this.apiKey);
                }

                return(new RichLocationRetrieverDialog(
                           prompt: this.prompt,
                           supportsKeyboard: isFacebookChannel,
                           cardBuilder: new LocationCardBuilder(this.apiKey, this.resourceManager),
                           geoSpatialService: geoService,
                           options: this.options,
                           requiredFields: this.requiredFields,
                           resourceManager: this.resourceManager,
                           skipPrompt: skipDialogPrompt));
            }
            else if (branch == BranchType.FavoriteLocationRetriever)
            {
                IGeoSpatialService geoService;

                if (useAzureMaps)
                {
                    geoService = new AzureMapsSpatialService(this.apiKey);
                }
                else
                {
                    geoService = new BingGeoSpatialService(this.apiKey);
                }

                return(new FavoriteLocationRetrieverDialog(
                           isFacebookChannel,
                           new FavoritesManager(),
                           this,
                           new LocationCardBuilder(this.apiKey, this.resourceManager),
                           geoService,
                           this.options,
                           this.requiredFields,
                           this.resourceManager));
            }
            else if (branch == BranchType.AddToFavorites)
            {
                return(new AddFavoriteLocationDialog(new FavoritesManager(), location, this.resourceManager));
            }
            else if (branch == BranchType.EditFavoriteLocation)
            {
                return(new EditFavoriteLocationDialog(this, new FavoritesManager(), locationName, location, this.resourceManager));
            }
            else
            {
                throw new ArgumentException("Invalid branch value.");
            }
        }