如何在一个机器人中调用集成简单对话框和QnA制造商对话框?

时间:2018-03-14 20:02:10

标签: botframework chatbot qnamaker

首先我要把手机号码作为用户输入,而不是我需要拨打qnadialog直到用户退出。以下是我的代码:

public class RootDialog : IDialog<object>
{
    private string phoneNo;
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result;

        await this.SendWelcomeMessageAsync(context);
    }
    private async Task SendWelcomeMessageAsync(IDialogContext context)
    {
        if (string.IsNullOrEmpty(phoneNo))
        {
            await context.PostAsync("Hi, I'm Anna. Let's get started.");
            context.Call(new PhoneNoDialog(), this.PhoneNoDialogResumeAfter);
        }
        else
        {
            await context.Forward(new SimpleQnADialog(), ResumeAfterSimpleQnADialog, context.Activity, CancellationToken.None);                
        }
    }
    private async Task PhoneNoDialogResumeAfter(IDialogContext context, IAwaitable<string> result)
    {
            this.phoneNo = await result;
            await context.PostAsync($"Thank you for the information. How can I help you?");
            context.Wait(this.MessageReceivedAsync);
    }

    private async Task ResumeAfterSimpleQnADialog(IDialogContext context, IAwaitable<object> result)
    {
            context.Done<object>(null);            
    }
}

PhoneNoDialog.cs

public class PhoneNoDialog : IDialog<string>
{
    private int attempts = 3;

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Please enter your phone no");

        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;

        if ((message.Text != null) && (message.Text.Trim().Length > 0))
        {
            context.Done(message.Text);
        }
        else
        {
            --attempts;
            if (attempts > 0)
            {
                await context.PostAsync("I'm sorry, I don't understand your reply. What is your phone no?");
                context.Wait(this.MessageReceivedAsync);
            }
            else
            {
                context.Fail(new TooManyAttemptsException("Message was not a string or was an empty string."));
            }
        }
    }
}

SimpleQnADialog.cs

[QnAMaker("subkey", "kbid")]
public class SimpleQnADialog : QnAMakerDialog
{

}

如果我使用QnAmaker创建独立的bot,但如果我以上述方式调用上下文,那么一切正常,而不是按预期工作。我不知道我在哪里出错了。而且,很多时候僵尸模拟器都会出现意想不到的异常。

1 个答案:

答案 0 :(得分:1)

您在QnA收到回复后尝试完成RootDialog

private async Task ResumeAfterSimpleQnADialog(IDialogContext context, IAwaitable<object> result)
{
        context.Done<object>(null);            
}

事实上,QnAMakerDialog一旦尝试回答就会完成,正如您在其来源here上看到的那样。

如果你想循环问题,你应该再次致电等待:

private async Task ResumeAfterSimpleQnADialog(IDialogContext context, IAwaitable<object> result)
{
    context.Wait(this.MessageReceivedAsync);
}