-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathMultiSelectChoiceInput.cs
More file actions
129 lines (105 loc) · 4.7 KB
/
MultiSelectChoiceInput.cs
File metadata and controls
129 lines (105 loc) · 4.7 KB
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
121
122
123
124
125
126
127
128
129
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using AdaptiveExpressions.Properties;
using Bot.Builder.Community.Components.Dialogs.Input.MultiSelectChoice;
using Bot.Builder.Community.Components.Dialogs.Input.MultiSelectChoice.Setting;
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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Bot.Builder.Community.Components.Dialogs.Input
{
public class MultiSelectChoiceInput : InputDialog
{
[JsonProperty("$Kind")]
public const string Kind = "BotBuilderCommunity.MultiSelectChoiceInput";
[JsonProperty("choices")]
public ObjectExpression<ChoiceSet> Choices { get; set; }
[JsonProperty("Orientation")]
public EnumExpression<Orientation> OrientationType { get; set; }
[JsonProperty("ActionName")]
public StringExpression ActionName { get; set; }
[JsonProperty("Result")]
public StringExpression Result { get; set; }
private readonly IMultiSelectAdaptiveChoice _multiSelectAdaptiveHandler;
[JsonConstructor]
public MultiSelectChoiceInput([CallerFilePath] string callerPath = "",
[CallerLineNumber] int callerLine = 0)
{
_multiSelectAdaptiveHandler = new MultiSelectAdaptiveChoice();
RegisterSourceLocation(callerPath, callerLine);
}
protected override async Task<IActivity> OnRenderPromptAsync(DialogContext dc, InputState state,
CancellationToken cancellationToken = new CancellationToken())
{
var cardSettings = await PrepareInputSetting(dc, state, cancellationToken);
if (cardSettings.ChoiceList == null || cardSettings.ChoiceList.Count <= 0)
{
throw new ArgumentException("List of Choices should not be empty");
}
var attachment = _multiSelectAdaptiveHandler.CreateAttachment(cardSettings);
if (attachment == null)
throw new ArgumentException("Render card has failed");
var activity = new StaticActivityTemplate((Activity)MessageFactory.Attachment(attachment));
switch (state)
{
case InputState.Invalid:
InvalidPrompt = activity;
break;
case InputState.Unrecognized:
UnrecognizedPrompt = activity;
break;
default:
Prompt = activity;
break;
}
return await base.OnRenderPromptAsync(dc, state, cancellationToken);
}
private async Task<CardSetting> PrepareInputSetting(DialogContext dc, InputState state,
CancellationToken cancellationToken)
{
var cardSetting = new CardSetting
{
ChoiceList = Choices?.TryGetValue(dc.State).Value
};
var actionName = ActionName?.TryGetValue(dc.State).Value;
if (string.IsNullOrEmpty(actionName))
actionName = "Submit";
cardSetting.ActionName = actionName;
cardSetting.OrientationType = OrientationType.GetValue(dc.State);
cardSetting.Title = await GetPromptText(dc, state, cancellationToken);
return cardSetting;
}
private async Task<string> GetPromptText(DialogContext dc, InputState state, CancellationToken cancellationToken)
{
var promptActivity = (Activity)await base.OnRenderPromptAsync(dc, state, cancellationToken);
var promptText = promptActivity != null && !string.IsNullOrEmpty(promptActivity.Text)
? promptActivity.Text
: "Prompt Text is missing";
return promptText;
}
protected override Task<InputState> OnRecognizeInputAsync(DialogContext dc, CancellationToken cancellationToken)
{
var validateText = dc.State.GetValue<object>(VALUE_PROPERTY);
if (validateText is JObject)
{
var result = _multiSelectAdaptiveHandler.ResultSet(validateText);
if (result != null && result.Count > 0)
{
dc.State.SetValue(Result.GetValue(dc.State), result);
return Task.FromResult(InputState.Valid);
}
}
else if (validateText != null)
{
return Task.FromResult(InputState.Unrecognized);
}
return Task.FromResult(InputState.Invalid);
}
}
}