Пример #1
0
        /// <summary>
        /// Handle logic for edit profile task module.
        /// </summary>
        /// <param name="token">User access token.</param>
        /// <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>A task that returns edit user profile card attachment.</returns>
        private async Task EditProfileAsync(string token, WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            try
            {
                var activity = stepContext.Context.Activity;

                var userProfileDetails     = new UserProfileDetailBase();
                var userProfileRequestData = JsonConvert.DeserializeObject <EditProfileCardAction>(((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase).ToString());

                userProfileDetails.AboutMe = userProfileRequestData.AboutMe;

                userProfileDetails.Skills = new List <string>();
                if (!string.IsNullOrEmpty(userProfileRequestData.Skills))
                {
                    var skills = userProfileRequestData.Skills.Split(';').Where(skillValue => !string.IsNullOrEmpty(skillValue));
                    userProfileDetails.Skills.AddRange(skills);
                }

                userProfileDetails.Interests = new List <string>();
                if (!string.IsNullOrEmpty(userProfileRequestData.Interests))
                {
                    var interests = userProfileRequestData.Interests.Split(';').Where(interestValue => !string.IsNullOrEmpty(interestValue));
                    userProfileDetails.Interests.AddRange(interests);
                }

                userProfileDetails.Schools = new List <string>();
                if (!string.IsNullOrEmpty(userProfileRequestData.Schools))
                {
                    var schools = userProfileRequestData.Schools.Split(';').Where(schoolValue => !string.IsNullOrEmpty(schoolValue));
                    userProfileDetails.Schools.AddRange(schools);
                }

                string userProfileDetailsData = JsonConvert.SerializeObject(userProfileDetails);
                bool   isUserProfileUpdated   = await this.graphApiHelper.UpdateUserProfileDetailsAsync(token, userProfileDetailsData).ConfigureAwait(false);

                if (!isUserProfileUpdated)
                {
                    await stepContext.Context.SendActivityAsync(Strings.FailedToUpdateProfile).ConfigureAwait(false);

                    this.logger.LogInformation($"Failure in saving data from task module to api for: {activity.Conversation.Id}.");
                }

                this.logger.LogInformation($"User profile updated using graph api for conversation id :  {activity.Conversation.Id}.");

                var userProfileCardId  = ((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase)["MyProfileCardId"].ToString();
                var userDetailsfromApi = await this.graphApiHelper.GetUserProfileAsync(token).ConfigureAwait(false);

                var userProfile = await this.storageHelper.GetUserProfileConversationDataAsync(userProfileCardId).ConfigureAwait(false);

                var updateProfileActivity = MessageFactory.Attachment(MyProfileCard.GetMyProfileCard(userDetailsfromApi, userProfileCardId));
                updateProfileActivity.Id           = userProfile.MyProfileCardActivityId;
                updateProfileActivity.Conversation = activity.Conversation;
                await stepContext.Context.UpdateActivityAsync(updateProfileActivity, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, $"Error occured while posting my profile data to api for:  {stepContext.Context.Activity.Conversation.Id}.");
                await stepContext.Context.SendActivityAsync($"{Strings.ErrorMessage}").ConfigureAwait(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Show profile details to user.
        /// </summary>
        /// <param name="token">User access token.</param>
        /// <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>A task that returns user profile card attachment.</returns>
        private async Task MyProfileAsync(string token, WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            try
            {
                var userProfileDetails = await this.graphApiHelper.GetUserProfileAsync(token).ConfigureAwait(false);

                var userProfileCardId = Guid.NewGuid().ToString();
                IMessageActivity myProfileCardActivity;
                var activity = stepContext.Context.Activity;

                if (userProfileDetails != null)
                {
                    this.logger.LogInformation($"User Profile obtained from graph api for: {activity.Conversation.Id}.");
                    myProfileCardActivity = MessageFactory.Attachment(MyProfileCard.GetMyProfileCard(userProfileDetails, userProfileCardId));
                }
                else
                {
                    this.logger.LogInformation($"User Profile obtained from graph api is null for: {activity.Conversation.Id}.");
                    myProfileCardActivity = MessageFactory.Attachment(MyProfileCard.GetEmptyUserProfileCard(userProfileCardId));
                }

                var myProfileCardActivityResponse = await stepContext.Context.SendActivityAsync(myProfileCardActivity, cancellationToken).ConfigureAwait(false);

                await this.StoreUserProfileCardActivityInfoAsync(myProfileCardActivityResponse.Id, userProfileCardId, stepContext.Context).ConfigureAwait(false);

                this.logger.LogInformation("profile updated by user.", new Dictionary <string, string>()
                {
                    { "User", activity.From.Id }, { "AADObjectId", activity.From.AadObjectId }
                });
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, $"Error occured while executing MyProfile:  {stepContext.Context.Activity.Conversation.Id}.");
            }
        }
Пример #3
0
        /// <summary>
        /// When OnTurn method receives a fetch invoke activity on bot turn, it calls this method.
        /// </summary>
        /// <param name="turnContext">Provides context for a turn of a bot.</param>
        /// <param name="taskModuleRequestData">Task module invoke request value payload.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents a task module response.</returns>
        protected override async Task <TaskModuleResponse> OnTeamsTaskModuleFetchAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequestData, CancellationToken cancellationToken)
        {
            var    activity = turnContext.Activity;
            var    userSearchTaskModuleDetails = JsonConvert.DeserializeObject <EditProfileCardAction>(((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase).ToString());
            string command = userSearchTaskModuleDetails.Command;

            try
            {
                var userGraphAccessToken = await this.tokenHelper.GetUserTokenAsync(activity.From.Id, GraphResourceUri).ConfigureAwait(false);

                if (userGraphAccessToken == null)
                {
                    await turnContext.SendActivityAsync(Strings.NotLoggedInText).ConfigureAwait(false);

                    await this.rootDialog.RunAsync(turnContext, this.dialogStatePropertyAccessor, cancellationToken).ConfigureAwait(false);

                    return(null);
                }
                else
                {
                    switch (command)
                    {
                    case Constants.Search:
                        this.logger.LogInformation("Search fetch activity called");
                        var apiAuthToken = this.customTokenHelper.GenerateAPIAuthToken(aadObjectId: activity.From.AadObjectId, serviceURL: activity.ServiceUrl, fromId: activity.From.Id, jwtExpiryMinutes: 60);
                        return(new TaskModuleResponse
                        {
                            Task = new TaskModuleContinueResponse
                            {
                                Value = new TaskModuleTaskInfo()
                                {
                                    Url = $"{this.botSettings.AppBaseUri}/?token={apiAuthToken}&telemetry={this.botSettings.AppInsightsInstrumentationKey}&theme={{theme}}",
                                    Height = TaskModuleHeight,
                                    Width = TaskModuleWidth,
                                    Title = Strings.SearchTaskModuleTitle,
                                },
                            },
                        });

                    case Constants.MyProfile:
                        this.logger.LogInformation("My profile fetch activity called");
                        var userProfileDetails = await this.graphApiHelper.GetUserProfileAsync(userGraphAccessToken).ConfigureAwait(false);

                        if (userProfileDetails == null)
                        {
                            this.logger.LogInformation("User profile details obtained from Graph API is null.");
                            await turnContext.SendActivityAsync(Strings.ErrorMessage).ConfigureAwait(false);

                            return(null);
                        }
                        else
                        {
                            return(new TaskModuleResponse
                            {
                                Task = new TaskModuleContinueResponse
                                {
                                    Value = new TaskModuleTaskInfo()
                                    {
                                        Card = MyProfileCard.GetEditProfileCard(userProfileDetails, userSearchTaskModuleDetails.MyProfileCardId, this.botSettings.AppBaseUri),
                                        Height = TaskModuleHeight,
                                        Width = TaskModuleWidth,
                                        Title = Strings.EditProfileTitle,
                                    },
                                },
                            });
                        }

                    default:
                        this.logger.LogInformation($"Invalid command for task module fetch activity.Command is : {command} ");
                        await turnContext.SendActivityAsync(Strings.ErrorMessage).ConfigureAwait(false);

                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Error in fetch action of task module.");
                return(null);
            }
        }