Microsoft Bot Framework Chain.While示例

时间:2016-09-29 00:41:54

标签: c# botframework

我试图在多次迭代中循环一个对话框。

示例:我想连续5次提问(使用不同的参数)。我可以使用MyDialog.Loop()循环我的对话无限 - 没问题。

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

到目前为止还没有成功,我找不到任何使用Chain.While()的例子。

非常感谢任何帮助或示例代码。

谢谢!

2 个答案:

答案 0 :(得分:0)

以下是一个显示电子邮件链的示例,使用While来询问电子邮件收件人:

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();

答案 1 :(得分: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"
                );
        }
    }