-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathNumberWithTypePrompt.cs
115 lines (99 loc) · 4.14 KB
/
NumberWithTypePrompt.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.Number;
using static Microsoft.Recognizers.Text.Culture;
namespace Bot.Builder.Community.Dialogs.Prompts
{
public enum NumberWithTypePromptType
{
Ordinal,
Percentage,
NumberRange,
Number
}
public class NumberWithTypePrompt : Prompt<NumberWithTypeResult>
{
public NumberWithTypePrompt(string dialogId, NumberWithTypePromptType type,PromptValidator<NumberWithTypeResult> validator=null, string defaultLocale = null)
: base(dialogId, validator)
{
DefaultLocale = defaultLocale;
PromptType = type;
}
public string DefaultLocale { get; set; }
public NumberWithTypePromptType PromptType { get; set; }
protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, bool isRetry,CancellationToken cancellationToken = new CancellationToken())
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (isRetry && options.RetryPrompt != null)
{
await turnContext.SendActivityAsync(options.RetryPrompt, cancellationToken).ConfigureAwait(false);
}
else if (options.Prompt != null)
{
await turnContext.SendActivityAsync(options.Prompt, cancellationToken).ConfigureAwait(false);
}
}
protected override Task<PromptRecognizerResult<NumberWithTypeResult>> OnRecognizeAsync(ITurnContext turnContext,IDictionary<string, object> state, PromptOptions options, CancellationToken cancellationToken = new CancellationToken())
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
var result = new PromptRecognizerResult<NumberWithTypeResult>();
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var message = turnContext.Activity.AsMessageActivity();
var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
List<ModelResult> results = null;
switch (PromptType)
{
case NumberWithTypePromptType.Ordinal:
results = NumberRecognizer.RecognizeOrdinal(message.Text, culture);
break;
case NumberWithTypePromptType.Percentage:
results = NumberRecognizer.RecognizePercentage(message.Text, culture);
break;
case NumberWithTypePromptType.NumberRange:
results = NumberRecognizer.RecognizeNumberRange(message.Text, culture);
break;
case NumberWithTypePromptType.Number:
results = NumberRecognizer.RecognizeNumber(message.Text, culture);
break;
}
if (results?.Count > 0)
{
var resolution = results[0].Resolution;
if (resolution != null)
{
result.Succeeded = true;
var numberWithTypeResult = new NumberWithTypeResult()
{
Text = results[0].Text,
Value = resolution["value"].ToString()
};
result.Value = numberWithTypeResult;
}
}
}
return Task.FromResult(result);
}
}
public class NumberWithTypeResult
{
public string Text { get; set; }
public dynamic Value { get; set; }
}
}