/// <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); } }
/// <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}."); } }