-
Notifications
You must be signed in to change notification settings - Fork 177
/
SocialMediaInput.cs
106 lines (88 loc) · 3.63 KB
/
SocialMediaInput.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
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveExpressions.Properties;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Input;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Templates;
using Microsoft.Bot.Schema;
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.Sequence;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using static Microsoft.Recognizers.Text.Culture;
namespace Bot.Builder.Community.Components.Dialogs.Input
{
[JsonConverter(typeof(StringEnumConverter), true)]
public enum SocialMediaInputType
{
Mention,
Hashtag
}
public class SocialMediaInput : InputDialog
{
[JsonProperty("$Kind")]
public const string Kind = "BotBuilderCommunity.SocialMediaInput";
[JsonProperty("defaultLocale")]
public StringExpression DefaultLocale { get; set; }
[JsonProperty("resultProperty")]
public StringExpression ResultProperty { get; set; }
[JsonProperty("mediaType")]
public EnumExpression<SocialMediaInputType> MediaType { get; set; } = SocialMediaInputType.Mention;
[JsonConstructor]
public SocialMediaInput([CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
this.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 Social Media Type"));
}
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 strEmailText))
{
return Task.FromResult(InputState.Invalid);
}
var culture = GetCulture(dc);
List<ModelResult> modelResult = null;
switch (this.MediaType.GetValue(dc.State))
{
case SocialMediaInputType.Mention:
modelResult = SequenceRecognizer.RecognizeMention(validateText.ToString(), culture);
break;
case SocialMediaInputType.Hashtag:
modelResult = SequenceRecognizer.RecognizeHashtag(validateText.ToString(), culture);
break;
default:
return Task.FromResult(InputState.Invalid);
}
if (modelResult == null || modelResult.Count <= 0)
{
return Task.FromResult(InputState.Unrecognized);
}
var result = modelResult[0].Resolution["value"].ToString();
if (ResultProperty != null)
{
dc.State.SetValue(this.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 this.DefaultLocale != null ? this.DefaultLocale.GetValue(dc.State) : English;
}
}
}