Beispiel #1
0
        /// <summary>
        /// OnExtractHistoryFetchTaskAsync
        /// </summary>
        /// <param name="turnContext">turnContext</param>
        /// <param name="action">action</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
        public async Task <MessagingExtensionActionResponse> OnExtractHistoryFetchTaskAsync(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken)
        {
            var attachment = AdaptiveTextCard(Resources.Strings.UnsupportedConversation);

            if (turnContext.IsSupportedConversation())
            {
                if (action.CommandId == CommandId)
                {
                    // Auth checking. Login form will be opened if auth required
                    var isAuthenticated = await IsAuthenticated(turnContext, cancellationToken);

                    if (!isAuthenticated)
                    {
                        return(await Authenticate(turnContext, action, cancellationToken));
                    }
                    else if (await turnContext.IsBotAddedToTheConversationAsync())
                    {
                        attachment = ExtractHistoryMessagingExtensionCard.Generate(turnContext, configuration.ReportFormats);
                    }
                    else
                    {
                        attachment = ExtractHistoryMessagingExtensionCard.GenerateInstallCard(turnContext);
                    }
                }
            }

            return(new MessagingExtensionActionResponse
            {
                Task = this.TaskModuleReportCardTask(turnContext, attachment),
            });
        }
Beispiel #2
0
        /// <summary>
        /// Invoked when members other than this bot (like a user) are added to the conversation when the base behavior of
        /// <see cref="M:Microsoft.Bot.Builder.ActivityHandler.OnConversationUpdateActivityAsync(Microsoft.Bot.Builder.ITurnContext{Microsoft.Bot.Schema.IConversationUpdateActivity},System.Threading.CancellationToken)" /> is used.
        /// If overridden, this could potentially send a greeting message to the user instead of waiting for the user to send a message first.
        /// By default, this method does nothing.
        /// </summary>
        /// <param name="teamsMembersAdded">teamsMembersAdded</param>
        /// <param name="teamInfo">teamInfo</param>
        /// <param name="turnContext">turnContext</param>
        /// <param name="cancellationToken">cancellationToken</param>
        protected override async Task OnTeamsMembersAddedAsync(IList <TeamsChannelAccount> teamsMembersAdded, TeamInfo teamInfo, ITurnContext <IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            if (turnContext.IsSupportedConversation())
            {
                if (teamInfo != null)
                {
                    foreach (var member in teamsMembersAdded)
                    {
                        try
                        {
                            if (member.Id == turnContext.Activity.Recipient.Id)
                            {
                                // Bot itself added to the team, skipping it.
                                continue;
                            }

                            var message = MessageFactory.Text($"Hey new member! I am a chat history bot. You have been added to the team '{teamInfo.Name}'. You can ask me to prepare message history reports. To start the process, just mention my name in a channel.");
                            await privateChat.SendPersonalMessageTo(turnContext, message, cancellationToken, member);
                        }
                        catch (Exception ex)
                        {
                            this.Logger.LogError("Welcome message sending error", ex);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// This is handler for MessagingExtensionSubmitAction event inside bot for current extension.
        /// Should be called inside bot OnTurnAsync method for MessagingExtensionSubmitAction event.
        /// This methods check a commandId parameter inside context and runs task module if commandId matches current extension commandId.
        /// Triggers report preparing after report settings card submit.
        /// </summary>
        /// <param name="turnContext">The turn context.</param>
        /// <param name="action">Action.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public async Task <MessagingExtensionActionResponse> OnExtractHistorySubmitAction(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionAction action, CancellationToken cancellationToken)
        {
            var attachment = AdaptiveTextCard(Resources.Strings.UnsupportedConversation);

            // is this supported conversation
            if (turnContext.IsSupportedConversation())
            {
                if (action.CommandId == CommandId)
                {
                    if (IsBotJustInstalled(turnContext))
                    {
                        attachment = ExtractHistoryMessagingExtensionCard.Generate(turnContext, configuration.ReportFormats);
                    }
                    else
                    {
                        var isignOutAction = turnContext.GetData()?["action"]?.Value <string>() == ExtractHistoryMessagingExtensionCard.SignOutAction;
                        if (isignOutAction)
                        {
                            return(await SignOut(turnContext, cancellationToken));
                        }

                        var reportParameters = await GetReportParametersAsync(turnContext);

                        attachment = AdaptiveTextCard(Resources.Strings.MessageExtFilePreparingMessage);

                        // run report preparing as background task because we need close task module for teams.
                        this.queue.QueueBackgroundTask(async token =>
                        {
                            using (var scope = this.serviceScopeFactory.CreateScope())
                            {
                                try
                                {
                                    var file = await HistoryReportService.PrepareReportInOneDrive(turnContext, reportParameters, cancellationToken);
                                    var fileIsReadyMessage = AdaptiveCardsHelper.GetPersonalFileCard(file, Resources.Strings.DialogReportReadyMessage);
                                    fileIsReadyMessage.AddMentionToText(turnContext.Activity.From);
                                    await privateChat.SendPersonalMessage(turnContext, fileIsReadyMessage, cancellationToken);

                                    var updMessage = MessageFactory.Text(MessageHelper.BuildUserExportedHistoryMessage(turnContext, reportParameters));
                                    await turnContext.SendActivityAsync(updMessage, cancellationToken);
                                }
                                catch (Exception ex)
                                {
                                    logger.LogError("Report generation error", ex);
                                    var message = MessageFactory.Text("Error happened during report genaration. Please, try again.");
                                    message.AddMentionToText(turnContext.Activity.From);
                                    await privateChat.SendPersonalMessage(turnContext, message, cancellationToken);
                                }
                            }
                        });
                    }
                }
            }

            return(new MessagingExtensionActionResponse
            {
                Task = this.TaskModuleReportCardTask(turnContext, attachment),
            });
        }
Beispiel #4
0
 /// <summary>
 /// Invoked when a message activity is received from the user when the base behavior of
 /// <see cref="!:OnTurnAsync(ITurnContext&lt;IConversationUpdateActivity&gt;, CancellationToken)" /> is used.
 /// If overridden, this could potentially contain conversational logic.
 /// By default, this method does nothing.
 /// </summary>
 /// <param name="turnContext">The context object for this turn.</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects
 /// or threads to receive notice of cancellation.</param>
 protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
 {
     if (turnContext.IsSupportedConversation())
     {
         Logger.LogInformation("Running dialog with Message Activity.");
         var state = ConversationState.CreateProperty <DialogState>(nameof(DialogState));
         await Dialog.RunAsync(turnContext, state, cancellationToken);
     }
     else
     {
         await SendUnsupportedConverationMessage(turnContext);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Called asynchronous when request is a signin state verification query.
        /// </summary>
        /// <param name="turnContext">The turn context.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        protected override async Task OnTeamsSigninVerifyStateAsync(ITurnContext <IInvokeActivity> turnContext, CancellationToken cancellationToken)
        {
            if (turnContext.IsSupportedConversation())
            {
                Logger.LogInformation("Running dialog with signin/verifystate from an Invoke Activity.");

                // The OAuth Prompt needs to see the Invoke Activity in order to complete the login process.

                // Run the Dialog with the new Invoke Activity.
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty <DialogState>(nameof(DialogState)), cancellationToken);
            }
            else
            {
                await SendUnsupportedConverationMessage(turnContext);
            }
        }