コード例 #1
0
        /// <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("Cards", StringComparison.OrdinalIgnoreCase))
            {
                // Demo card 1 - adaptive card with bot bulder actions
                AdaptiveCards.AdaptiveCard adaptiveCard = new AdaptiveCards.AdaptiveCard();
                adaptiveCard.Body.Add(new AdaptiveCards.AdaptiveTextBlock("Bot Builder actions"));

                var action1 = new CardAction("imback", "imBack", null, null, null, "text");
                var action2 = new CardAction("messageBack", "message back", null, "text received by bots", "text display to users", JObject.Parse(@"{ ""key"" : ""value"" }"));
                var action3 = new CardAction("invoke", "invoke", null, null, null, JObject.Parse(@"{ ""key"" : ""value"" }"));
                adaptiveCard.Actions.Add(action1.ToAdaptiveCardAction());
                adaptiveCard.Actions.Add(action2.ToAdaptiveCardAction());
                adaptiveCard.Actions.Add(action3.ToAdaptiveCardAction());

                // Task module action
                var taskModuleAction = new TaskModuleAction("Launch Task Module", @"{ ""hiddenKey"": ""hidden value from task module launcher"" }");

                // Demo card 2 - launch task module from adaptive card
                AdaptiveCards.AdaptiveCard taskModuleCard1 = new AdaptiveCards.AdaptiveCard();
                taskModuleCard1.Body.Add(new AdaptiveCards.AdaptiveTextBlock("Task Module Adaptive Card"));
                taskModuleCard1.Actions.Add(taskModuleAction.ToAdaptiveCardAction());

                // Demo card 3 - launch task module from hero card (or any bot-builder framework card)
                HeroCard taskModuleCard2 = new HeroCard("Launch Task Module", null, null, null, new List <CardAction> {
                    taskModuleAction
                });

                Activity replyActivity = turnContext.Activity.CreateReply();
                replyActivity.Attachments = new List <Attachment>()
                {
                    adaptiveCard.ToAttachment(),
                        taskModuleCard1.ToAttachment(),
                        taskModuleCard2.ToAttachment(),
                };
                await turnContext.SendActivityAsync(replyActivity).ConfigureAwait(false);
            }
            else
            {
                Activity replyActivity = turnContext.Activity.CreateReply();
                replyActivity.TextFormat = "xml";
                replyActivity.Text       = $"You said: {turnContext.Activity.Text}";
                await turnContext.SendActivityAsync(replyActivity).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);
            }
        }