Ejemplo n.º 1
0
 public static string GetTrimmedText(this IMessageActivity activity)
 {
     if (activity.Text == null)
     {
         return(null);
     }
     return(activity.RemoveRecipientMention().Trim().Replace(" \n\n ", "\r\n"));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles any incoming command and route them to the appropriate dialog.
        /// </summary>
        /// <param name="context">A <see cref="IDialogContext"/>.</param>
        /// <param name="result">An <see cref="IMessageActivity"/>.</param>
        /// <returns>A <see cref="Task"/>.</returns>
        public virtual async Task HandleCommandAsync(IDialogContext context, IMessageActivity result)
        {
            context.ThrowIfNull(nameof(context));
            result.ThrowIfNull(nameof(result));

            // Message is coming from a group chat, so skipping if the bot is not explicitly mentioned.
            if (result.Conversation.IsGroup.GetValueOrDefault() && !result.MentionsRecipient())
            {
                context.Wait(this.HandleActivityAsync);
                return;
            }

            var dialog = GlobalConfiguration.Configuration.DependencyResolver.Find(result.RemoveRecipientMention());

            context.UserData.TryGetValue("userData", out UserData data);

            if (dialog == null || (data == null && !(dialog is ConnectDialog)))
            {
                if (data == null)
                {
                    data = new UserData();
                }

                var reply = context.MakeMessage();

                reply.Text = result.Text.Trim().Equals(CommandMatchHelp, StringComparison.OrdinalIgnoreCase)
                    ? Labels.Help
                    : Labels.UnknownCommand;

                reply.Attachments.Add(new MainOptionsCard(!string.IsNullOrEmpty(data.TeamProject)));

                await context.PostAsync(reply);

                context.Wait(this.HandleActivityAsync);
            }
            else
            {
                this.telemetryClient.TrackEvent(result.Text);

                await context.Forward(dialog, this.ResumeAfterChildDialog, result, CancellationToken.None);
            }
        }
Ejemplo n.º 3
0
        public virtual async Task HandleCommandAsync(IDialogContext context, IMessageActivity result)
        {
            context.ThrowIfNull(nameof(context));
            result.ThrowIfNull(nameof(result));

            var text = result.RemoveRecipientMention();

            await context.SendTyping();

            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, result))
            {
                this.SetLocale(context, result);

                var dialog = scope.Find(text);

                var reply = context.MakeMessage();

                if (dialog == null)
                {
                    reply.Text = Labels.UnknownCommand;

                    await context.PostAsync(reply);

                    context.Wait(this.HandleActivityAsync);
                }
                else if (!(dialog is ConnectDialog) && !IsConnected(context.UserData))
                {
                    reply.Text = Labels.Connect;

                    await context.PostAsync(reply);

                    context.Wait(this.HandleActivityAsync);
                }
                else
                {
                    await context.Forward(dialog, this.ResumeAfterChildDialog, result, CancellationToken.None);
                }
            }
        }
Ejemplo n.º 4
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;
            }
        }