2017-07-10 40 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

相關問題