private static async Task SendWelcomeMessageToAllMembers(Tenant tenant, Activity message, TeamsChannelData channelData, IEnumerable <TeamsChannelAccount> members) { var tid = channelData.Tenant.Id; var emailId = await RootDialog.GetUserEmailId(message); var tenatInfo = await Cache.Tenants.GetItemAsync(tid); Role role = Common.GetUserRole(emailId, tenatInfo); var card = CardHelper.GetWelcomeScreen(false, role); foreach (var member in members) { var userDetails = await Cache.Users.GetItemAsync(member.UserPrincipalName.ToLower()); if (userDetails == null) { userDetails = new User() { BotConversationId = member.Id, Id = member.UserPrincipalName.ToLower().Trim(), Name = member.Name ?? member.GivenName }; await Cache.Users.AddOrUpdateItemAsync(userDetails.Id, userDetails); tenant.Users.Add(userDetails.Id); await Cache.Tenants.AddOrUpdateItemAsync(tenant.Id, tenant); if (tenant.IsAdminConsented) { if (tenant.Moderators.Contains(userDetails.Id)) { await ProactiveMessageHelper.SendNotification(message.ServiceUrl, channelData.Tenant.Id, member.Id, null, card); } else { // Get User welcome card. await ProactiveMessageHelper.SendNotification(message.ServiceUrl, channelData.Tenant.Id, member.Id, $"Welcome to {ApplicationSettings.AppName}. We will deliver company {ApplicationSettings.AppFeature}s here.", null); } } } } }
public async Task <ActionResult> ConsentPage(string tenant, string admin_consent, string state) { if (string.IsNullOrEmpty(tenant)) { return(HttpNotFound()); } var adminUserDetails = JsonConvert.DeserializeObject <AdminUserDetails>(HttpUtility.UrlDecode(state)); var tenantDetails = await Cache.Tenants.GetItemAsync(tenant); tenantDetails.IsAdminConsented = true; tenantDetails.Admin = adminUserDetails.UserEmailId; await Cache.Tenants.AddOrUpdateItemAsync(tenantDetails.Id, tenantDetails); var userDetails = await Cache.Users.GetItemAsync(adminUserDetails.UserEmailId); await ProactiveMessageHelper.SendPersonalNotification(adminUserDetails.ServiceUrl, tenant, userDetails, "Your app consent is successfully granted. Please go ahead and set groups & moderators.", null); await ProactiveMessageHelper.SendPersonalNotification(adminUserDetails.ServiceUrl, tenant, userDetails, null, CardHelper.GetAdminPanelCard(string.Join(",", tenantDetails.Moderators))); return(View()); }
private static async Task SendWelcomeMessageToAllMembers(Tenant tenant, Activity message, TeamsChannelData channelData, IEnumerable <TeamsChannelAccount> members) { var tid = channelData.Tenant.Id; var currentUser = members.FirstOrDefault(m => m.Id == message.From.Id)?.AsTeamsChannelAccount()?.UserPrincipalName?.ToLower(); var tenatInfo = await Cache.Tenants.GetItemAsync(tid); var moderatorCard = CardHelper.GetWelcomeScreen(false, Role.Moderator); var userCard = CardHelper.GetWelcomeScreen(false, Role.User); var newUsers = new List <User>(); foreach (var member in members) { var emailId = member.UserPrincipalName.ToLower().Trim(); emailId = Common.RemoveHashFromGuestUserUPN(emailId); if (!tenatInfo.Users.Contains(emailId)) { var userDetails = new User() { BotConversationId = member.Id, Id = emailId, Name = member.Name ?? member.GivenName, AadObjectId = member.AadObjectId }; tenant.Users.Add(userDetails.Id); newUsers.Add(userDetails); } } var importResult = await DocumentDBRepository.BulkExecutor.BulkImportAsync(newUsers); if (importResult.NumberOfDocumentsImported != newUsers.Count) { // TODO: Take action } // Save all the user's details in the tenant. await Cache.Tenants.AddOrUpdateItemAsync(tenant.Id, tenant); // Comment this if you don't want to send welcome message to each new user. if (tenant.IsAdminConsented && newUsers.Count > 0) { var serviceUrl = message.ServiceUrl; var tenantId = channelData.Tenant.Id; var results = new ConcurrentBag <NotificationSendStatus>(); await Common.ForEachAsync(newUsers, ApplicationSettings.NoOfParallelTasks, async userDetails => { Attachment card = tenant.Moderators.Contains(userDetails.Id) ? moderatorCard : userCard; var result = await ProactiveMessageHelper.SendPersonalNotification(serviceUrl, tenantId, userDetails, null, card); results.Add(result); }); var owner = await Cache.Users.GetItemAsync(currentUser); if (owner != null) { var successCount = results.Count(m => m.IsSuccessful); await ProactiveMessageHelper.SendPersonalNotification(serviceUrl, tenantId, owner, $"Process of sending welcome message to all members of {channelData.Team.Name} completed. Successful: {successCount}. " + $"Failure: {results.Count - successCount}.", null); } } }