首页 > 解决方案 > Bot 在 OAuthPrompt 之后仍在等待

问题描述

我有以下代码:

public class MainDialog : ComponentDialog
    {
        protected readonly IConfiguration Configuration;
        protected readonly ILogger Logger;
        protected int counter = 0;
        protected bool HaveToken = false;
        protected static string Token = "";
  

        public MainDialog(IConfiguration configuration, ILogger<MainDialog> logger)
            : base(nameof(MainDialog))
        {   
            //cache the config from appstettings.json for later usage in LUIS & QnAMaker.
            Configuration = configuration;
            Logger = logger;

            AddDialog(new OAuthPrompt(
              nameof(OAuthPrompt),
               new OAuthPromptSettings
               {
                   ConnectionName = Configuration["ConnectionName"],
                   Text = "Bitte melden sie sich an.",
                   Title = "Login",
                   Timeout = 3000,
               }));

            AddDialog(new TextPrompt(nameof(TextPrompt)));
            //Adding Dialogs and giving the Dialogs the config info for the QnAMaker connection.
            AddDialog(new HelpDialog(Configuration, logger));
            AddDialog(new SpellingDialog(Configuration, logger));
            AddDialog(new CreateTeamDialog(Configuration, logger));
            AddDialog(new DIGASDialog(Configuration, logger));
            AddDialog(new GreetingDialog(Configuration, logger));
            AddDialog(new OnboardingDialog(Configuration, logger));
            //AddDialog(new LoginDialog(Configuration, logger));
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                PromptStepAsync,
                LoginStepAsync,
                ProcessStepAsync,
                IntroStepAsync,
                ActStepAsync,
                FinalStepAsync
            }));
            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);

        }

        private async Task<DialogTurnResult> PromptStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {

            return await stepContext.BeginDialogAsync(nameof(OAuthPrompt),null, cancellationToken);
          
           
        }

        private async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (HaveToken)
            {
                return await stepContext.NextAsync(cancellationToken);
            }
            
            if(stepContext.Result.GetType() == typeof(CancellationToken) )
            {
                return await stepContext.EndDialogAsync();
            }
            try
            {
                var tokenResponse = (TokenResponse)stepContext.Result;
                Token = tokenResponse.Token;
                HaveToken = true;
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("You are now logged in."), cancellationToken);
                //await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your token is: {tokenResponse.Token}"), cancellationToken);

                //await OAuthhelper.ListMeAsync(stepContext.Context, tokenResponse);

                return await stepContext.NextAsync(cancellationToken);
            }
            catch (Exception x)
            {
               
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("Login was not successful please try again.   " + x.Message), cancellationToken);
                return await stepContext.EndDialogAsync();
            }
             

        }
        private async Task<DialogTurnResult> ProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (HaveToken)
            {
                return await stepContext.NextAsync(cancellationToken);
            }
            if (stepContext.Result != null)
            {
                // We do not need to store the token in the bot. When we need the token we can
                // send another prompt. If the token is valid the user will not need to log back in.
                // The token will be available in the Result property of the task.
                var tokenResponse = stepContext.Result as TokenResponse;

                // If we have the token use the user is authenticated so we may use it to make API calls.
                if (tokenResponse?.Token != null)
                {
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your token is: {tokenResponse.Token}"), cancellationToken);

                }
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("We couldn't log you in. Please try again later."), cancellationToken);
            }

            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }
        private async Task<DialogTurnResult> IntroStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            return await stepContext.ContinueDialogAsync(cancellationToken);
        }

        private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var intentDetails = new QueryDetails();

            // Call LUIS and gather any potential booking details. (Note the TurnContext has the response to the prompt.)
            intentDetails = stepContext.Result != null
                    ?
                await LuisHelper.ExecuteLuisQuery(Configuration, Logger, stepContext.Context, cancellationToken)
                    :
                new QueryDetails();

            switch (intentDetails.Intent)
            {
                case "Hilfe":
                    return await stepContext.BeginDialogAsync(nameof(HelpDialog), intentDetails, cancellationToken);
                case "Schreibweise_Ausdruck":
                    return await stepContext.BeginDialogAsync(nameof(SpellingDialog), intentDetails, cancellationToken);
                case "Team_erstellen":
                    return await stepContext.BeginDialogAsync(nameof(CreateTeamDialog), intentDetails, cancellationToken);
                case "DIGAS":
                    return await stepContext.BeginDialogAsync(nameof(DIGASDialog), intentDetails, cancellationToken);
                case "Begrueßung":
                    return await stepContext.BeginDialogAsync(nameof(GreetingDialog), intentDetails, cancellationToken);
                case "Onboarding":
                    return await stepContext.BeginDialogAsync(nameof(OnboardingDialog), intentDetails, cancellationToken);
                default:
                    await stepContext.Context.SendActivityAsync("Das habe ich nicht verstanden.");
                    return await stepContext.BeginDialogAsync(nameof(HelpDialog), intentDetails, cancellationToken);
                    
            }
        }
        private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            //await stepContext.Context.SendActivityAsync(MessageFactory.Text("Vielen Dank."), cancellationToken);
            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }

        public static string getToken()
        {
            return Token;
        }
    }

在 Promptstep 之后,在我输入之前不会运行下一步。我或多或少地从 Microsoft 复制了 AuthBot 示例并对其进行了一些修改。

在 Promptstep 中,用户对自己进行身份验证,但在提示消失后,Bot 等待更多输入,而不是跳到下一步。

机器人,我从中复制了代码:

https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/csharp_dotnetcore/46.teams-auth/Dialogs/MainDialog.cs

我无法测试样本,因为它不能自行运行。当我在模拟器中使用 ngrok 隧道测试我的机器人时,收到了令牌,但机器人只是退出并等待输入。

这是发生的事情: 这是它在聊天中的样子

为什么 LoginStep 在提示后没有启动?

标签: c#botframework

解决方案


我认为您也应该添加 ChoicePrompt :

AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));

推荐阅读