Example #1
0
        /**
         * Waterfall step to prompt for user's name
         *
         * @param {DialogContext} Dialog context
         * @param {WaterfallStepContext} water fall step context
         */
        private async Task<DialogTurnResult> AskForUserNameAsync(
                                                WaterfallStepContext stepContext,
                                                CancellationToken cancellationToken)
        {
            var context = stepContext.Context;

            // Get user profile.
            var userProfile = await UserProfileAccessor.GetAsync(context, () => new UserProfile(null, null));

            // Get on turn properties.
            var onTurnProperty = await OnTurnAccessor.GetAsync(context, () => new OnTurnProperty("None", new List<EntityProperty>()));

            // Handle case where user is re-introducing themselves.
            // This flow is triggered when we are not in the middle of who-are-you dialog
            // and the user says something like 'call me {username}' or 'my name is {username}'.

            // Get user name entities from on turn property (from the cafe bot dispatcher LUIS model)
            var userNameInOnTurnProperty = (onTurnProperty.Entities ?? new List<EntityProperty>()).Where(item => ((item.EntityName == userNameEntity) || (item.EntityName == userNamePatternAnyEntity)));
            if (userNameInOnTurnProperty.Count() > 0)
            {
                // Get user name from on turn property
                var userName = userNameInOnTurnProperty.First().Value as string;

                // Capitalize user name
                userName = char.ToUpper(userName[0]) + userName.Substring(1);

                // Set user name
                await UserProfileAccessor.SetAsync(context, new UserProfile(userName));

                // End this step so we can greet the user.
                return await stepContext.NextAsync(haveUserName);
            }

            // Prompt user for name if
            // we have an invalid or empty user name or
            // if the user name was previously set to 'Human'
            if (userProfile == null || string.IsNullOrWhiteSpace(userProfile.UserName) || userProfile.UserName.Equals("Human", StringComparison.Ordinal))
            {
                await context.SendActivityAsync("Hello, I'm the Contoso Cafe Bot.");

                // Begin the prompt to ask user their name
                var opts = new PromptOptions
                {
                    Prompt = new Activity
                    {
                        Type = ActivityTypes.Message,
                        Text = "What's your name?",
                    },
                };
                return await stepContext.PromptAsync(askUserNamePrompt, opts);
            }
            else
            {
                // Already have the user name. So just greet them.
                await context.SendActivityAsync($"Hello {userProfile.UserName}, Nice to meet you again! I'm the Contoso Cafe Bot.");

                // End this dialog. We are skipping the next water fall step deliberately.
                return await stepContext.EndDialogAsync();
            }
        }
Example #2
0
        public async Task SetUserProfileAsync(UserProfile profile, DialogContext dialogContext
                                              , CancellationToken cancellationToken = default)
        {
            //Also update the dialog contexts state
            await UserProfileAccessor.SetAsync(dialogContext.Context, profile, cancellationToken);

            dialogContext.GetState().SetValue("user.UserProfile", profile);
        }
Example #3
0
        private async Task <DialogTurnResult> InitializeStateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await UserProfileAccessor.GetAsync(stepContext.Context, () => null);

            if (state == null)
            {
                var reservationDataOpt = stepContext.Options as ReservationData;
                if (reservationDataOpt != null)
                {
                    await UserProfileAccessor.SetAsync(stepContext.Context, reservationDataOpt);
                }
                else
                {
                    await UserProfileAccessor.SetAsync(stepContext.Context, new ReservationData());
                }
            }

            return(await stepContext.NextAsync());
        }