微软Bot框架链.当例子
本文关键字:Bot 框架 微软 | 更新日期: 2023-09-27 18:09:35
我正在尝试在多次迭代中循环一个对话框。
例子:我喜欢连续问一个问题5次(不同的参数)。我能够循环我的对话框无限与MyDialog.Loop()
-没有问题。
但我需要一个有限的数字-我认为我需要使用Chain.While()
。
到目前为止没有成功,我找不到任何使用Chain.While()
的例子。
任何帮助或示例代码是非常感激的。
谢谢!
下面是一个固定计数while循环的例子:
[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"
);
}
}
下面是一个示例,显示了一个电子邮件链,使用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();