Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

NameDialog.cs 1.7 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...