Ejemplo n.º 1
0
        /// <summary>
        /// Handle message activity in channel.
        /// </summary>
        /// <param name="message">A message in a conversation.</param>
        /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        private async Task OnMessageActivityInChannelAsync(
            IMessageActivity message,
            ITurnContext <IMessageActivity> turnContext,
            CancellationToken cancellationToken)
        {
            try
            {
                if (message.Type.Equals(ActivityTypes.Message, StringComparison.OrdinalIgnoreCase))
                {
                    string actionType = message.Value != null?JObject.Parse(message.Value.ToString())["AdaptiveActionType"]?.ToString() : null;

                    string scrumMembers = message.Value != null?JObject.Parse(message.Value.ToString())["ScrumMembers"]?.ToString() : null;

                    message.RemoveRecipientMention();
                    string text = string.IsNullOrEmpty(message.Text) ? actionType : message.Text;

                    switch (text.ToUpperInvariant().Trim())
                    {
                    case Constants.EndScrum:
                        string conversationId = message.Conversation.Id;
                        string scrumMasterId  = JObject.Parse(message.Value.ToString())["ScrumMasterId"].ToString();
                        var    scrumInfo      = await this.scrumHelper.GetActiveScrumAsync(scrumMasterId);

                        var activitySummary = await this.activityHelper.GetEndScrumSummaryActivityAsync(scrumInfo, conversationId, scrumMembers, turnContext, cancellationToken);

                        if (activitySummary != null)
                        {
                            this.logger.LogInformation($"Scrum completed by: {turnContext.Activity.From.Name} for {conversationId} with ScrumStartCardResponseId: {scrumInfo.ScrumStartCardResponseId}");
                            await turnContext.UpdateActivityAsync(activitySummary, cancellationToken);

                            await turnContext.SendActivityAsync(this.localizer.GetString("SuccessMessageAfterEndingScrum"), cancellationToken : cancellationToken);
                        }

                        break;

                    case Constants.Help:
                        this.logger.LogInformation("Sending help card");
                        var helpAttachment = HelpCard.GetHelpCard(this.localizer);
                        await turnContext.SendActivityAsync(MessageFactory.Attachment(helpAttachment), cancellationToken);

                        break;

                    case Constants.Settings:
                        this.logger.LogInformation("Sending settings button card");
                        var settingsAttachment = SettingsCard.GetSettingsCard(this.localizer);
                        await turnContext.SendActivityAsync(MessageFactory.Attachment(settingsAttachment), cancellationToken);

                        break;

                    default:
                        this.logger.LogInformation("Invalid command text entered in channel. Sending help card");
                        var helpAttachmentcard = HelpCard.GetHelpCard(this.localizer);
                        await turnContext.SendActivityAsync(MessageFactory.Attachment(helpAttachmentcard), cancellationToken);

                        break;
                    }
                }
                else
                {
                    await turnContext.SendActivityAsync(this.localizer.GetString("InformationAboutBotInstallationLimitation"), cancellationToken : cancellationToken);
                }
            }
            catch (Exception ex)
            {
                await turnContext.SendActivityAsync(this.localizer.GetString("ErrorMessage"), cancellationToken : cancellationToken);

                this.logger.LogError(ex, $"Error processing message: {ex.Message}", SeverityLevel.Error);
            }
        }
        /// <summary>
        /// Handle message activity in channel.
        /// </summary>
        /// <param name="message">A message in a conversation.</param>
        /// <param name="turnContext">Context object containing information cached for a single turn of conversation with a user.</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        private async Task OnMessageActivityInChannelAsync(
            IMessageActivity message,
            ITurnContext <IMessageActivity> turnContext,
            CancellationToken cancellationToken)
        {
            string actionType = message.Value != null?JObject.Parse(message.Value.ToString())["AdaptiveActionType"]?.ToString() : null;

            string scrumMembers = message.Value != null?JObject.Parse(message.Value.ToString())["ScrumMembers"]?.ToString() : null;

            message.RemoveRecipientMention();
            string text = string.IsNullOrEmpty(message.Text) ? actionType : message.Text;

            switch (text.ToUpperInvariant().Trim())
            {
            case Constants.EndScrum:     // command to handle end scrum.
                string conversationId    = message.Conversation.Id;
                string scrumTeamConfigId = JObject.Parse(message.Value.ToString())["ScrumTeamConfigId"].ToString();
                string aadGroupId        = await this.activityHelper.GetTeamAadGroupIdAsync(turnContext, cancellationToken);

                var scrumInfo = await this.scrumHelper.GetActiveScrumAsync(scrumTeamConfigId, aadGroupId);

                if (scrumInfo == null || scrumInfo.IsCompleted)
                {
                    await turnContext.SendActivityAsync(string.Format(CultureInfo.CurrentCulture, this.localizer.GetString("ErrorScrumDoesNotExist"), turnContext.Activity.From.Name), cancellationToken : cancellationToken);

                    break;
                }

                scrumInfo.IsCompleted          = true;
                scrumInfo.ThreadConversationId = conversationId;
                var scrumSaveResponse = await this.scrumStorageProvider.CreateOrUpdateScrumAsync(scrumInfo);

                if (!scrumSaveResponse)
                {
                    this.logger.LogError("Error in saving scrum information in storage.");
                    await turnContext.SendActivityAsync(this.localizer.GetString("ErrorSavingScrumData"), cancellationToken : cancellationToken);

                    break;
                }

                var activitySummary = await this.activityHelper.GetEndScrumSummaryActivityAsync(scrumInfo, conversationId, scrumMembers, turnContext, cancellationToken);

                if (activitySummary != null)
                {
                    this.logger.LogInformation($"Scrum completed by: {turnContext.Activity.From.AadObjectId} for {conversationId} with ScrumStartCardResponseId: {scrumInfo.ScrumStartCardResponseId}");
                    await turnContext.UpdateActivityAsync(activitySummary, cancellationToken);

                    await turnContext.SendActivityAsync(this.localizer.GetString("SuccessMessageAfterEndingScrum"), cancellationToken : cancellationToken);
                }

                break;

            case Constants.Help:     // command to show help card.
                this.logger.LogInformation("Sending help card");
                var helpAttachment = HelpCard.GetHelpCard(this.localizer);
                await turnContext.SendActivityAsync(MessageFactory.Attachment(helpAttachment), cancellationToken);

                break;

            case Constants.Settings:     // Command to show adaptive card with settings CTA button.
                this.logger.LogInformation("Sending settings button card");
                var settingsAttachment = SettingsCard.GetSettingsCard(this.localizer);
                await turnContext.SendActivityAsync(MessageFactory.Attachment(settingsAttachment), cancellationToken);

                break;

            default:
                this.logger.LogInformation("Invalid command text entered in channel. Sending help card");
                var helpAttachmentcard = HelpCard.GetHelpCard(this.localizer);
                await turnContext.SendActivityAsync(MessageFactory.Attachment(helpAttachmentcard), cancellationToken);

                break;
            }
        }