|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using AdaptiveExpressions.Properties; |
| 5 | +using Microsoft.Bot.Builder; |
| 6 | +using Microsoft.Bot.Builder.Dialogs; |
| 7 | +using Microsoft.Bot.Builder.Dialogs.Adaptive.Input; |
| 8 | +using Microsoft.Bot.Builder.Dialogs.Adaptive.Templates; |
| 9 | +using Microsoft.Bot.Schema; |
| 10 | +using Microsoft.Recognizers.Text.Sequence; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using static Microsoft.Recognizers.Text.Culture; |
| 13 | + |
| 14 | +namespace Bot.Builder.Community.Dialogs.CustomInput.Input |
| 15 | +{ |
| 16 | + public class EmailInput : InputDialog |
| 17 | + { |
| 18 | + [JsonProperty("$Kind")] |
| 19 | + public const string Kind = "EmailInput"; |
| 20 | + |
| 21 | + [JsonProperty("defaultLocale")] |
| 22 | + public StringExpression DefaultLocale { get; set; } |
| 23 | + |
| 24 | + [JsonProperty("resultProperty")] |
| 25 | + public StringExpression ResultProperty { get; set; } |
| 26 | + |
| 27 | + [JsonConstructor] |
| 28 | + public EmailInput([CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0) |
| 29 | + { |
| 30 | + this.RegisterSourceLocation(sourceFilePath, sourceLineNumber); |
| 31 | + } |
| 32 | + |
| 33 | + protected override Task<IActivity> OnRenderPromptAsync(DialogContext dc, InputState state, |
| 34 | + CancellationToken cancellationToken = new CancellationToken()) |
| 35 | + { |
| 36 | + if (this.Prompt == null) |
| 37 | + { |
| 38 | + this.Prompt = new StaticActivityTemplate(MessageFactory.Text("Prompt for a email")); |
| 39 | + } |
| 40 | + return base.OnRenderPromptAsync(dc, state, cancellationToken); |
| 41 | + } |
| 42 | + |
| 43 | + protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc, CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + var validateText = dc.State.GetValue<object>(VALUE_PROPERTY); |
| 46 | + |
| 47 | + if (!(validateText is string strEmailText)) |
| 48 | + { |
| 49 | + return Task.FromResult(InputState.Invalid); |
| 50 | + } |
| 51 | + |
| 52 | + var culture = GetCulture(dc); |
| 53 | + |
| 54 | + var recognizeEmail = SequenceRecognizer.RecognizeEmail(strEmailText, culture); |
| 55 | + |
| 56 | + if (recognizeEmail == null || recognizeEmail.Count <= 0) |
| 57 | + { |
| 58 | + return Task.FromResult(InputState.Unrecognized); |
| 59 | + } |
| 60 | + |
| 61 | + var result = recognizeEmail[0].Resolution["value"].ToString(); |
| 62 | + dc.State.SetValue(this.ResultProperty.GetValue(dc.State), result); |
| 63 | + |
| 64 | + return Task.FromResult(InputState.Valid); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private string GetCulture(DialogContext dc) |
| 69 | + { |
| 70 | + if (!string.IsNullOrEmpty(dc.Context.Activity.Locale)) |
| 71 | + { |
| 72 | + return dc.Context.Activity.Locale; |
| 73 | + } |
| 74 | + |
| 75 | + return this.DefaultLocale != null ? this.DefaultLocale.GetValue(dc.State) : English; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments