Exemple #1
0
        /// <summary>
        /// Create public or private channel based on grouping criteria.
        /// </summary>
        /// <param name="accessToken">Token to access Microsoft Graph API.</param>
        /// <param name="groupActivityId">group activity Id.</param>
        /// <param name="teamId">Team id where messaging extension is installed.</param>
        /// <param name="groupId">Team Azure Active Directory object id of the channel where bot is installed.</param>
        /// <param name="valuesFromTaskModule">Group activity details from task module entered by user.</param>
        /// <param name="membersGroupingWithChannel">List of all members grouped in channels based on grouping criteria.</param>
        /// <param name="groupActivityCreator">Team owner who started the group activity.</param>
        /// <param name="turnContext">Provides context for a turn of a bot.</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 true if channel is created successfully.</returns>
        private async Task CreateChannelAsync(string accessToken, string groupActivityId, string teamId, string groupId, GroupDetail valuesFromTaskModule, Dictionary <int, IList <TeamsChannelAccount> > membersGroupingWithChannel, TeamsChannelAccount groupActivityCreator, string groupingMessage, ITurnContext <IInvokeActivity> turnContext, CancellationToken cancellationToken)
        {
            try
            {
                string channelType = valuesFromTaskModule.ChannelType;

                Tuple <List <ChannelApiResponse>, List <string> > channelDetails = null;
                switch (channelType)
                {
                case Constants.PublicChannelType:
                    channelDetails = await this.CreatePublicChannelAsync(accessToken, membersGroupingWithChannel, valuesFromTaskModule, groupId, groupActivityCreator.Name, groupingMessage, turnContext, cancellationToken);

                    break;

                case Constants.PrivateChannelType:
                    channelDetails = await this.CreatePrivateChannelAsync(accessToken, groupId, membersGroupingWithChannel, groupActivityCreator, valuesFromTaskModule);

                    break;
                }

                if (channelDetails != null && channelDetails.Item1.Count > 0)
                {
                    bool isChannelInfoSaved = await this.StoreChannelsCreatedDetailsAsync(teamId, groupActivityId, channelType, channelDetails.Item1, turnContext);

                    if (!isChannelInfoSaved)
                    {
                        await turnContext.SendActivityAsync(Strings.CustomErrorMessage);

                        this.logger.LogInformation($"Saving newly created channel details to table storage failed for teamId: {teamId}.");
                    }
                }

                // show the list of channels to the user which are failed to get created.
                if (channelDetails.Item2.Count > 0)
                {
                    StringBuilder channelsNotCreated = new StringBuilder();
                    foreach (var channel in channelDetails.Item2)
                    {
                        channelsNotCreated.AppendLine(channel).AppendLine();
                    }

                    this.logger.LogError($"Number of channels failed to get created. TotalChannels {channelDetails.Item2.Count.ToString()}, Team {teamId}");
                    await turnContext.SendActivityAsync(MessageFactory.Attachment(GroupActivityCard.GetChannelCreationFailedCard(channelsNotCreated.ToString(), valuesFromTaskModule.GroupTitle)));
                }
            }
            catch (Exception ex)
            {
                await turnContext.SendActivityAsync(Strings.CustomErrorMessage);

                this.logger.LogError(ex, $"Error while creating channels for teamId: {teamId}");
            }
        }