forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCancelablePromptChoice.cs
60 lines (50 loc) · 2.53 KB
/
CancelablePromptChoice.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
51
52
53
54
55
56
57
58
59
60
namespace Search.Dialogs
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
[Serializable]
public class CancelablePromptChoice<T> : PromptDialog.PromptChoice<T>
{
protected readonly CancelablePromptOptions<T> PromptOptions;
private static IEnumerable<string> cancelTerms = new[] { "Cancel", "Back", "B", "Abort" };
public CancelablePromptChoice(CancelablePromptOptions<T> promptOptions)
: base(promptOptions)
{
this.PromptOptions = promptOptions;
}
public CancelablePromptChoice(IEnumerable<T> options, string prompt, string cancelPrompt, string retry, int attempts, PromptStyle promptStyle = PromptStyle.Auto)
: this(new CancelablePromptOptions<T>(prompt, cancelPrompt, retry, options: options.ToList(), attempts: attempts, promptStyler: new PromptStyler(promptStyle)))
{
}
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)
{
Choice(context, resume, new CancelablePromptOptions<T>(prompt, cancelPrompt, retry, attempts: attempts, options: options.ToList(), promptStyler: new PromptStyler(promptStyle)));
}
public static void Choice(IDialogContext context, ResumeAfter<T> resume, CancelablePromptOptions<T> promptOptions)
{
var child = new CancelablePromptChoice<T>(promptOptions);
context.Call(child, resume);
}
public static bool IsCancel(string text)
{
return cancelTerms.Any(t => string.Equals(t, text, StringComparison.CurrentCultureIgnoreCase));
}
protected override bool TryParse(IMessageActivity message, out T result)
{
if (IsCancel(message.Text))
{
result = default(T);
return true;
}
return base.TryParse(message, out result);
}
protected override IMessageActivity MakePrompt(IDialogContext context, string prompt, IReadOnlyList<T> options = null, IReadOnlyList<string> descriptions = null, string speak = null)
{
prompt += Environment.NewLine + (this.PromptOptions.CancelPrompt ?? this.PromptOptions.DefaultCancelPrompt);
return base.MakePrompt(context, prompt, options, descriptions, speak);
}
}
}