Exemple #1
0
        // Process the new members added to the team
        private async Task ProcessNewTeamMembersAsync(IEnumerable <TeamsChannelAccount> teamMembers, Team teamInfo)
        {
            this.logProvider.LogInfo($"Processing new members in team {teamInfo.Id}");

            var welcomeCard = CelebrationCard.GetWelcomeMessageForGeneralChannelAndTeamMembers(teamInfo.InstallerName, teamInfo.Name).ToAttachment();
            await Task.WhenAll(teamMembers.Select(member => this.ProcessNewTeamMemberAsync(member, teamInfo, welcomeCard)));
        }
        /// <summary>
        /// Send welcome message in general channel.
        /// </summary>
        /// <param name="activity">Activity instance.</param>
        /// <param name="isBotInstalledInTeam">true/false.</param>
        /// <param name="installerName">bot installer name.</param>
        /// <param name="teamName">TeamName</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task SendWelcomeMessageToGeneralChannel(Activity activity, bool isBotInstalledInTeam, string installerName, string teamName)
        {
            this.logProvider.LogInfo("Sending welcome message to general channel.");

            Activity     welcomeActivity = activity.CreateReply();
            AdaptiveCard welcomeCard;

            if (isBotInstalledInTeam)
            {
                welcomeCard = CelebrationCard.GetWelcomeMessageForGeneralChannelAndTeamMembers(installerName, teamName);
            }
            else
            {
                welcomeCard = CelebrationCard.GetWelcomeCardForInstaller();
            }

            this.logProvider.LogInfo($"Welcome card json: {welcomeCard.ToJson()}");

            welcomeActivity.Attachments.Add(welcomeCard.ToAttachment());
            await this.connectorClient.Conversations.ReplyToActivityAsync(welcomeActivity);

            this.logProvider.LogInfo("Welcome message sent to general Chanel.");
        }
        // send welcome message to all team members
        private async Task SendWelcomeMessageToTeamMembers(IList <ChannelAccount> teamMembers, TeamsChannelData channelData, string installerName, string teamName)
        {
            this.logProvider.LogInfo($"Sending welcome message to all the team members. Total team members of team: {channelData.Team.Id} = {teamMembers.Count}");
            string conversationId = string.Empty;

            foreach (var member in teamMembers)
            {
                conversationId = this.connectorServiceHelper.CreateOrGetConversationIdAsync(channelData.Tenant.Id, member.Id);
                List <Attachment> attachmentList = new List <Attachment> {
                    CelebrationCard.GetWelcomeMessageForGeneralChannelAndTeamMembers(installerName, teamName).ToAttachment()
                };
                string userAadObjectId = (member.Properties["objectId"] ?? member.Properties["aadObjectId"]).ToString();
                List <CelebrationEvent> celebrationEvents = await(await this.eventHelper.GetEventsByOwnerObjectIdAsync(userAadObjectId)).ToListAsync();

                if (celebrationEvents.Count > 0)
                {
                    attachmentList.Add(CelebrationCard.GetShareEventAttachement(channelData.Team.Id, channelData.Team.Name, userAadObjectId));
                }

                this.logProvider.LogInfo($"Sending personal welcome message to {member.Name}", new Dictionary <string, string>
                {
                    { "TeamId", channelData.Team.Id },
                    { "UserTeamId", member.Id },
                    { "UserObjectId", userAadObjectId },
                    { "ConversationId", conversationId },
                    { "Attachment", attachmentList.ToString() },
                });

                await this.connectorServiceHelper.SendPersonalMessageAsync(string.Empty, attachmentList, conversationId);

                this.logProvider.LogInfo("Saving member details to database.");

                // Save team member details.
                await this.SaveMemberDetailsAsync(member, channelData, conversationId);
            }
        }
Exemple #4
0
        // Handle team members added event (in team scope)
        private async Task HandleTeamMembersAddedAsync(Activity activity, TeamsChannelData channelData)
        {
            string teamId = channelData.Team.Id;

            this.logProvider.LogInfo($"Handling team members added event in team {teamId}");

            // Determine if the bot was installed to the team
            bool isBotAdded = activity.MembersAdded.Any(member => member.Id == activity.Recipient.Id);

            if (isBotAdded)
            {
                this.logProvider.LogInfo($"Bot was installed to team {teamId}");
                var properties = new Dictionary <string, string>
                {
                    { "Scope", activity.Conversation?.ConversationType },
                    { "TeamId", teamId },
                    { "InstallerId", activity.From.Id },
                };
                this.logProvider.LogEvent("AppInstalled", properties);
            }

            var membersAdded = activity.MembersAdded;

            // Ensure that we have an installation record for this team
            var isBackfill = false;
            var teamInfo   = await this.userManagementHelper.GetTeamsDetailsByTeamIdAsync(teamId);

            if ((teamInfo == null) && !isBotAdded)
            {
                this.logProvider.LogInfo($"Detected a missed installation to team {teamId}, will attempt to backfill");

                // We must have missed an event from this team-- attempt to backfill
                isBotAdded = true;
                isBackfill = true;
            }

            if (isBotAdded)
            {
                // Try to determine the name of the person that installed the app, which is usually the sender of the message (From.Id)
                // Note that in some cases we cannot resolve it to a team member, because the app was installed to the team programmatically via Graph
                string installerName = null;
                if (!isBackfill)
                {
                    installerName = activity.From?.Name;
                    if (installerName == null)
                    {
                        installerName = await this.GetUserNameAsync(activity.From, teamId);
                    }
                }

                // Get team details
                this.logProvider.LogInfo("Getting team details");
                var teamDetails = await this.connectorClient.GetTeamsConnectorClient().Teams.FetchTeamDetailsAsync(teamId);

                // Add team installation record
                this.logProvider.LogInfo("Recording team installation");
                teamInfo = new Team
                {
                    Id            = teamId,
                    Name          = teamDetails.Name,
                    ServiceUrl    = activity.ServiceUrl,
                    TenantId      = channelData.Tenant.Id,
                    InstallerName = installerName,
                };
                await this.userManagementHelper.SaveTeamDetailsAsync(teamInfo);

                // Send welcome message to the General channel
                this.logProvider.LogInfo("Sending welcome message to general channel");
                Activity reply = activity.CreateReply();
                reply.Attachments.Add(CelebrationCard.GetWelcomeMessageForGeneralChannelAndTeamMembers(installerName, teamDetails.Name).ToAttachment());
                await this.connectorClient.Conversations.SendToConversationWithRetriesAsync(reply);

                // Get all team members to welcome them
                this.logProvider.LogInfo("Getting all team members to send them a welcome message");
                membersAdded = await this.connectorClient.Conversations.GetConversationMembersAsync(teamId);
            }
            else
            {
                this.logProvider.LogInfo($"Members added to team {teamId} ({membersAdded?.Count} new members)");
            }

            // Process new team members
            await this.ProcessNewTeamMembersAsync(membersAdded.AsTeamsChannelAccounts(), teamInfo);
        }