forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternetProtocolPrompt.cs
89 lines (76 loc) · 3.24 KB
/
InternetProtocolPrompt.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
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 static Microsoft.Recognizers.Text.Culture;
namespace Bot.Builder.Community.Dialogs.Prompts
{
public enum InternetProtocolPromptType
{
IpAddress,
Url
}
public class InternetProtocolPrompt : Prompt<string>
{
public InternetProtocolPrompt(string dialogId, InternetProtocolPromptType type, PromptValidator<string> validator = null, string defaultLocale = null)
: base(dialogId, validator)
{
DefaultLocale = defaultLocale;
PromptType = type;
}
public string DefaultLocale { get; set; }
public InternetProtocolPromptType 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<string>> 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<string>();
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var message = turnContext.Activity.AsMessageActivity();
var culture = turnContext.Activity.Locale ?? DefaultLocale ?? English;
List<ModelResult> modelResults = null;
switch (PromptType)
{
case InternetProtocolPromptType.IpAddress:
modelResults = Microsoft.Recognizers.Text.Sequence.SequenceRecognizer.RecognizeIpAddress(message.Text, culture);
break;
case InternetProtocolPromptType.Url:
modelResults = Microsoft.Recognizers.Text.Sequence.SequenceRecognizer.RecognizeURL(message.Text, culture);
break;
}
if (modelResults?.Count > 0)
{
result.Succeeded = true;
result.Value = modelResults[0].Resolution["value"].ToString();
}
}
return Task.FromResult(result);
}
}
}