使用context.call/done链接多个对话框

时间:2017-01-16 06:27:48

标签: c# bots botframework

我有一个LuisDialog,如果找到了意图,它会调用特定的对话框。在那里,在提取的实体的基础上,我正在调用另一个对话框,它似乎抛出了一些未处理的异常。 这是我的根LuiDialog调用BuyDialog的意图(我将LuisResult保存起来供以后使用) -

[LuisIntent("Buy")]
public async Task Buy(IDialogContext context,  LuisResult result)
{
   var cts = new CancellationTokenSource();
   await context.PostAsync("you want to buy");
   context.PrivateConversationData.SetValue<LuisResult>("luis", result);
   context.Call(new BuyDialog(), ResumeAfterBuyDialog);
}
 private  async Task ResumeAfterBuyDialog(IDialogContext context, IAwaitable<bool> result)
    {
        var success = await result;
        if (success)
        {
            await context.PostAsync("How else can I help you?");
            context.Wait(MessageReceived);
          }
    }

这是我的BuyDialog调用另一个对话框(BookDialog),如果实体包含&#34; book&#34;串 -

else if(is_book)
                {
                    await context.PostAsync("You selected book category");
                    context.Call(new BookDialog(),BookFormComplete);
                    context.Done(true);
                }
 private async Task BookFormComplete(IDialogContext context, IAwaitable<bool> result)  
    {
        var BookResult = await result;
        //do something if result is true
    }

这是我的BookDialog -

[Serializable]
public class BookDialog : IDialog<object>
{
    public  async Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);
    }
    public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var message = await argument;
        if (message.Text == "mtl100")
        {
            await context.PostAsync("Correct");
            context.Done(true);
        }
        else
        {
            await context.PostAsync("Please enter valid course code");
            context.Wait(MessageReceivedAsync);
        }

我知道问题出在第二个context.call(对bookDialog的调用),因为如果我删除它,代码就可以正常工作。现在,我得到了#抱歉,我的机器人代码出了问题&#34;当它到达那里。知道什么是错的吗?

1 个答案:

答案 0 :(得分:3)

您在第二个对话框中执行的context.Done(true)必须采用BookDialog ResumeAfter<T>方法(BookFormComplete

“Is Book”路径应如下所示:

else if(is_book)
{
    await context.PostAsync("You selected book category");
    context.Call(new BookDialog(),BookFormComplete);
}