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

CancelablePromptChoice.cs 3.2 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  1. namespace ContosoFlowers.BotAssets.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using ContosoFlowers.BotAssets.Properties;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Connector;
  9. [Serializable]
  10. public class CancelablePromptChoice<T> : PromptDialog.PromptChoice<T>
  11. {
  12. private static IEnumerable<string> cancelTerms = new[] { "Cancel", "Back", "B", "Abort" };
  13. private new readonly CancelablePromptOptions<T> promptOptions;
  14. public CancelablePromptChoice(CancelablePromptOptions<T> promptOptions)
  15. : base(promptOptions)
  16. {
  17. this.promptOptions = promptOptions;
  18. }
  19. public CancelablePromptChoice(IEnumerable<T> options, string prompt, string cancelPrompt, string retry, int attempts, PromptStyle promptStyle = PromptStyle.Auto)
  20. : this(new CancelablePromptOptions<T>(prompt, cancelPrompt, retry, options: options.ToList(), attempts: attempts, promptStyler: new PromptStyler(promptStyle)))
  21. {
  22. }
  23. public static void Choice(IDialogContext context, ResumeAfter<T> resume, IEnumerable<T> options, string prompt, string cancelPrompt = null, string retry = null, int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto)
  24. {
  25. Choice(context, resume, new CancelablePromptOptions<T>(prompt, cancelPrompt, retry, attempts: attempts, options: options.ToList(), promptStyler: new PromptStyler(promptStyle)));
  26. }
  27. public static void Choice(IDialogContext context, ResumeAfter<T> resume, CancelablePromptOptions<T> promptOptions)
  28. {
  29. var child = new CancelablePromptChoice<T>(promptOptions);
  30. context.Call(child, resume);
  31. }
  32. public static bool IsCancel(string text)
  33. {
  34. return cancelTerms.Any(t => string.Equals(t, text, StringComparison.CurrentCultureIgnoreCase));
  35. }
  36. protected override bool TryParse(IMessageActivity message, out T result)
  37. {
  38. if (IsCancel(message.Text))
  39. {
  40. result = default(T);
  41. return true;
  42. }
  43. return base.TryParse(message, out result);
  44. }
  45. protected override IMessageActivity MakePrompt(IDialogContext context, string prompt, IReadOnlyList<T> options = null, IReadOnlyList<string> descriptions = null)
  46. {
  47. prompt += Environment.NewLine + (this.promptOptions.CancelPrompt ?? this.promptOptions.DefaultCancelPrompt);
  48. return base.MakePrompt(context, prompt, options);
  49. }
  50. }
  51. [Serializable]
  52. public class CancelablePromptOptions<T> : PromptOptions<T>
  53. {
  54. public CancelablePromptOptions(string prompt, string cancelPrompt = null, string retry = null, string tooManyAttempts = null, IReadOnlyList<T> options = null, int attempts = 3, PromptStyler promptStyler = null)
  55. : base(prompt, retry, tooManyAttempts, options, attempts, promptStyler)
  56. {
  57. this.DefaultCancelPrompt = Resources.CancelablePromptChoice_CancelText;
  58. this.CancelPrompt = cancelPrompt;
  59. }
  60. public string DefaultCancelPrompt { get; private set; }
  61. public string CancelPrompt { get; private set; }
  62. }
  63. }
Tip!

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

Comments

Loading...