forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameDialog.cs
50 lines (44 loc) · 1.74 KB
/
NameDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
namespace BasicMultiDialogBot.Dialogs
{
using Microsoft.Bot.Builder.Dialogs;
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
[Serializable]
public class NameDialog : IDialog<string>
{
private int attempts = 3;
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("What is your name?");
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
/* If the message returned is a valid name, return it to the calling dialog. */
if ((message.Text != null) && (message.Text.Trim().Length > 0))
{
/* Completes the dialog, removes it from the dialog stack, and returns the result to the parent/calling
dialog. */
context.Done(message.Text);
}
/* Else, try again by re-prompting the user. */
else
{
--attempts;
if (attempts > 0)
{
await context.PostAsync("I'm sorry, I don't understand your reply. What is your name (e.g. 'Bill', 'Melinda')?");
context.Wait(this.MessageReceivedAsync);
}
else
{
/* Fails the current dialog, removes it from the dialog stack, and returns the exception to the
parent/calling dialog. */
context.Fail(new TooManyAttemptsException("Message was not a string or was an empty string."));
}
}
}
}
}