Esempio n. 1
0
        /// <summary>
        /// Get user profile or search or edit profile based on activity type.
        /// </summary>
        /// <param name="stepContext">Provides context for a step in a bot dialog.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>User profile or search or edit profile based on activity type.</returns>
        private async Task <DialogTurnResult> MyProfileAndSearchAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var tokenResponse = (TokenResponse)stepContext.Result;
            var activity      = stepContext.Context.Activity;

            if (tokenResponse == null)
            {
                this.logger.LogInformation($"User is not authenticated and token is null for: {activity.Conversation.Id}.");
                await stepContext.Context.SendActivityAsync(Strings.NotLoginText).ConfigureAwait(false);

                return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }

            var token = tokenResponse.Token.ToString(CultureInfo.InvariantCulture);

            // signin/verifyState activity name used here to send my profile card after successful sign in.
            if ((activity.Type == MessageActivityType) || (activity.Name == SignInActivityName))
            {
                var command = ((string)stepContext.Values["command"]).ToUpperInvariant().Trim();

                switch (command)
                {
                case Constants.MyProfile:
                    this.logger.LogInformation("my profile command triggered", new Dictionary <string, string>()
                    {
                        { "User", activity.From.Id }, { "AADObjectId", activity.From.AadObjectId }
                    });
                    await this.MyProfileAsync(token, stepContext, cancellationToken).ConfigureAwait(false);

                    break;

                case Constants.Search:
                    this.logger.LogInformation("Search command triggered.", new Dictionary <string, string>()
                    {
                        { "User", activity.From.Id }, { "AADObjectId", activity.From.AadObjectId }
                    });
                    await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(SearchCard.GetSearchCard())).ConfigureAwait(false);

                    break;

                default:
                    await this.EditProfileAsync(token, stepContext, cancellationToken).ConfigureAwait(false);

                    break;
                }

                return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
            }

            // submit-invoke request at edit profile
            else if (activity.Type == InvokeActivityType)
            {
                await this.EditProfileAsync(token, stepContext, cancellationToken).ConfigureAwait(false);
            }

            return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken).ConfigureAwait(false));
        }
        /// <summary>
        /// Get user profile or search or edit profile based on activity type.
        /// </summary>
        /// <param name="stepContext">Provides context for a step in a bot dialog.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Tracking task.</returns>
        private async Task <DialogTurnResult> MyProfileAndSearchAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var activity = stepContext.Context.Activity;

            var tokenResponse = (TokenResponse)stepContext.Result;

            if (tokenResponse == null)
            {
                this.logger.LogInformation($"User is not authenticated and token is null for: {activity.Conversation.Id}.");
                await stepContext.Context.SendActivityAsync(Strings.NotLoggedInText).ConfigureAwait(false);

                return(await stepContext.EndDialogAsync().ConfigureAwait(false));
            }

            var token = tokenResponse.Token;

            // signin/verifyState activity name used here to send my profile card after successful sign in.
            if ((activity.Type == MessageActivityType) || (activity.Name == SignInActivityName))
            {
                var command = ((string)stepContext.Values["command"]).Trim();

                if (command.Equals(Strings.BotCommandMyProfile, StringComparison.CurrentCultureIgnoreCase) || command.Equals(Constants.MyProfile))
                {
                    this.logger.LogInformation("My profile command triggered", new Dictionary <string, string>()
                    {
                        { "User", activity.From.Id }, { "AADObjectId", activity.From.AadObjectId }
                    });
                    await this.MyProfileAsync(token, stepContext, cancellationToken).ConfigureAwait(false);
                }
                else if (command.Equals(Strings.BotCommandSearch, StringComparison.CurrentCultureIgnoreCase) || command.Equals(Constants.Search))
                {
                    this.logger.LogInformation("Search command triggered", new Dictionary <string, string>()
                    {
                        { "User", activity.From.Id }, { "AADObjectId", activity.From.AadObjectId }
                    });
                    await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(SearchCard.GetSearchCard())).ConfigureAwait(false);
                }
                else
                {
                    await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(HelpCard.GetHelpCard())).ConfigureAwait(false);
                }
            }

            // submit-invoke request at edit profile
            else if (activity.Type == InvokeActivityType)
            {
                await this.EditProfileAsync(token, stepContext, cancellationToken).ConfigureAwait(false);
            }

            return(await stepContext.EndDialogAsync().ConfigureAwait(false));
        }