Exemplo n.º 1
0
        /// <summary>
        /// Handles the team members removed event asynchronously.
        /// </summary>
        /// <param name="teamMembersRemovedEvent">The team members removed event.</param>
        /// <returns>
        /// Task tracking operation.
        /// </returns>
        public async Task HandleTeamMembersRemovedEventAsync(TeamMembersRemovedEvent teamMembersRemovedEvent)
        {
            TeamOperationHistory conversationHistory = await this.teamHistoryAccessor.AuditLog.GetAsync(teamMembersRemovedEvent.TurnContext, () => new TeamOperationHistory()).ConfigureAwait(false);

            foreach (ChannelAccount memberRemoved in teamMembersRemovedEvent.MembersRemoved)
            {
                ITeamsContext       teamsContext        = teamMembersRemovedEvent.TurnContext.TurnState.Get <ITeamsContext>();
                TeamsChannelAccount teamsChannelAccount = teamsContext.AsTeamsChannelAccount(memberRemoved);

                if (conversationHistory.MemberOperations == null)
                {
                    conversationHistory.MemberOperations = new List <OperationDetails>();
                }

                conversationHistory.MemberOperations.Add(new OperationDetails
                {
                    ObjectId      = teamsChannelAccount.AadObjectId,
                    Operation     = "MemberRemoved",
                    OperationTime = DateTimeOffset.Now,
                });
            }

            await this.teamHistoryAccessor.AuditLog.SetAsync(teamMembersRemovedEvent.TurnContext, conversationHistory).ConfigureAwait(false);

            await this.teamHistoryAccessor.ConversationState.SaveChangesAsync(teamMembersRemovedEvent.TurnContext).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles the channel renamed event asynchronously.
        /// </summary>
        /// <param name="channelRenamedEvent">The channel renamed event.</param>
        /// <returns>
        /// Task tracking operation.
        /// </returns>
        public async Task HandleChannelRenamedEventAsync(ChannelRenamedEvent channelRenamedEvent)
        {
            TeamOperationHistory conversationHistory = await this.teamHistoryAccessor.AuditLog.GetAsync(channelRenamedEvent.TurnContext, () => new TeamOperationHistory()).ConfigureAwait(false);

            if (conversationHistory.MemberOperations == null)
            {
                conversationHistory.MemberOperations = new List <OperationDetails>();
            }

            conversationHistory.MemberOperations.Add(new OperationDetails
            {
                ObjectId      = channelRenamedEvent.Channel.Id,
                Operation     = "ChannelRenamed",
                OperationTime = DateTimeOffset.Now,
            });

            await this.teamHistoryAccessor.AuditLog.SetAsync(channelRenamedEvent.TurnContext, conversationHistory).ConfigureAwait(false);

            await this.teamHistoryAccessor.ConversationState.SaveChangesAsync(channelRenamedEvent.TurnContext).ConfigureAwait(false);
        }
        /// <summary>
        /// Handles the message activity asynchronously.
        /// </summary>
        /// <param name="turnContext">The turn context.</param>
        /// <returns>
        /// Task tracking operation.
        /// </returns>
        public async Task HandleMessageAsync(ITurnContext turnContext)
        {
            ITeamsContext teamsContext = turnContext.TurnState.Get <ITeamsContext>();

            string actualText = teamsContext.GetActivityTextWithoutMentions();

            if (actualText.Equals("ShowHistory", StringComparison.OrdinalIgnoreCase) ||
                actualText.Equals("Show History", StringComparison.OrdinalIgnoreCase))
            {
                TeamOperationHistory memberHistory = await this.teamHistoryAccessor.AuditLog.GetAsync(turnContext, () => new TeamOperationHistory()).ConfigureAwait(false);

                Activity replyActivity = turnContext.Activity.CreateReply();

                teamsContext.AddMentionToText(replyActivity, turnContext.Activity.From);
                replyActivity.Text = replyActivity.Text + $" Total of {memberHistory.MemberOperations.Count} operations were performed";

                await turnContext.SendActivityAsync(replyActivity).ConfigureAwait(false);

                // Going in reverse chronological order.
                for (int i = memberHistory.MemberOperations.Count % 10; i >= 0; i--)
                {
                    List <OperationDetails> elementsToSend = memberHistory.MemberOperations.Skip(10 * i).Take(10).ToList();

                    StringBuilder stringBuilder = new StringBuilder();

                    if (elementsToSend.Count > 0)
                    {
                        for (int j = elementsToSend.Count - 1; j >= 0; j--)
                        {
                            stringBuilder.Append($"{elementsToSend[j].ObjectId} --> {elementsToSend[j].Operation} -->  {elementsToSend[j].OperationTime} </br>");
                        }

                        Activity memberListActivity = turnContext.Activity.CreateReply(stringBuilder.ToString());
                        await turnContext.SendActivityAsync(memberListActivity).ConfigureAwait(false);
                    }
                }
            }
            else if (actualText.Equals("ShowCurrentMembers", StringComparison.OrdinalIgnoreCase) ||
                     actualText.Equals("Show Current Members", StringComparison.OrdinalIgnoreCase))
            {
                List <ChannelAccount> teamMembers = (await turnContext.TurnState.Get <IConnectorClient>().Conversations.GetConversationMembersAsync(
                                                         turnContext.Activity.GetChannelData <TeamsChannelData>().Team.Id).ConfigureAwait(false)).ToList();

                Activity replyActivity = turnContext.Activity.CreateReply();
                teamsContext.AddMentionToText(replyActivity, turnContext.Activity.From);
                replyActivity.Text = replyActivity.Text + $" Total of {teamMembers.Count} members are currently in team";

                await turnContext.SendActivityAsync(replyActivity).ConfigureAwait(false);

                for (int i = teamMembers.Count % 10; i >= 0; i--)
                {
                    List <TeamsChannelAccount> elementsToSend = teamMembers.Skip(10 * i).Take(10).ToList().ConvertAll <TeamsChannelAccount>((account) => teamsContext.AsTeamsChannelAccount(account));

                    StringBuilder stringBuilder = new StringBuilder();

                    if (elementsToSend.Count > 0)
                    {
                        for (int j = elementsToSend.Count - 1; j >= 0; j--)
                        {
                            stringBuilder.Append($"{elementsToSend[j].AadObjectId} --> {elementsToSend[j].Name} -->  {elementsToSend[j].UserPrincipalName} </br>");
                        }

                        Activity memberListActivity = turnContext.Activity.CreateReply(stringBuilder.ToString());
                        await turnContext.SendActivityAsync(memberListActivity).ConfigureAwait(false);
                    }
                }
            }
            else if (actualText.Equals("ShowChannelList", StringComparison.OrdinalIgnoreCase) ||
                     actualText.Equals("Show Channels", StringComparison.OrdinalIgnoreCase) ||
                     actualText.Equals("ShowChannels", StringComparison.OrdinalIgnoreCase) ||
                     actualText.Equals("Show Channel List", StringComparison.OrdinalIgnoreCase))
            {
                ConversationList channelList = await teamsContext.Operations.FetchChannelListAsync(turnContext.Activity.GetChannelData <TeamsChannelData>().Team.Id).ConfigureAwait(false);

                Activity replyActivity = turnContext.Activity.CreateReply();
                teamsContext.AddMentionToText(replyActivity, turnContext.Activity.From);
                replyActivity.Text = replyActivity.Text + $" Total of {channelList.Conversations.Count} channels are currently in team";

                await turnContext.SendActivityAsync(replyActivity).ConfigureAwait(false);

                for (int i = channelList.Conversations.Count % 10; i >= 0; i--)
                {
                    List <ChannelInfo> elementsToSend = channelList.Conversations.Skip(10 * i).Take(10).ToList();

                    StringBuilder stringBuilder = new StringBuilder();

                    if (elementsToSend.Count > 0)
                    {
                        for (int j = elementsToSend.Count - 1; j >= 0; j--)
                        {
                            stringBuilder.Append($"{elementsToSend[j].Id} --> {elementsToSend[j].Name}</br>");
                        }

                        Activity memberListActivity = turnContext.Activity.CreateReply(stringBuilder.ToString());
                        await turnContext.SendActivityAsync(memberListActivity).ConfigureAwait(false);
                    }
                }
            }
            else
            {
                await turnContext.SendActivityAsync("Invalid command").ConfigureAwait(false);
            }
        }