问题描述
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),cancellationToken);
case "Team_erstellen":
return await stepContext.BeginDialogAsync(nameof(CreateTeamDialog),cancellationToken);
case "DIGAS":
return await stepContext.BeginDialogAsync(nameof(DIGASDialog),cancellationToken);
case "Begrueßung":
return await stepContext.BeginDialogAsync(nameof(GreetingDialog),cancellationToken);
case "Onboarding":
return await stepContext.BeginDialogAsync(nameof(OnboardingDialog),cancellationToken);
default:
await stepContext.Context.SendActivityAsync("Das habe ich nicht verstanden.");
return await stepContext.BeginDialogAsync(nameof(HelpDialog),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;
}
}
在提示步骤之后,直到我提供输入后,下一步才运行。我或多或少从Microsoft复制了AuthBot示例并对其进行了一些修改。
在“提示”步骤中,用户进行身份验证,但提示消失后,机器人将等待更多输入,而不是跳到下一步。
我在其中复制代码的机器人:
我无法测试样本,因为它不能自行运行。 当我使用ngrok隧道在仿真器中测试我的机器人时,已收到令牌,但该机器人只是退出并等待输入。
为什么提示后没有启动LoginStep?
解决方法
我认为您也应该添加ChoicePrompt:
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));