예제 #1
0
        protected override async Task MessageReceivedInternalAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            var messageText          = (await result).Text.Trim();
            FavoriteLocation value   = null;
            string           command = null;

            if (StringComparer.OrdinalIgnoreCase.Equals(messageText, this.ResourceManager.OtherComand))
            {
                this.SwitchToLocationRetriever(context);
            }
            else if (this.TryParseSelection(context, messageText, out value))
            {
                await this.ProcessRetrievedLocation(context, value.Location);
            }
            else if (this.TryParseCommandSelection(context, messageText, out value, out command) &&
                     (StringComparer.OrdinalIgnoreCase.Equals(command, this.ResourceManager.DeleteCommand) ||
                      StringComparer.OrdinalIgnoreCase.Equals(command, this.ResourceManager.EditCommand)))
            {
                if (StringComparer.OrdinalIgnoreCase.Equals(command, this.ResourceManager.DeleteCommand))
                {
                    TryConfirmAndDelete(context, value);
                }
                else
                {
                    var editDialog = this.locationDialogFactory.CreateDialog(BranchType.EditFavoriteLocation, value.Location, value.Name);
                    context.Call(editDialog, this.ResumeAfterChildDialogAsync);
                }
            }
            else
            {
                await context.PostAsync(string.Format(this.ResourceManager.InvalidFavoriteLocationSelection, messageText));

                context.Wait(this.MessageReceivedAsync);
            }
        }
예제 #2
0
        private void TryConfirmAndDelete(IDialogContext context, FavoriteLocation favoriteLocation)
        {
            var confirmationAsk = string.Format(
                this.ResourceManager.DeleteFavoriteConfirmationAsk,
                $"{favoriteLocation.Name}: {favoriteLocation.Location.GetFormattedAddress(this.ResourceManager.AddressSeparator)}");

            this.selectedLocation = favoriteLocation;

            PromptDialog.Confirm(
                context,
                async(dialogContext, answer) =>
            {
                if (await answer)
                {
                    this.favoritesManager.Delete(dialogContext, this.selectedLocation);
                    await dialogContext.PostAsync(string.Format(this.ResourceManager.FavoriteDeletedConfirmation, this.selectedLocation.Name));
                    await this.StartAsync(dialogContext);
                }
                else
                {
                    await dialogContext.PostAsync(string.Format(this.ResourceManager.DeleteFavoriteAbortion, this.selectedLocation.Name));
                    await dialogContext.PostAsync(this.ResourceManager.SelectFavoriteLocationPrompt);
                    dialogContext.Wait(this.MessageReceivedAsync);
                }
            },
                confirmationAsk,
                retry: this.ResourceManager.ConfirmationInvalidResponse,
                promptStyle: PromptStyle.None);
        }
예제 #3
0
        private bool TryParseCommandSelection(IDialogContext context, string text, out FavoriteLocation value, out string command)
        {
            value   = null;
            command = null;

            var tokens = text.Split(' ');

            if (tokens.Length != 2)
            {
                return(false);
            }

            command = tokens[0];

            return(this.TryParseSelection(context, tokens[1], out value));
        }
예제 #4
0
        private bool TryParseSelection(IDialogContext context, string text, out FavoriteLocation value)
        {
            var favoriteRetrievedByName = this.favoritesManager.GetFavoriteByName(context, text);

            if (favoriteRetrievedByName != null)
            {
                value = favoriteRetrievedByName;
                return(true);
            }

            int index = -1;

            if (int.TryParse(text, out index))
            {
                value = this.favoritesManager.GetFavoriteByIndex(context, index - 1);
                return(value != null);
            }

            value = null;
            return(false);
        }