2016-09-29 51 views
2

我正在嘗試在一些迭代中循環對話框。Microsoft Bot框架鏈。雖然示例

例如:我喜歡連續詢問5次問題(使用不同的參數)。我可以使用MyDialog.Loop()來循環我的對話框無限 - 沒問題。

但我需要一個有限的數字 - 我想我需要使用Chain.While()

到目前爲止沒有成功,我找不到使用Chain.While()的任何示例。

任何幫助或示例代碼非常讚賞。

謝謝!

回答

0

這裏是一個固定數的例子,而循環:

[TestMethod] 
    public async Task SampleChain_While_Count() 
    { 
     var root = 
      Chain 
      .PostToChain() 
      .Select(_ => (IReadOnlyList<string>)Array.Empty<string>()) 
      .While 
      (
       items => Chain 
          .Return(items) 
          .Select(i => i.Count < 3), 
       items => Chain 
          .Return(items) 
          .Select(i => $"question {i.Count}") 
          .PostToUser() 
          .WaitToBot() 
          .Select(a => items.Concat(new[] { a.Text }).ToArray()) 
      ) 
      .Select(items => string.Join(",", items)) 
      .PostToUser(); 

     using (var container = Build(Options.ResolveDialogFromContainer | Options.Reflection)) 
     { 
      var builder = new ContainerBuilder(); 
      builder 
       .RegisterInstance(root) 
       .As<IDialog<object>>(); 
      builder.Update(container); 

      await AssertScriptAsync(container, 
       "hello", 
       "question 0", 
       "A", 
       "question 1", 
       "B", 
       "question 2", 
       "C", 
       "A,B,C" 
       ); 
     } 
    } 
0

在這裏是一個示例示出了電子郵件鏈,使用雖然請求輸入的電子郵件的收件人:

Func<string, IDialog<string>> Ask = toUser => 
    Chain.Return(toUser) 
    .PostToUser() 
    .WaitToBot() 
    .Select(m => m.Text); 

IDialog<IReadOnlyList<string>> recipientsDialog = 
    Chain 
    .Return(Array.Empty<string>()) 
    .While(items => Ask($"have {items.Length} recipients, want more?").Select(text => text == "yes"), 
    items => Ask("next recipient?").Select(item => items.Concat(new[] { item }).ToArray())); 

var emailDialog = from hello in Chain.PostToChain().Select(m => m.Text + " back!").PostToUser() 
        from subject in Ask("what is the subject?") 
        from body in Ask("what is the body?") 
        from recipients in recipientsDialog 
        select new { subject, body, recipients }; 

var rootDialog = emailDialog 
    .Select(email => $"'{email.subject}': '{email.body}' to {email.recipients.Count} recipients") 
    .PostToUser(); 
+0

這是有意義當我們收集用戶輸入以確定是否繼續循環時,我喜歡做什麼(以及需要幫助)是如何修改此代碼以表現得像簡單的While(i = - ; i <5;我++)循環。無需提示用戶的對話框。 概念是MyDialog.While(從0開始,直到5,增加1)。 謝謝你的幫助 – gabics

相關問題