2017-07-10 38 views
1

我想着手的机器人的其余功能,之前对用户进行验证,并有这样的代码,以确保用户存在对话框返回到父对话框API后得到调用,而不是继续当前对话框

[Serializable] 
public class ModifyLicenceDialog: IDialog<object> 
{ 
    private const string CreateLicence = "Create a new licence"; 
    private const string ModifyEndDate = "Modify end date"; 
    private const string CancelLicence = "Cancel a licence"; 

    public async Task StartAsync(IDialogContext context) 
    { 
     if (!CommonConversation.User.IsAuthorized) 
     { 
      context.Call(new AuthDialog(), ResumeAfterAuth); 
     } 
    } 

    private async Task ResumeAfterAuth(IDialogContext context, IAwaitable<object> result) 
    { 
     await result; 
     PromptDialog.Choice(context, ResumeAfterChoice, new[] { CreateLicence, ModifyEndDate, CancelLicence }, 
      "What licence function would you like?", 
      "I am sorry i did not understand that, please select one of the below options"); 
    } 

    private async Task ResumeAfterChoice(IDialogContext context, IAwaitable<string> result) 
    { 
     string selected = await result; 

     switch (selected) 
     { 
      case CreateLicence: 
       context.Call(new CreateLicenseDialog(), AfterLicenseDialog(CreateLicence)); 
       break; 
      case ModifyEndDate: 
       break; 
      case CancelLicence: 
       break; 

     } 
    } 

    private ResumeAfter<object> AfterLicenseDialog(IDialogContext context, string licenseEvent) 
    { 
     switch (licenseEvent) 
     { 
      case CancelLicence: 
       await context.PostAsync("Auth done"); 
       context.Done(licenseEvent); 
       break; 

     } 
    } 

然而,当我的代码进入AuthDialog时,当它尝试调用api以获取具有提供的电子邮件的用户是否存在(specifically at res = await client.GetAsync(uri)),一旦api返回结果,机器人返回到父对话框(ModifyLicenceDialog)并执行ResumeAfterAuth

[Serializable] 
    public class AuthDialog: IDialog<object> 
    { 
     private const string InputEmail = "Please input your email address to continue."; 
     private int _attempts = 3; 

     public async Task StartAsync(IDialogContext context) 
     { 
      await context.PostAsync(InputEmail); 
      context.Wait(MessageReceivedAsync); 
     } 

     private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result) 
     { 
      IMessageActivity activity = await result; 
      string email = activity.Text; 

      await context.PostAsync("Please wait while we verify this..."); 

      HttpResponseMessage res = new HttpResponseMessage(); 
      HttpClient client = AuthFunctions.Client; 

      string uri = $"api/v1/auth/licenses/{email}"; 

      res = await client.GetAsync(uri); 

      if (res.IsSuccessStatusCode) 
      { 
       CommonConversation.User = JsonConvert.DeserializeObject<CustomerUser>(await res.Content.ReadAsStringAsync()); 
       await context.PostAsync($"Welcome {CommonConversation.User.FirstName}!"); 
       context.Done(email); 
      } 
      else if (_attempts > 0) 
      { 
       _attempts--; 

       await context.PostAsync("I am sorry we do not recognize that email address, lets try again. Please input your email address"); 
       context.Wait(MessageReceivedAsync); 
      } 
      else 
      { 
       context.Fail(new Exception("I am afraid you are not an authorized user")); 
      } 
     } 



    } 
} 

更新尼古拉斯 - [R一些指导之后事实证明,在这一点上抛出一个异常 的例外是如下

{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."} 
" at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at SupportHelpdeskBot.Dialogs.AuthDialog.<MessageReceivedAsync>d__3.MoveNext() in C:\\Workspace\\SupportBot\\SupportHelpdeskBot\\SupportHelpdeskBot\\Dialogs\\AuthDialog.cs:line 41" 

我试图System.Net.ServicePointManager.Expect100Continue = false;来阻止这种情况发生,但这种处理不当工作的任何suggesitons?

+0

尝试使用Context.Forward()而不是Context.Call():'await context.Forward(new PaymentDialog(),ResumeAfterPaymentDialog,context.Activity.AsMessageActivity(),CancellationToken.None); ' – JasonSowers

+0

嗨,感谢您的回复,使用转发后,即使在AuthDialog StartAsync也不会等待。 – Harry

+0

值得一试 – JasonSowers

回答

1

。在你执行没有错误:你与你的context.Done(email)完成您AuthDialog这里:

if (res.IsSuccessStatusCode) 
{ 
    CommonConversation.User = JsonConvert.DeserializeObject<CustomerUser>(await res.Content.ReadAsStringAsync()); 
    await context.PostAsync($"Welcome {CommonConversation.User.FirstName}!"); 
    context.Done(email); 
} 

这是正常行为。你想要实现什么?我认为你的问题是不完整的

+0

不,很抱歉,如果它不清楚从我的问题,但问题是它永远不会达到res = await client.GetAsync(uri)后的任何代码;一旦请求完成而不是继续,就像你指出if语句返回到父对话框一样。 – Harry

+0

您是否在调试时添加了“res”的外观?你还可以把你的'ModifyLicenceDialog'的所有代码都放进去吗? –

+0

是的,res会返回一个带有预期json对象的200响应,给我一秒,而我更新ModifyLicenceDialog的其余部分 – Harry

相关问题