IFormBuilder.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
36 using System.Collections.Generic;
37 using System.Diagnostics;
38 using System.Reflection;
39 using System.Threading.Tasks;
40 
41 namespace Microsoft.Bot.Builder.FormFlow
42 {
43  #region Documentation
44  #endregion
49  public delegate Task<PromptAttribute> MessageDelegate<T>(T state);
50 
51  #region Documentation
52  #endregion
77  public interface IFormBuilder<T>
78  where T : class
79  {
90  IForm<T> Build(Assembly resourceAssembly = null, string resourceName = null);
91 
96  FormConfiguration Configuration { get; }
97 
105  IFormBuilder<T> Message(string message, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
106 
114  IFormBuilder<T> Message(PromptAttribute prompt, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
115 
116  #region Documentation
117  #endregion
123  IFormBuilder<T> Message(MessageDelegate<T> generateMessage, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
124 
136 
144  IFormBuilder<T> Field(string name, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
145 
154  IFormBuilder<T> Field(string name, string prompt, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
155 
164  IFormBuilder<T> Field(string name, PromptAttribute prompt, ActiveDelegate<T> active = null, ValidateAsyncDelegate<T> validate = null);
165 
175  IFormBuilder<T> AddRemainingFields(IEnumerable<string> exclude = null);
176 
188  IFormBuilder<T> Confirm(string prompt = null, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
189 
200  IFormBuilder<T> Confirm(PromptAttribute prompt, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
201 
202  #region Documentation
203  #endregion
209  IFormBuilder<T> Confirm(MessageDelegate<T> generateMessage, ActiveDelegate<T> condition = null, IEnumerable<string> dependencies = null);
210 
211 
217  IFormBuilder<T> Prompter(PromptAsyncDelegate<T> prompter);
218 
229  IFormBuilder<T> OnCompletion(OnCompletionAsyncDelegate<T> callback);
230 
236  bool HasField(string name);
237  }
238 
245  public class FormConfiguration
246  {
251  {
252  DefaultPrompt.IsLocalizable = false;
253  foreach (var template in Templates)
254  {
255  template.IsLocalizable = false;
256  }
257  }
258 
266  public PromptAttribute DefaultPrompt = new PromptAttribute("")
267  {
268  AllowDefault = BoolDefault.True,
269  ChoiceCase = CaseNormalization.None,
270  ChoiceFormat = Resources.DefaultChoiceFormat,
271  ChoiceLastSeparator = Resources.DefaultChoiceLastSeparator,
272  ChoiceParens = BoolDefault.True,
273  ChoiceSeparator = Resources.DefaultChoiceSeparator,
274  ChoiceStyle = ChoiceStyleOptions.Auto,
275  FieldCase = CaseNormalization.Lower,
276  Feedback = FeedbackOptions.Auto,
277  LastSeparator = Resources.DefaultLastSeparator,
278  Separator = Resources.DefaultSeparator,
279  ValueCase = CaseNormalization.InitialUpper
280  };
281 
288  public string[] NoPreference = Resources.MatchNoPreference.SplitList();
289 
296  public string[] CurrentChoice = Resources.MatchCurrentChoice.SplitList();
297 
301  public string[] Yes = Resources.MatchYes.SplitList();
302 
306  public string[] No = Resources.MatchNo.SplitList();
307 
311  public string Navigation = Resources.Navigation;
312 
317 
321  public List<TemplateAttribute> Templates = new List<TemplateAttribute>
322  {
324  // {0} is current choice, {1} is no preference
326 
327  // {0} is term being clarified
329 
331 
333 
335  // {0} is current choice, {1} is no preference
336  // new TemplateAttribute(TemplateUsage.DateTimeHelp, "Please enter a date or time expression like 'Monday' or 'July 3rd'{?, {0}}{?, {1}}."),
338 
339  // {0} is min and {1} is max.
341  // {0} is current choice, {1} is no preference
342  // {2} is min and {3} is max
344 
345  // {0} is min, {1} is max and {2} are enumerated descriptions
348 
349  // {2} are the words people can type
352 
355 
356  // {0} is the not understood term
358 
359  // For {0} is recognizer help and {1} is command help.
364 
365  // {0} is min and {1} is max if present
367  // {0} is current choice, {1} is no preference
368  // {2} is min and {3} is max
370 
371  new TemplateAttribute(TemplateUsage.Navigation, Resources.TemplateNavigation) { FieldCase = CaseNormalization.None },
372  // {0} is list of field names.
374  new TemplateAttribute(TemplateUsage.NavigationFormat, Resources.TemplateNavigationFormat) {FieldCase = CaseNormalization.None },
375  // {0} is min, {1} is max
377 
379 
380  // {0} is the term that is not understood
382 
383  new TemplateAttribute(TemplateUsage.StatusFormat, Resources.TemplateStatusFormat) {FieldCase = CaseNormalization.None },
384 
386  // {0} is current choice, {1} is no preference
388 
390  };
391 
395  public Dictionary<FormCommand, CommandDescription> Commands = new Dictionary<FormCommand, CommandDescription>()
396  {
397  {FormCommand.Backup, new CommandDescription(
399  Resources.CommandBackTerms.SplitList(),
403  Resources.CommandHelpTerms.SplitList(),
405  {FormCommand.Quit, new CommandDescription(
407  Resources.CommandQuitTerms.SplitList(),
409  {FormCommand.Reset, new CommandDescription(
411  Resources.CommandResetTerms.SplitList(),
413  {FormCommand.Status, new CommandDescription(
415  Resources.CommandStatusTerms.SplitList(),
417  };
418 
425  {
426  TemplateAttribute result = null;
427  foreach (var template in Templates)
428  {
429  if (template.Usage == usage)
430  {
431  result = template;
432  break;
433  }
434  }
435  Debug.Assert(result != null);
436  return result;
437  }
438  };
439 }
static string TemplateDateTime
Looks up a localized string similar to Please enter a date and time for {&} {||}. ...
static string TemplateIntegerChoiceFormat
Looks up a localized string similar to {1}.
Core namespace for FormFlow and associated infrastructure.
Definition: Attributes.cs:39
static string TemplateStringChoiceFormat
Looks up a localized string similar to {1}.
TemplateAttribute Template(TemplateUsage usage)
Look up a particular template.
A strongly-typed resource class, for looking up localized strings, etc.
How to show no preference in an optional field.
Define a template for generating strings.
Definition: Attributes.cs:571
How to show feedback after user input.
static string TemplateString
Looks up a localized string similar to Please enter {&} {||}.
static string TemplateBool
Looks up a localized string similar to Would you like a {&}? {||}.
A prompt and recognizer packaged together.
Definition: IPrompt.cs:330
static string TemplateFeedback
Looks up a localized string similar to For {&} I understood {}. {?"{0}" is not an option...
static string MatchCurrentChoice
Looks up a localized string similar to Current Choice('c');current;c;current choice;no change...
Define the prompt used when asking about a field.
Definition: Attributes.cs:294
static string TemplateNavigationHelp
Looks up a localized string similar to Choose {?a number from {0}-{1}, or} a field name...
static string TemplateClarify
Looks up a localized string similar to By "{0}" {&} did you mean {||}.
static string CommandQuitTerms
Looks up a localized string similar to quit;stop;finish;goodby?;good bye?;bye;ciao;adios;bye-bye;so l...
static string TemplateHelpConfirm
Looks up a localized string similar to Please answer the question. Possible responses: {0} {1}...
TemplateUsage
All of the built-in templates.
Definition: Attributes.cs:321
static string TemplateDateTimeHelp
Looks up a localized string similar to Please enter a date or time expression {?, {0}}{...
static string DefaultChoiceFormat
Looks up a localized string similar to {0}. {1}.
static string TemplateEnumManyNumberHelp
Looks up a localized string similar to You can enter one or more numbers {0}-{1} or words from the de...
static string TemplateHelp
Looks up a localized string similar to You are filling in the {&} field. Possible responses: {0} {1}...
static string CommandHelpTerms
Looks up a localized string similar to help;choices;\?.
Interface for all the information about a specific field.
Definition: IField.cs:404
FeedbackOptions
Control how the user gets feedback after each entry.
Definition: Attributes.cs:261
ChoiceStyleOptions
Specifies how to show choices generated by {||} in a Pattern Language string.
Definition: Attributes.cs:160
static string CommandResetTerms
Looks up a localized string similar to start over;reset;clear.
static string TemplateUnspecified
Looks up a localized string similar to Unspecified.
static string TemplateEnumManyWordHelp
Looks up a localized string similar to You can enter in one or more selections from the descriptions...
static string TemplateDoubleHelp
Looks up a localized string similar to Please enter a number{? between {2:F1} and {3:F1}}{...
static string TemplateEnumOneWordHelp
Looks up a localized string similar to You can enter in any words from the descriptions. ({2}).
static string TemplateNavigationFormat
Looks up a localized string similar to {&}({}).
BoolDefault
Three state boolean value.
Definition: Attributes.cs:240
static string TemplateNotUnderstood
Looks up a localized string similar to "{0}" is not a {&} option..
static string DefaultSeparator
Looks up a localized string similar to , .
static string CommandReset
Looks up a localized string similar to Start over.
Interface for building a form.
Definition: IFormBuilder.cs:77
static string Navigation
Looks up a localized string similar to Field Name.
Namespace for FormFlow advanced building blocks.
Definition: Attributes.cs:672
static string MatchNoPreference
Looks up a localized string similar to No Preference;no;none;I don'?t care.
Namespace for resources.
static string TemplateIntegerHelp
Looks up a localized string similar to You can enter a number{? between {2} and {3}}{?, {0}}{?, {1}}..
Form definition interface.
Definition: IForm.cs:47
static string CommandStatusHelp
Looks up a localized string similar to Status: Show your progress in filling in the form so far...
static string TemplateEnumOneNumberHelp
Looks up a localized string similar to You can enter a number {0}-{1} or words from the descriptions...
CaseNormalization
How to normalize the case of words.
Definition: Attributes.cs:205
static string CommandHelpHelp
Looks up a localized string similar to Help: Show the kinds of responses you can enter..
static string CommandHelp
Looks up a localized string similar to Help.
static string TemplateStatusFormat
Looks up a localized string similar to {&}: {}.
static string TemplateHelpClarify
Looks up a localized string similar to You are clarifying a {&} value. Possible responses: {0} {1}...
Base class with declarative implementation of IField.
Definition: Field.cs:67
static string MatchYes
Looks up a localized string similar to Yes;y;sure;ok;yep;1.
static string CommandBackTerms
Looks up a localized string similar to backup;go back;back.
static string CommandResetHelp
Looks up a localized string similar to Reset: Start over filling in the form. (With defaults from you...
static string DefaultChoiceSeparator
Looks up a localized string similar to , .
FormCommand
Commands supported in form dialogs.
Definition: IFormDialog.cs:146
static string TemplateDouble
Looks up a localized string similar to Please enter a number {?between {0:F1} and {1:F1}} for {&} {||...
static string TemplateConfirmation
Looks up a localized string similar to Is this your selection? {*}.
static string CommandQuitHelp
Looks up a localized string similar to Quit: Quit the form without completing it..
static string TemplateHelpNavigation
Looks up a localized string similar to Choose what field to change. Possible responses: {0} {1}...
static string TemplateCurrentChoice
Looks up a localized string similar to (current choice: {}).
static string DefaultChoiceLastSeparator
Looks up a localized string similar to , or .
static string TemplateInteger
Looks up a localized string similar to Please enter a number{? between {0} and {1}} for {&} {||}...
static string CommandBackHelp
Looks up a localized string similar to Back: Go back to the previous question..
static string TemplateEnumSelectMany
Looks up a localized string similar to Please select one or more {&} {||}.
string Help
Help string for the command.
Definition: IFormDialog.cs:192
static string CommandStatusTerms
Looks up a localized string similar to status;progress;so far;results.
static string CommandStatus
Looks up a localized string similar to Status.
static string TemplateNavigationCommandHelp
Looks up a localized string similar to You can switch to another field by entering its name...
Description of all the information needed for a built-in command.
Definition: IFormDialog.cs:177
static string TemplateNoPreference
Looks up a localized string similar to No Preference.
static string CommandQuit
Looks up a localized string similar to Quit.
static string TemplateDoubleChoiceFormat
Looks up a localized string similar to {1}.
Field is used to confirm some settings during the dialog.
static string CommandBack
Looks up a localized string similar to Back.
static string TemplateNavigation
Looks up a localized string similar to What do you want to change? {||}.
static string TemplateBoolHelp
Looks up a localized string similar to Please enter 'yes' or 'no'{?, {0}}..
static string TemplateEnumSelectOne
Looks up a localized string similar to Please select a {&} {||}.
static string TemplateStringHelp
Looks up a localized string similar to You can enter anything (use "'s to force string){?, {0}}{?, {1}}..
static string DefaultLastSeparator
Looks up a localized string similar to , and .
static string Confirmation
Looks up a localized string similar to Confirmation.
Root namespace for the Microsoft Bot Builder SDK.
static string MatchNo
Looks up a localized string similar to No;n;nope;2.