First I've to take phone no as a user input, than I need to call qnadialog till user quits. Following is my code:
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
{
}
Everything works fine if I create independent bot with QnAmaker but if I call the context in above mentioned way, than it doesn't work as expected. I am not sure where I'm going wrong with this. And also, many a times bot emulator gives unexpected exceptions.