This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
IFormBuilder.cs
451 lines (400 loc) · 22.6 KB
/
IFormBuilder.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Builder SDK GitHub:
// https://github.com/Microsoft/BotBuilder
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.FormFlow.Advanced;
using Microsoft.Bot.Builder.Resource;
namespace Microsoft.Bot.Builder.FormFlow
{
#region Documentation
/// <summary> Given <paramref name="state"/> return a <see cref="PromptAttribute"/> with a template for the message to display. </summary>
/// <typeparam name="T"> Form state type. </typeparam>
/// <param name="state"> Form state. </param>
/// <returns> A PromptAttribute describing the message to display. </returns>
#endregion
public delegate Task<PromptAttribute> MessageDelegate<T>(T state);
#region Documentation
/// <summary> Interface for building a form. </summary>
/// <remarks>
/// A form consists of a series of steps that can be one of:
/// <list type="list">
/// <item>A message to the user.</item>
/// <item>A prompt sent to the user where the response is to fill in a form state value.</item>
/// <item>A confirmation of the current state with the user.</item>
/// </list>
/// By default the steps are executed in the order of the <see cref="Message"/>, <see cref="Field"/> and <see cref="Confirm"/> calls.
/// If you do not take explicit control, the steps will be executed in the order defined in the
/// form state with a final confirmation.
/// This interface allows you to flently build a form by composing together fields,
/// messages and confirmation. The fluent building blocks provide common patterns
/// like fields being based on your state class, but you can also build up your
/// own definition of a form by using Advanced.IField.
/// If you want to build a form using C# reflection over your state class use FormBuilder.
/// To declaratively build a form through JSON Schema you can use Json.FormBuilderJson.
///
/// Forms are sensitive to the current thread UI culture. The Microsoft.Bot.Builder strings will localize
/// to that culture if available. You can also localize the strings generated for your form by calling IForm.SaveResources
/// or by using the RView tool and adding that resource to your project. For strings in dynamic fields, messages or confirmations you will
/// need to use the normal C# mechanisms to localize them. Look in the overview documentation for more information.
/// </remarks>
/// <typeparam name="T">Form state.</typeparam>
#endregion
public interface IFormBuilder<T>
where T : class
{
/// <summary>
/// Build the form based on the methods called on the builder.
/// </summary>
/// <param name="resourceAssembly">Assembly for localization resources.</param>
/// <param name="resourceName">Name of resources to use for localization.</param>
/// <returns>The constructed form.</returns>
/// <remarks>
/// The default assembly is the one that contains <typeparamref name="T"/>
/// and the default resourceName if the name of that type.
/// </remarks>
IForm<T> Build(Assembly resourceAssembly = null, string resourceName = null);
/// <summary>
/// The form configuration supplies default templates and settings.
/// </summary>
/// <returns>The current form configuration.</returns>
FormConfiguration Configuration { get; }
/// <summary>
/// Show a message that does not require a response.
/// </summary>
/// <param name="message">A \ref patterns string to fill in and send.</param>
/// <param name="condition">Whether or not this step is active.</param>
/// <param name="dependencies">Fields message depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Message(string message, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
/// <summary>
/// Show a message with more format control that does not require a response.
/// </summary>
/// <param name="prompt">Message to fill in and send.</param>
/// <param name="condition">Whether or not this step is active.</param>
/// <param name="dependencies">Fields message depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Message(PromptAttribute prompt, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
#region Documentation
/// <summary> Generate a message using a delegate to dynamically build the message. </summary>
/// <param name="generateMessage"> Delegate for building message. </param>
/// <param name="condition"> Whether or not this step is active. </param>
/// <param name="dependencies">Fields message depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
#endregion
IFormBuilder<T> Message(MessageDelegate<T> generateMessage, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
/// <summary>
/// Derfine a field step by supplying your own field definition.
/// </summary>
/// <param name="field">Field definition to use.</param>
/// <returns>Modified IFormBuilder.</returns>
/// <remarks>
/// You can provide your own implementation of <see cref="IField{T}"/> or you can
/// use the <see cref="Field{T}"/> class to provide fluent values,
/// <see cref="FieldReflector{T}"/> to use reflection or Json.FieldJson to use JSON Schema.
/// </remarks>
IFormBuilder<T> Field(IField<T> field);
/// <summary>
/// Define a step for filling in a particular value in the form state.
/// </summary>
/// <param name="name">Path in the form state to the value being filled in.</param>
/// <param name="active">Delegate to test form state to see if step is active.</param>
/// <param name="validate">Delegate to validate the field value.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Field(string name, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
/// <summary>
/// Define a step for filling in a particular value in the form state.
/// </summary>
/// <param name="name">Path in the form state to the value being filled in.</param>
/// <param name="prompt">Simple \ref patterns to describe prompt for field.</param>
/// <param name="active">Delegate to test form state to see if step is active.n</param>
/// <param name="validate">Delegate to validate the field value.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Field(string name, string prompt, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
/// <summary>
/// Define a step for filling in a particular value in the form state.
/// </summary>
/// <param name="name">Path in the form state to the value being filled in.</param>
/// <param name="prompt">Prompt pattern with more formatting control to describe prompt for field.</param>
/// <param name="active">Delegate to test form state to see if step is active.n</param>
/// <param name="validate">Delegate to validate the field value.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Field(string name, PromptAttribute prompt, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
/// <summary>
/// Add all fields not already added to the form.
/// </summary>
/// <param name="exclude">Fields not to include.</param>
/// <returns>Modified IFormBuilder.</returns>
/// <remarks>
/// This will add all fields defined in your form that have not already been
/// added if the fields are supported.
/// </remarks>
IFormBuilder<T> AddRemainingFields(IEnumerable<string> exclude = null);
/// <summary>
/// Add a confirmation step.
/// </summary>
/// <param name="prompt">Prompt to use for confirmation.</param>
/// <param name="condition">Delegate to test if confirmation applies to the current form state.</param>
/// <param name="dependencies">What fields this confirmation depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
/// <remarks>
/// If prompt is not supplied the \ref patterns element {*} will be used to confirm.
/// Dependencies will by default be all active steps defined before this confirmation.
/// </remarks>
IFormBuilder<T> Confirm(string prompt = null, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
/// <summary>
/// Add a confirmation step.
/// </summary>
/// <param name="prompt">Prompt to use for confirmation.</param>
/// <param name="condition">Delegate to test if confirmation applies to the current form state.</param>
/// <param name="dependencies">What fields this confirmation depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
/// <remarks>
/// Dependencies will by default be all active steps defined before this confirmation.
/// </remarks>
IFormBuilder<T> Confirm(PromptAttribute prompt, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
#region Documentation
/// <summary> Generate a confirmation using a delegate to dynamically build the message. </summary>
/// <param name="generateMessage"> Delegate for building message. </param>
/// <param name="condition"> Whether or not this step is active. </param>
/// <param name="dependencies">What fields this confirmation depends on.</param>
/// <returns>Modified IFormBuilder.</returns>
#endregion
IFormBuilder<T> Confirm(MessageDelegate<T> generateMessage, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
/// <summary>
/// Delegate to send prompt to user.
/// </summary>
/// <param name="prompter">Delegate.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Prompter(PromptAsyncDelegate<T> prompter);
/// <summary>
/// Delegate to call when form is completed.
/// </summary>
/// <param name="callback">Delegate to call on completion.</param>
/// <returns>Modified IFormBuilder.</returns>
/// <remarks>
/// This should only be used for side effects such as calling your service with
/// the form state results. In any case the completed form state will be passed
/// to the parent dialog.
/// </remarks>
IFormBuilder<T> OnCompletion(OnCompletionAsyncDelegate<T> callback);
/// <summary>
/// Test to see if there is already a field with <paramref name="name"/>.
/// </summary>
/// <param name="name"></param>
/// <returns>True if field is already present.</returns>
bool HasField(string name);
}
/// <summary>
/// Default values for the form.
/// </summary>
/// <remarks>
/// These defaults can all be overridden when you create a form and before you add steps.
/// </remarks>
public class FormConfiguration
{
/// <summary>
/// Construct configuration.
/// </summary>
public FormConfiguration()
{
DefaultPrompt.IsLocalizable = false;
foreach (var template in Templates)
{
template.IsLocalizable = false;
}
}
/// <summary>
/// Default prompt and template format settings.
/// </summary>
/// <remarks>
/// When you specify a <see cref="PromptAttribute"/> or <see cref="TemplateAttribute"/>, any format
/// value you do not specify will come from this default.
/// </remarks>
public PromptAttribute DefaultPrompt = new PromptAttribute("")
{
AllowDefault = BoolDefault.True,
ChoiceCase = CaseNormalization.None,
ChoiceFormat = Resources.DefaultChoiceFormat,
ChoiceLastSeparator = Resources.DefaultChoiceLastSeparator,
ChoiceParens = BoolDefault.True,
ChoiceSeparator = Resources.DefaultChoiceSeparator,
ChoiceStyle = ChoiceStyleOptions.Auto,
FieldCase = CaseNormalization.Lower,
Feedback = FeedbackOptions.Auto,
LastSeparator = Resources.DefaultLastSeparator,
Separator = Resources.DefaultSeparator,
ValueCase = CaseNormalization.InitialUpper
};
/// <summary>
/// Enumeration of strings for interpreting a user response as setting an optional field to be unspecified.
/// </summary>
/// <remarks>
/// The first string is also used to describe not having a preference for an optional field.
/// </remarks>
public string[] NoPreference = Resources.MatchNoPreference.SplitList();
/// <summary>
/// Enumeration of strings for interpreting a user response as asking for the current value.
/// </summary>
/// <remarks>
/// The first value is also used to describe the option of keeping the current value.
/// </remarks>
public string[] CurrentChoice = Resources.MatchCurrentChoice.SplitList();
/// <summary>
/// Enumeration of values for a "yes" response for boolean fields or confirmations.
/// </summary>
public string[] Yes = Resources.MatchYes.SplitList();
/// <summary>
/// Enumeration of values for a "no" response for boolean fields or confirmations.
/// </summary>
public string[] No = Resources.MatchNo.SplitList();
/// <summary>
/// String for naming the "navigation" field.
/// </summary>
public string Navigation = Resources.Navigation;
/// <summary>
/// String for naming "Confirmation" fields.
/// </summary>
public string Confirmation = Resources.Confirmation;
/// <summary>
/// Default templates to use if not override on the class or field level.
/// </summary>
public List<TemplateAttribute> Templates = new List<TemplateAttribute>
{
new TemplateAttribute(TemplateUsage.AttachmentCollection, Resources.TemplateAttachmentCollection),
new TemplateAttribute(TemplateUsage.AttachmentCollectionDescription, Resources.TemplateAttachmentCollectionDescription),
new TemplateAttribute(TemplateUsage.AttachmentCollectionHelp, Resources.TemplateAttachmentCollectionHelp),
new TemplateAttribute(TemplateUsage.AttachmentContentTypeValidatorError, Resources.AttachmentContentTypeValidatorError),
new TemplateAttribute(TemplateUsage.AttachmentContentTypeValidatorHelp, Resources.AttachmentContentTypeValidatorHelp),
new TemplateAttribute(TemplateUsage.AttachmentField, Resources.TemplateAttachment),
new TemplateAttribute(TemplateUsage.AttachmentFieldDescription, Resources.TemplateAttachmentDescription),
new TemplateAttribute(TemplateUsage.AttachmentFieldHelp, Resources.TemplateAttachmentHelp),
new TemplateAttribute(TemplateUsage.Bool, Resources.TemplateBool),
// {0} is current choice, {1} is no preference
new TemplateAttribute(TemplateUsage.BoolHelp, Resources.TemplateBoolHelp),
// {0} is term being clarified
new TemplateAttribute(TemplateUsage.Clarify, Resources.TemplateClarify),
new TemplateAttribute(TemplateUsage.Confirmation, Resources.TemplateConfirmation),
new TemplateAttribute(TemplateUsage.CurrentChoice, Resources.TemplateCurrentChoice),
new TemplateAttribute(TemplateUsage.DateTime, Resources.TemplateDateTime),
// {0} is current choice, {1} is no preference
// new TemplateAttribute(TemplateUsage.DateTimeHelp, "Please enter a date or time expression like 'Monday' or 'July 3rd'{?, {0}}{?, {1}}."),
new TemplateAttribute(TemplateUsage.DateTimeHelp, Resources.TemplateDateTimeHelp),
// {0} is min and {1} is max.
new TemplateAttribute(TemplateUsage.Double, Resources.TemplateDouble) { ChoiceFormat = Resources.TemplateDoubleChoiceFormat },
// {0} is current choice, {1} is no preference
// {2} is min and {3} is max
new TemplateAttribute(TemplateUsage.DoubleHelp, Resources.TemplateDoubleHelp),
// {0} is min, {1} is max and {2} are enumerated descriptions
new TemplateAttribute(TemplateUsage.EnumManyNumberHelp, Resources.TemplateEnumManyNumberHelp),
new TemplateAttribute(TemplateUsage.EnumOneNumberHelp, Resources.TemplateEnumOneNumberHelp),
// {2} are the words people can type
new TemplateAttribute(TemplateUsage.EnumManyWordHelp, Resources.TemplateEnumManyWordHelp),
new TemplateAttribute(TemplateUsage.EnumOneWordHelp, Resources.TemplateEnumOneWordHelp),
new TemplateAttribute(TemplateUsage.EnumSelectOne, Resources.TemplateEnumSelectOne),
new TemplateAttribute(TemplateUsage.EnumSelectMany, Resources.TemplateEnumSelectMany),
// {0} is the not understood term
new TemplateAttribute(TemplateUsage.Feedback, Resources.TemplateFeedback),
// For {0} is recognizer help and {1} is command help.
new TemplateAttribute(TemplateUsage.Help, Resources.TemplateHelp),
new TemplateAttribute(TemplateUsage.HelpClarify, Resources.TemplateHelpClarify),
new TemplateAttribute(TemplateUsage.HelpConfirm, Resources.TemplateHelpConfirm),
new TemplateAttribute(TemplateUsage.HelpNavigation, Resources.TemplateHelpNavigation),
// {0} is min and {1} is max if present
new TemplateAttribute(TemplateUsage.Integer, Resources.TemplateInteger) { ChoiceFormat = Resources.TemplateIntegerChoiceFormat },
// {0} is current choice, {1} is no preference
// {2} is min and {3} is max
new TemplateAttribute(TemplateUsage.IntegerHelp, Resources.TemplateIntegerHelp),
new TemplateAttribute(TemplateUsage.Navigation, Resources.TemplateNavigation) { FieldCase = CaseNormalization.None },
// {0} is list of field names.
new TemplateAttribute(TemplateUsage.NavigationCommandHelp, Resources.TemplateNavigationCommandHelp),
new TemplateAttribute(TemplateUsage.NavigationFormat, Resources.TemplateNavigationFormat) {FieldCase = CaseNormalization.None },
// {0} is min, {1} is max
new TemplateAttribute(TemplateUsage.NavigationHelp, Resources.TemplateNavigationHelp),
new TemplateAttribute(TemplateUsage.NoPreference, Resources.TemplateNoPreference),
// {0} is the term that is not understood
new TemplateAttribute(TemplateUsage.NotUnderstood, Resources.TemplateNotUnderstood),
new TemplateAttribute(TemplateUsage.StatusFormat, Resources.TemplateStatusFormat) {FieldCase = CaseNormalization.None },
new TemplateAttribute(TemplateUsage.String, Resources.TemplateString) { ChoiceFormat = Resources.TemplateStringChoiceFormat },
// {0} is current choice, {1} is no preference
new TemplateAttribute(TemplateUsage.StringHelp, Resources.TemplateStringHelp),
new TemplateAttribute(TemplateUsage.Unspecified, Resources.TemplateUnspecified)
};
/// <summary>
/// Definitions of the built-in commands.
/// </summary>
public Dictionary<FormCommand, CommandDescription> Commands = new Dictionary<FormCommand, CommandDescription>()
{
{FormCommand.Backup, new CommandDescription(
Resources.CommandBack,
Resources.CommandBackTerms.SplitList(),
Resources.CommandBackHelp) },
{FormCommand.Help, new CommandDescription(
Resources.CommandHelp,
Resources.CommandHelpTerms.SplitList(),
Resources.CommandHelpHelp) },
{FormCommand.Quit, new CommandDescription(
Resources.CommandQuit,
Resources.CommandQuitTerms.SplitList(),
Resources.CommandQuitHelp) },
{FormCommand.Reset, new CommandDescription(
Resources.CommandReset,
Resources.CommandResetTerms.SplitList(),
Resources.CommandResetHelp)},
{FormCommand.Status, new CommandDescription(
Resources.CommandStatus,
Resources.CommandStatusTerms.SplitList(),
Resources.CommandStatusHelp) }
};
/// <summary>
/// Look up a particular template.
/// </summary>
/// <param name="usage">Desired template.</param>
/// <returns>Matching template.</returns>
public TemplateAttribute Template(TemplateUsage usage)
{
TemplateAttribute result = null;
foreach (var template in Templates)
{
if (template.Usage == usage)
{
result = template;
break;
}
}
Debug.Assert(result != null);
return result;
}
};
}