Reputation: 2434
I was recently developing a chatbot using Microsoft's botframework. I am using prompt.confirm to take user's yes/no input but it shows too many attempts exception when I write basic yes/no. I don't want my bot to display too many attempts exception instead I want to handle it internally. Here's my code.
[LuisIntent("None")]
public async Task NoneIntent(IDialogContext context, LuisResult result)
{
try
{
PromptDialog.Confirm(context, NextQuestionAsync, QuestionPrompt, attempts: 1, promptStyle: PromptStyle.Auto);
}
catch (Exception ex)
{
await context.PostAsync("Something really bad happened.");
}
}
public async Task NextQuestionAsync(IDialogContext context, IAwaitable<bool> result)
{
try
{
if (await result)
{
await context.PostAsync($"Ok, alarm 0 disabled.");
//context.Wait(NoneIntent);
}
else
{
await context.PostAsync("You Said No");
}
}
catch (Exception e)
{
}
}
Upvotes: 1
Views: 2012
Reputation: 14787
You can just override the TooManyAttempts
message by passing your own string in the PromptOptions constructor, which is later used here to display the message.
Also, have in mind that in the case of the TooManyAttempts
exception, the way to handle it is in the try/catch of the ResumeAfter
method (in this case your NextQuestionAsync
method), surrounding the await
and not in the caller method.
Upvotes: 4
Reputation: 2434
I resolved this issue by overriding the PromptOptions constructor, Thanks to ezequiel. I used PromptDialog.Choice to achieve it however I could also have done it with confirm. Here's what I did
List<string> questions = new List<string>();
questions.Add("Yes"); // Added yes option to prompt
questions.Add("No"); // Added no option to prompt
string QuestionPrompt = "Are you sure?";
PromptOptions<string> options = new PromptOptions<string>(QuestionPrompt, "", "", questions, 1); // Overrided the PromptOptions Constructor.
PromptDialog.Choice<string>(context, NextQuestionAsync, options);
Upvotes: 3