forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberWithTypeInput.cs
120 lines (103 loc) · 4.11 KB
/
NumberWithTypeInput.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
116
117
118
119
120
using System;
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Input;
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.Number;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Templates;
using Microsoft.Bot.Schema;
using static Microsoft.Recognizers.Text.Culture;
namespace Bot.Builder.Community.Components.Dialogs.Input
{
public class NumberWithTypeInput : InputDialog
{
[JsonProperty("$Kind")]
public const string Kind = "BotBuilderCommunity.NumberWithTypeInput";
[JsonProperty("defaultLocale")]
public StringExpression DefaultLocale { get; set; }
[JsonProperty("NumberType")]
public EnumExpression<NumberWithTypeInputType> NumberType { get; set; } = NumberWithTypeInputType.Number;
[JsonProperty("resultProperty")]
public StringExpression ResultProperty { get; set; }
[JsonConstructor]
public NumberWithTypeInput([CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
RegisterSourceLocation(sourceFilePath, sourceLineNumber);
}
protected override Task<IActivity> OnRenderPromptAsync(DialogContext dc, InputState state,
CancellationToken cancellationToken = new CancellationToken())
{
if (this.Prompt == null)
{
this.Prompt = new StaticActivityTemplate(MessageFactory.Text("Prompt for a NumberWithTypeInput"));
}
return base.OnRenderPromptAsync(dc, state, cancellationToken);
}
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc, CancellationToken cancellationToken)
{
var validateText = dc.State.GetValue<object>(VALUE_PROPERTY);
if (!(validateText is string))
{
return Task.FromResult(InputState.Invalid);
}
var culture = GetCulture(dc);
List<ModelResult> results;
var message = validateText.ToString();
switch (NumberType.GetValue(dc.State))
{
case NumberWithTypeInputType.Percentage:
results = NumberRecognizer.RecognizePercentage(message, culture);
break;
case NumberWithTypeInputType.Number:
results = NumberRecognizer.RecognizeNumber(message, culture);
break;
case NumberWithTypeInputType.NumberRange:
results = NumberRecognizer.RecognizeNumberRange(message, culture);
break;
case NumberWithTypeInputType.Ordinal:
results = NumberRecognizer.RecognizeOrdinal(message, culture);
break;
default:
throw new ArgumentOutOfRangeException();
}
if (results == null || results.Count <= 0 || results[0].Resolution == null)
{
return Task.FromResult(InputState.Unrecognized);
}
var result = new
{
Text = results[0].Text,
Value = results[0].Resolution["value"].ToString(),
};
if (ResultProperty != null)
{
dc.State.SetValue(ResultProperty.GetValue(dc.State), result);
}
return Task.FromResult(InputState.Valid);
}
private string GetCulture(DialogContext dc)
{
if (!string.IsNullOrEmpty(dc.Context.Activity.Locale))
{
return dc.Context.Activity.Locale;
}
return DefaultLocale != null ? DefaultLocale.GetValue(dc.State) : English;
}
}
[JsonConverter(typeof(StringEnumConverter), true)]
public enum NumberWithTypeInputType
{
Ordinal,
Percentage,
NumberRange,
Number
}
}