-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathPromptDialog.cs
784 lines (710 loc) · 34.5 KB
/
PromptDialog.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//
// 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 Microsoft.Bot.Builder.ConnectorEx;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Resource;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.Bot.Builder.Dialogs
{
/// <summary>
/// The style of generated prompt
/// </summary>
public enum PromptStyle
{
/// <summary>
/// Generate buttons for choices and let connector generate the right style based on channel capabilities
/// </summary>
Auto,
/// <summary>
/// Generate keyboard card for choices that will be mapped to a
/// <see cref="HeroCard"/> or a keyboard, e.g. Facebook quick replies
/// </summary>
/// <remarks>
/// Make sure to use <see cref="MapToChannelData_BotToUser"/> with <see cref="KeyboardCardMapper"/>
/// when you use this option
/// </remarks>
Keyboard,
/// <summary>
/// Show choices as Text.
/// </summary>
/// <remarks> The prompt decides if it should generate the text inline or perline based on number of choices.</remarks>
AutoText,
/// <summary>
/// Show choices on the same line.
/// </summary>
Inline,
/// <summary>
/// Show choices with one per line.
/// </summary>
PerLine,
/// <summary>
/// Do not show possible choices in the prompt
/// </summary>
None
}
/// <summary>
/// Options for <see cref="PromptDialog"/>.
/// </summary>
/// <typeparam name="T"> The type of the options.</typeparam>
[Serializable]
public class PromptOptions<T>
{
/// <summary>
/// The prompt.
/// </summary>
public readonly string Prompt;
/// <summary>
/// What to display on retry.
/// </summary>
public readonly string Retry;
/// <summary>
/// The choices to be returned when selected.
/// </summary>
public readonly IReadOnlyList<T> Options;
/// <summary>
/// The description of each possible option.
/// </summary>
/// <remarks>
/// If this is null, then the descriptions will be the options converted to strings.
/// Otherwise this should have the same number of values as Options and it contains the string to describe the value being selected.
/// </remarks>
public readonly IReadOnlyList<string> Descriptions;
/// <summary>
/// What to display when user didn't say a valid response after <see cref="Attempts"/>.
/// </summary>
public readonly string TooManyAttempts;
/// <summary>
/// Maximum number of attempts.
/// </summary>
public int Attempts { set; get; }
/// <summary>
/// Styler of the prompt <see cref="Dialogs.PromptStyler"/>.
/// </summary>
public readonly PromptStyler PromptStyler;
/// <summary>
/// Default retry prompt that is used if <see cref="Retry"/> is null.
/// </summary>
public string DefaultRetry { get; set; }
/// <summary>
/// Default <see cref="TooManyAttempts"/> string that is used if <see cref="TooManyAttempts"/> is null.
/// </summary>
protected string DefaultTooManyAttempts
{
get { return Resources.TooManyAttempts; }
}
/// <summary>
/// Constructs the prompt options.
/// </summary>
/// <param name="prompt"> The prompt.</param>
/// <param name="retry"> What to display on retry.</param>
/// <param name="tooManyAttempts"> What to display when user didn't say a valid response after <see cref="Attempts"/>.</param>
/// <param name="options"> The prompt choice values.</param>
/// <param name="attempts"> Maximum number of attempts.</param>
/// <param name="promptStyler"> The prompt styler.</param>
/// <param name="descriptions">Descriptions for each prompt.</param>
public PromptOptions(string prompt, string retry = null, string tooManyAttempts = null, IReadOnlyList<T> options = null, int attempts = 3, PromptStyler promptStyler = null, IReadOnlyList<string> descriptions = null)
{
SetField.NotNull(out this.Prompt, nameof(this.Prompt), prompt);
this.Retry = retry;
this.TooManyAttempts = tooManyAttempts ?? this.DefaultTooManyAttempts;
this.Attempts = attempts;
this.Options = options;
this.Descriptions = descriptions;
this.DefaultRetry = prompt;
if (promptStyler == null)
{
promptStyler = new PromptStyler();
}
this.PromptStyler = promptStyler;
}
}
/// <summary>
/// Styles a prompt
/// </summary>
[Serializable]
public class PromptStyler
{
/// <summary>
/// Style of the prompt <see cref="Dialogs.PromptStyle"/>.
/// </summary>
public readonly PromptStyle PromptStyle;
public PromptStyler(PromptStyle promptStyle = PromptStyle.Auto)
{
this.PromptStyle = promptStyle;
}
/// <summary>
/// <see cref="PromptStyler.Apply(ref IMessageActivity, string)"/>.
/// </summary>
/// <typeparam name="T"> The type of the options.</typeparam>
/// <param name="message"> The message.</param>
/// <param name="prompt"> The prompt.</param>
/// <param name="options"> The options.</param>
/// <param name="promptStyle"> The prompt style.</param>
/// <param name="descriptions">Descriptions for each option.</param>
public static void Apply<T>(ref IMessageActivity message, string prompt, IReadOnlyList<T> options, PromptStyle promptStyle, IReadOnlyList<string> descriptions = null)
{
var styler = new PromptStyler(promptStyle);
styler.Apply(ref message, prompt, options, descriptions);
}
/// <summary>
/// Style a prompt and populate the <see cref="IMessageActivity.Text"/>.
/// </summary>
/// <param name="message"> The message that will contain the prompt.</param>
/// <param name="prompt"> The prompt.</param>
public virtual void Apply(ref IMessageActivity message, string prompt)
{
SetField.CheckNull(nameof(prompt), prompt);
message.Text = prompt;
}
/// <summary>
/// Style a prompt and populate the message based on <see cref="PromptStyler.PromptStyle"/>.
/// </summary>
/// <typeparam name="T"> The type of the options.</typeparam>
/// <param name="message"> The message that will contain the prompt.</param>
/// <param name="prompt"> The prompt.</param>
/// <param name="options"> The options.</param>
/// <param name="descriptions">Descriptions to display for each option.</param>
/// <remarks>
/// <typeparamref name="T"/> should implement <see cref="object.ToString"/> unless descriptions are supplied.
/// </remarks>
public virtual void Apply<T>(ref IMessageActivity message, string prompt, IReadOnlyList<T> options, IReadOnlyList<string> descriptions = null)
{
SetField.CheckNull(nameof(prompt), prompt);
SetField.CheckNull(nameof(options), options);
if (descriptions == null)
{
descriptions = (from option in options select option.ToString()).ToList();
}
switch (PromptStyle)
{
case PromptStyle.Auto:
case PromptStyle.Keyboard:
if (options != null && options.Any())
{
if (PromptStyle == PromptStyle.Keyboard)
{
message.AddKeyboardCard(prompt, options, descriptions);
}
else
{
message.AddHeroCard(prompt, options, descriptions);
}
}
else
{
message.Text = prompt;
}
break;
case PromptStyle.AutoText:
Apply(ref message, prompt, options, options?.Count() > 4 ? PromptStyle.PerLine : PromptStyle.Inline, descriptions);
break;
case PromptStyle.Inline:
//TODO: Refactor buildlist function to a more generic namespace when changing prompt to use recognizers.
message.Text = $"{prompt} {FormFlow.Advanced.Language.BuildList(descriptions, Resources.DefaultChoiceSeparator, Resources.DefaultChoiceLastSeparator)}";
break;
case PromptStyle.PerLine:
message.Text = $"{prompt}\n{FormFlow.Advanced.Language.BuildList(descriptions.Select(description => $"* {description}"), "\n", "\n")}";
break;
case PromptStyle.None:
default:
message.Text = prompt;
break;
}
}
}
/// <summary> Dialog factory for simple prompts. </summary>
/// <remarks>The exception <see cref="TooManyAttemptsException"/> will be thrown if the number of allowed attempts is exceeded.</remarks>
public class PromptDialog
{
/// <summary> Prompt for a string. </summary>
/// <param name="context"> The context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="retry"> What to show on retry. </param>
/// <param name="attempts"> The number of times to retry. </param>
public static void Text(IDialogContext context, ResumeAfter<string> resume, string prompt, string retry = null, int attempts = 3)
{
var child = new PromptString(prompt, retry, attempts);
context.Call<string>(child, resume);
}
/// <summary> Ask a yes/no question. </summary>
/// <param name="context"> The context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="retry"> What to show on retry. </param>
/// <param name="attempts"> The number of times to retry. </param>
/// <param name="promptStyle"> Style of the prompt <see cref="PromptStyle" /> </param>
public static void Confirm(IDialogContext context, ResumeAfter<bool> resume, string prompt, string retry = null, int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto)
{
Confirm(context, resume, new PromptOptions<string>(prompt, retry, attempts: attempts, options: PromptConfirm.Options.ToList(), promptStyler: new PromptStyler(promptStyle: promptStyle)));
}
/// <summary>
/// Ask a yes/no questions.
/// </summary>
/// <param name="context"> The dialog context.</param>
/// <param name="resume"> Resume handler.</param>
/// <param name="promptOptions"> The options for the prompt, <see cref="PromptOptions{T}"/>.</param>
public static void Confirm(IDialogContext context, ResumeAfter<bool> resume, PromptOptions<string> promptOptions)
{
var child = new PromptConfirm(promptOptions);
context.Call<bool>(child, resume);
}
/// <summary> Prompt for a long. </summary>
/// <param name="context"> The context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="retry"> What to show on retry. </param>
/// <param name="attempts"> The number of times to retry. </param>
public static void Number(IDialogContext context, ResumeAfter<long> resume, string prompt, string retry = null, int attempts = 3)
{
var child = new PromptInt64(prompt, retry, attempts);
context.Call<long>(child, resume);
}
/// <summary> Prompt for a double. </summary>
/// <param name="context"> The context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="retry"> What to show on retry. </param>
/// <param name="attempts"> The number of times to retry. </param>
public static void Number(IDialogContext context, ResumeAfter<double> resume, string prompt, string retry = null, int attempts = 3)
{
var child = new PromptDouble(prompt, retry, attempts);
context.Call<double>(child, resume);
}
/// <summary> Prompt for one of a set of choices. </summary>
/// <param name="context"> The context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="options"> The possible options all of which must be convertible to a string.</param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="retry"> What to show on retry. </param>
/// <param name="attempts"> The number of times to retry. </param>
/// <param name="promptStyle"> Style of the prompt <see cref="PromptStyle" /> </param>
/// <param name="descriptions">Descriptions to display for choices.</param>
public static void Choice<T>(IDialogContext context, ResumeAfter<T> resume, IEnumerable<T> options, string prompt, string retry = null, int attempts = 3, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable<string> descriptions = null)
{
Choice(context, resume, new PromptOptions<T>(prompt, retry, attempts: attempts, options: options.ToList(), promptStyler: new PromptStyler(promptStyle), descriptions: descriptions?.ToList()));
}
/// <summary>
/// Prompt for one of a set of choices.
/// </summary>
/// <remarks><typeparamref name="T"/> should implement <see cref="object.ToString"/></remarks>
/// <typeparam name="T"> The type of the options.</typeparam>
/// <param name="context"> The dialog context.</param>
/// <param name="resume"> Resume handler.</param>
/// <param name="promptOptions"> The prompt options.</param>
public static void Choice<T>(IDialogContext context, ResumeAfter<T> resume, PromptOptions<T> promptOptions)
{
var child = new PromptChoice<T>(promptOptions);
context.Call<T>(child, resume);
}
/// <summary>
/// Prompt for an attachment
/// </summary>
/// <param name="context"> The dialog context. </param>
/// <param name="resume"> Resume handler. </param>
/// <param name="prompt"> The prompt to show to the user. </param>
/// <param name="contentTypes">The optional content types the attachment type should be part of</param>
/// <param name="retry"> What to show on retry</param>
/// <param name="attempts"> The number of times to retry</param>
public static void Attachment(IDialogContext context, ResumeAfter<IEnumerable<Attachment>> resume, string prompt, IEnumerable<string> contentTypes = null, string retry = null, int attempts = 3)
{
var child = new PromptAttachment(prompt, retry, attempts, contentTypes);
context.Call<IEnumerable<Attachment>>(child, resume);
}
/// <summary> Prompt for a text string. </summary>
/// <remarks> Normally used through <see cref="PromptDialog.Text(IDialogContext, ResumeAfter{string}, string, string, int)"/>.</remarks>
[Serializable]
public sealed class PromptString : Prompt<string, string>
{
/// <summary> Constructor for a prompt string dialog. </summary>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> Maximum number of attempts. </param>
public PromptString(string prompt, string retry, int attempts)
: this(new PromptOptions<string>(prompt, retry, attempts: attempts)) { }
/// <summary> Constructor for a prompt string dialog. </summary>
/// <param name="promptOptions"> THe prompt options.</param>
public PromptString(PromptOptions<string> promptOptions)
: base(promptOptions)
{
this.promptOptions.DefaultRetry = this.DefaultRetry;
}
protected override bool TryParse(IMessageActivity message, out string result)
{
if (!string.IsNullOrWhiteSpace(message.Text))
{
result = message.Text;
return true;
}
else
{
result = null;
return false;
}
}
public string DefaultRetry
{
get
{
return Resources.PromptRetry + "\n" + this.promptOptions.Prompt;
}
}
}
/// <summary> Prompt for a confirmation. </summary>
/// <remarks> Normally used through <see cref="PromptDialog.Confirm(IDialogContext, ResumeAfter{bool}, string, string, int, PromptStyle)"/>.</remarks>
[Serializable]
public sealed class PromptConfirm : Prompt<bool, string>
{
/// <summary>
/// Index of yes descriptions.
/// </summary>
public const int Yes = 0;
/// <summary>
/// Index of no descriptions.
/// </summary>
public const int No = 1;
/// <summary>
/// The yes, no options for confirmation prompt
/// </summary>
public static string[] Options { set; get; } = { Resources.MatchYes.SplitList().First(), Resources.MatchNo.SplitList().First() };
/// <summary>
/// The patterns for matching yes/no responses in the confirmation prompt.
/// </summary>
public static string[][] Patterns { get; set; } = { Resources.MatchYes.SplitList(), Resources.MatchNo.SplitList() };
/// <summary> Constructor for a prompt confirmation dialog. </summary>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> Maximum number of attempts. </param>
/// <param name="promptStyle"> Style of the prompt <see cref="PromptStyle" /> </param>
public PromptConfirm(string prompt, string retry, int attempts, PromptStyle promptStyle = PromptStyle.Auto)
: this(new PromptOptions<string>(prompt, retry, attempts: attempts, options: Options.ToList(), promptStyler: new PromptStyler(promptStyle)))
{
}
/// <summary>
/// Constructor for a prompt confirmation dialog.
/// </summary>
/// <param name="promptOptions"> THe prompt options.</param>
public PromptConfirm(PromptOptions<string> promptOptions)
: base(promptOptions)
{
this.promptOptions.DefaultRetry = this.DefaultRetry;
}
protected override bool TryParse(IMessageActivity message, out bool result)
{
var found = false;
result = false;
if (message.Text != null)
{
var term = message.Text.Trim().ToLower();
if ((from r in Patterns[Yes] select r.ToLower()).Contains(term))
{
result = true;
found = true;
}
else if ((from r in Patterns[No] select r.ToLower()).Contains(term))
{
result = false;
found = true;
}
}
return found;
}
public string DefaultRetry
{
get
{
return Resources.PromptRetry + "\n" + this.promptOptions.Prompt;
}
}
}
/// <summary> Prompt for a confirmation. </summary>
/// <remarks> Normally used through <see cref="PromptDialog.Number(IDialogContext, ResumeAfter{long}, string, string, int)"/>.</remarks>
[Serializable]
public sealed class PromptInt64 : Prompt<long, long>
{
/// <summary> Constructor for a prompt int64 dialog. </summary>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> Maximum number of attempts. </param>
public PromptInt64(string prompt, string retry, int attempts)
: this(new PromptOptions<long>(prompt, retry, attempts: attempts)) { }
/// <summary> Constructor for a prompt int64 dialog. </summary>
/// <param name="promptOptions"> THe prompt options.</param>
public PromptInt64(PromptOptions<long> promptOptions)
: base(promptOptions) { }
protected override bool TryParse(IMessageActivity message, out Int64 result)
{
return Int64.TryParse(message.Text, out result);
}
}
/// <summary> Prompt for a double. </summary>
/// <remarks> Normally used through <see cref="PromptDialog.Number(IDialogContext, ResumeAfter{double}, string, string, int)"/>.</remarks>
[Serializable]
public sealed class PromptDouble : Prompt<double, double>
{
/// <summary> Constructor for a prompt double dialog. </summary>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> Maximum number of attempts. </param>
public PromptDouble(string prompt, string retry, int attempts)
: this(new PromptOptions<double>(prompt, retry, attempts: attempts)) { }
/// <summary> Constructor for a prompt double dialog. </summary>
/// <param name="promptOptions"> THe prompt options.</param>
public PromptDouble(PromptOptions<double> promptOptions)
: base(promptOptions) { }
protected override bool TryParse(IMessageActivity message, out double result)
{
return double.TryParse(message.Text, out result);
}
}
/// <summary> Prompt for a choice from a set of choices. </summary>
/// <remarks> Normally used through <see cref="PromptDialog.Choice{T}(IDialogContext, ResumeAfter{T}, IEnumerable{T}, string, string, int, PromptStyle, IEnumerable{string})"/>.</remarks>
[Serializable]
public class PromptChoice<T> : Prompt<T, T>
{
/// <summary> Constructor for a prompt choice dialog. </summary>
/// <param name="options">Enumerable of the options to choose from.</param>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> Maximum number of attempts. </param>
/// <param name="promptStyle"> Style of the prompt <see cref="PromptStyle" /> </param>
/// <param name="descriptions">Descriptions to show for each option.</param>
public PromptChoice(IEnumerable<T> options, string prompt, string retry, int attempts, PromptStyle promptStyle = PromptStyle.Auto, IEnumerable<string> descriptions = null)
: this(new PromptOptions<T>(prompt, retry, options: options.ToList(), attempts: attempts, promptStyler: new PromptStyler(promptStyle), descriptions: descriptions?.ToList()))
{
}
/// <summary>
/// Constructs a choice dialog.
/// </summary>
/// <param name="promptOptions"> The prompt options</param>
public PromptChoice(PromptOptions<T> promptOptions)
: base(promptOptions)
{
SetField.CheckNull(nameof(promptOptions.Options), promptOptions.Options);
}
public virtual Tuple<bool, int> ScoreMatch(T option, string input)
{
var trimmed = input.Trim();
var text = option.ToString();
bool occurs = text.IndexOf(trimmed, StringComparison.CurrentCultureIgnoreCase) >= 0;
bool equals = text == trimmed;
return occurs
? Tuple.Create(equals, trimmed.Length)
: null;
}
protected override bool TryParse(IMessageActivity message, out T result)
{
if (!string.IsNullOrWhiteSpace(message.Text))
{
var scores = from option in this.promptOptions.Options
let score = ScoreMatch(option, message.Text)
select new { score, option };
var winner = scores.MaxBy(s => s.score);
if (winner.score != null)
{
result = winner.option;
return true;
}
}
result = default(T);
return false;
}
}
/// <summary> Prompt for an attachment</summary>
/// <remarks> Normally used through <see cref="PromptDialog.Attachment(IDialogContext, ResumeAfter{IEnumerable{Connector.Attachment}}, string, IEnumerable{string}, string, int)"/>.</remarks>
[Serializable]
public sealed class PromptAttachment : Prompt<IEnumerable<Attachment>, Attachment>
{
public IEnumerable<string> ContentTypes
{
get;
private set;
}
/// <summary> Constructor for a prompt attachment dialog. </summary>
/// <param name="prompt"> The prompt. </param>
/// <param name="retry"> What to display on retry. </param>
/// <param name="attempts"> The optional content types the attachment type should be part of.</param>
/// <param name="contentTypes"> The content types that is used to filter the attachments. Null implies any content type.</param>
public PromptAttachment(string prompt, string retry, int attempts, IEnumerable<string> contentTypes = null)
: base(new PromptOptions<Attachment>(prompt, retry, attempts: attempts))
{
this.ContentTypes = contentTypes ?? new List<string>();
}
protected override bool TryParse(IMessageActivity message, out IEnumerable<Attachment> result)
{
if (message.Attachments != null && message.Attachments.Any())
{
// Retrieve attachments corresponding to content types if any
result = ContentTypes.Any() ? message.Attachments.Join(ContentTypes, a => a.ContentType, c => c, (a, c) => a)
: message.Attachments;
return result != null && result.Any();
}
else
{
result = null;
return false;
}
}
}
}
public static partial class Extensions
{
/// <summary>
/// Generates buttons from options and add them to the message.
/// </summary>
/// <remarks>
/// <typeparamref name="T"/> should implement ToString().
/// </remarks>
/// <typeparam name="T"> Type of the options.</typeparam>
/// <param name="message"> The message that the buttons will be added to.</param>
/// <param name="text"> The text in the <see cref="HeroCard"/>.</param>
/// <param name="options"> The options that cause generation of buttons.</param>
/// <param name="descriptions">Descriptions for each option.</param>
public static void AddHeroCard<T>(this IMessageActivity message, string text, IEnumerable<T> options, IEnumerable<string> descriptions = null)
{
message.AttachmentLayout = AttachmentLayoutTypes.List;
message.Attachments = options.GenerateHeroCard(text, descriptions);
}
/// <summary>
/// Generates buttons from options and add them to the message.
/// </summary>
/// <remarks>
/// <typeparamref name="T"/> should implement ToString().
/// </remarks>
/// <typeparam name="T"> Type of the options.</typeparam>
/// <param name="message"> The message that the buttons will be added to.</param>
/// <param name="text"> The text in the <see cref="HeroCard"/>.</param>
/// <param name="options"> The options that cause generation of buttons.</param>
/// <param name="descriptions">Descriptions for each option.</param>
public static void AddKeyboardCard<T>(this IMessageActivity message, string text, IEnumerable<T> options,
IEnumerable<string> descriptions = null)
{
message.AttachmentLayout = AttachmentLayoutTypes.List;
message.Attachments = options.GenerateKeyboardCard(text, descriptions);
}
internal static IList<Attachment> GenerateHeroCard<T>(this IEnumerable<T> options, string text, IEnumerable<string> descriptions = null)
{
var attachments = new List<Attachment>
{
new HeroCard(text: text, buttons: GenerateButtons(options, descriptions)).ToAttachment()
};
return attachments;
}
internal static IList<Attachment> GenerateKeyboardCard<T>(this IEnumerable<T> options, string text, IEnumerable<string> descriptions = null)
{
var attachments = new List<Attachment>
{
new KeyboardCard(text: text, buttons: GenerateButtons(options, descriptions)).ToAttachment()
};
return attachments;
}
internal static IList<CardAction> GenerateButtons<T>(IEnumerable<T> options,
IEnumerable<string> descriptions = null)
{
var actions = new List<CardAction>();
int i = 0;
var adescriptions = descriptions?.ToArray();
foreach (var option in options)
{
var title = (adescriptions == null ? option.ToString() : adescriptions[i]);
actions.Add(new CardAction
{
Title = title,
Type = ActionTypes.ImBack,
Value = option.ToString()
});
++i;
}
return actions;
}
}
}
namespace Microsoft.Bot.Builder.Dialogs.Internals
{
[Serializable]
public abstract class Prompt<T, U> : IDialog<T>
{
protected readonly PromptOptions<U> promptOptions;
public Prompt(PromptOptions<U> promptOptions)
{
SetField.NotNull(out this.promptOptions, nameof(promptOptions), promptOptions);
}
async Task IDialog<T>.StartAsync(IDialogContext context)
{
await context.PostAsync(this.MakePrompt(context, promptOptions.Prompt, promptOptions.Options, promptOptions.Descriptions));
context.Wait(MessageReceivedAsync);
}
protected virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> message)
{
T result;
if (this.TryParse(await message, out result))
{
context.Done(result);
}
else
{
--promptOptions.Attempts;
if (promptOptions.Attempts >= 0)
{
await context.PostAsync(this.MakePrompt(context, promptOptions.Retry ?? promptOptions.DefaultRetry, promptOptions.Options, promptOptions.Descriptions));
context.Wait(MessageReceivedAsync);
}
else
{
//too many attempts, throw.
await context.PostAsync(this.MakePrompt(context, promptOptions.TooManyAttempts));
throw new TooManyAttemptsException(promptOptions.TooManyAttempts);
}
}
}
protected abstract bool TryParse(IMessageActivity message, out T result);
protected virtual IMessageActivity MakePrompt(IDialogContext context, string prompt, IReadOnlyList<U> options = null, IReadOnlyList<string> descriptions = null)
{
var msg = context.MakeMessage();
if (options != null && options.Count > 0)
{
promptOptions.PromptStyler.Apply(ref msg, prompt, options, descriptions);
}
else
{
promptOptions.PromptStyler.Apply(ref msg, prompt);
}
return msg;
}
}
}