Beispiel #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>
        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);
                    }
Beispiel #4
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.");
            }
        }