/// <summary>
        /// Send the nomination reminder notification to specified team.
        /// </summary>
        /// <param name="rewardCycleEntity">Reward cycle model object.</param>
        /// <returns>A task that sends notification card in channel.</returns>
        private async Task SendCardToTeamAsync(RewardCycleEntity rewardCycleEntity)
        {
            rewardCycleEntity = rewardCycleEntity ?? throw new ArgumentNullException(nameof(rewardCycleEntity));

            var awardsList = await this.awardsStorageProvider.GetAwardsAsync(rewardCycleEntity.TeamId);

            var valuesFromTaskModule = new TaskModuleResponseDetails()
            {
                RewardCycleStartDate = rewardCycleEntity.RewardCycleStartDate,
                RewardCycleEndDate   = rewardCycleEntity.RewardCycleEndDate,
                RewardCycleId        = rewardCycleEntity.CycleId,
            };

            var teamDetails = await this.teamStorageProvider.GetTeamDetailAsync(rewardCycleEntity.TeamId);

            string serviceUrl = teamDetails.ServiceUrl;

            MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
            string teamGeneralChannelId = rewardCycleEntity.TeamId;

            this.logger.LogInformation($"sending notification to channel id - {teamGeneralChannelId}");

            await retryPolicy.ExecuteAsync(async() =>
            {
                try
                {
                    var conversationParameters = new ConversationParameters()
                    {
                        ChannelData = new TeamsChannelData()
                        {
                            Channel = new ChannelInfo()
                            {
                                Id = rewardCycleEntity.TeamId
                            }
                        },
                        Activity = (Activity)MessageFactory.Carousel(NominateCarouselCard.GetAwardNominationCards(this.options.Value.AppBaseUri, awardsList, this.localizer, valuesFromTaskModule)),
                    };

                    Activity mentionActivity = MessageFactory.Text(this.localizer.GetString("NominationReminderNotificationText"));

                    await((BotFrameworkAdapter)this.adapter).CreateConversationAsync(
                        Constants.TeamsBotFrameworkChannelId,
                        serviceUrl,
                        this.microsoftAppCredentials,
                        conversationParameters,
                        async(conversationTurnContext, conversationCancellationToken) =>
                    {
                        await conversationTurnContext.SendActivityAsync(mentionActivity, conversationCancellationToken);
                    },
                        default);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "Error while sending mention card notification to channel.");
                    throw;
                }
            });
        }
Пример #2
0
        /// <summary>
        /// When OnTurn method receives a submit invoke activity on bot turn, it calls this method.
        /// </summary>
        /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param>
        /// <param name="taskModuleRequest">Task module invoke request value payload.</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 represents a task module response.</returns>
        protected override async Task <TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(ITurnContext <IInvokeActivity> turnContext, TaskModuleRequest taskModuleRequest, CancellationToken cancellationToken)
        {
            try
            {
                turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext));

                var activity = (Activity)turnContext.Activity;
                this.RecordEvent(nameof(this.OnTeamsTaskModuleFetchAsync), turnContext);
                Activity mentionActivity;
                var      valuesFromTaskModule = JsonConvert.DeserializeObject <TaskModuleResponseDetails>(((JObject)activity.Value).GetValue("data", StringComparison.OrdinalIgnoreCase)?.ToString());
                switch (valuesFromTaskModule.Command.ToUpperInvariant())
                {
                // Command to send award admin card on save admin action.
                case Constants.SaveAdminDetailsAction:
                    mentionActivity = await CardHelper.GetMentionActivityAsync(
                        valuesFromTaskModule.AdminUserPrincipalName.Split(",").ToList(),
                        turnContext.Activity.From.AadObjectId,
                        valuesFromTaskModule.TeamId,
                        turnContext,
                        this.localizer,
                        this.logger,
                        MentionActivityType.SetAdmin,
                        cancellationToken);

                    var cardDetail = AdminCard.GetAdminCard(this.localizer, valuesFromTaskModule);
                    await this.SendCardAndMentionsAsync(turnContext, cardDetail, mentionActivity);

                    this.logger.LogInformation("Admin has been configured successfully.");

                    break;

                // Command to update award admin card
                case Constants.UpdateAdminDetailCommand:
                    mentionActivity = await CardHelper.GetMentionActivityAsync(
                        valuesFromTaskModule.AdminUserPrincipalName.Split(",").ToList(),
                        turnContext.Activity.From.AadObjectId,
                        valuesFromTaskModule.TeamId,
                        turnContext,
                        this.localizer,
                        this.logger,
                        MentionActivityType.SetAdmin,
                        cancellationToken);

                    var notificationCard = (Activity)MessageFactory.Attachment(AdminCard.GetAdminCard(this.localizer, valuesFromTaskModule));

                    // Split here extracts the activity id from turn context conversation
                    notificationCard.Id           = turnContext.Activity.Conversation.Id.Split(';')[1].Split("=")[1];
                    notificationCard.Conversation = turnContext.Activity.Conversation;
                    await turnContext.UpdateActivityAsync(notificationCard);

                    await turnContext.SendActivityAsync(mentionActivity);

                    this.logger.LogInformation("Admin card is updated successfully.");
                    break;

                // Command to show list of awards ready for nomination
                case Constants.NominateAction:
                    var awardsList = await this.awardsStorageProvider.GetAwardsAsync(valuesFromTaskModule.TeamId);

                    await turnContext.SendActivityAsync(MessageFactory.Carousel(NominateCarouselCard.GetAwardNominationCards(this.options.Value.AppBaseUri, awardsList, this.localizer, valuesFromTaskModule)));

                    this.logger.LogInformation("Nomination carousel card is sent successfully.");
                    break;

                // Command to save nominated user details
                case Constants.SaveNominatedDetailsAction:
                    turnContext.Activity.Conversation.Id = valuesFromTaskModule.TeamId;
                    var endorsementCard = EndorseCard.GetEndorseCard(this.options.Value.AppBaseUri, this.localizer, valuesFromTaskModule);
                    mentionActivity = await CardHelper.GetMentionActivityAsync(
                        valuesFromTaskModule.NomineeUserPrincipalNames.Split(",").Select(row => row.Trim()).ToList(),
                        turnContext.Activity.From.AadObjectId,
                        valuesFromTaskModule.TeamId,
                        turnContext,
                        this.localizer,
                        this.logger,
                        MentionActivityType.Nomination,
                        cancellationToken);

                    await this.SendCardAndMentionsAsync(turnContext, endorsementCard, mentionActivity);

                    this.logger.LogInformation("Award nomination for user sent successfully");
                    break;

                // Commands to close task modules
                case Constants.OkCommand:
                case Constants.CancelCommand:
                    this.logger.LogInformation($"{valuesFromTaskModule.Command.ToUpperInvariant()} is called. [note] - no actions are performed.");
                    break;

                default:
                    this.logger.LogInformation($"Invalid command for task module submit activity.Command is : {valuesFromTaskModule.Command} ");
                    await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage"));

                    break;
                }

                return(null);
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, $"Error at OnTeamsTaskModuleSubmitAsync(): {ex.Message}");
                throw;
            }
        }