forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIFormDialog.cs
More file actions
244 lines (215 loc) · 8.99 KB
/
IFormDialog.cs
File metadata and controls
244 lines (215 loc) · 8.99 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
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
// 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;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Bot.Builder.Community.Dialogs.FormFlow.Advanced;
namespace Bot.Builder.Community.Dialogs.FormFlow
{
/// <summary>
/// A delegate for testing a form state to see if a particular step is active.
/// </summary>
/// <typeparam name="T">Form state type.</typeparam>
/// <param name="state">Form state to test.</param>
/// <returns>True if step is active given the current form state.</returns>
public delegate bool ActiveDelegate<T>(T state);
/// <summary>
/// Choice for clarifying an ambiguous value in <see cref="ValidateResult"/>.
/// </summary>
[Serializable]
public class Choice
{
/// <summary>
/// Value to return if choice is selected.
/// </summary>
public object Value;
/// <summary>
/// Description of value.
/// </summary>
public DescribeAttribute Description;
/// <summary>
/// Terms to match value.
/// </summary>
public TermsAttribute Terms;
}
/// <summary> Encapsulates the result of a <see cref="ValidateAsyncDelegate{T}"/> </summary>
/// <remarks>
/// If <see cref="IsValid"/> is true, then the field will be set to <see cref="Value"/>.
/// Otherwise if <see cref="Choices"/> is non-null they will be used to select a clarifying value.
/// if <see cref="FeedbackCard"/> is non-null the resulting card will be displayed.
/// Otherwise the <see cref="Feedback"/> string will be shown to provide feedback on the value.
/// </remarks>
public class ValidateResult
{
/// <summary> Feedback to provide back to the user on the input. </summary>
public string Feedback;
/// <summary>
/// Fully specified feedback card.
/// </summary>
public FormPrompt FeedbackCard;
/// <summary> True if value is a valid response. </summary>
public bool IsValid;
/// <summary>
/// Value to put in the field if result is valid.
/// </summary>
/// <remarks>This provides an opportunity for validation to compute the final value.</remarks>
public object Value;
/// <summary>
/// Choices for clarifying response.
/// </summary>
public IEnumerable<Choice> Choices;
}
/// <summary>
/// A delegate for validating a particular response to a prompt.
/// </summary>
/// <typeparam name="T">Form state type.</typeparam>
/// <param name="state">Form state to test.</param>
/// <param name="value">Response value to validate.</param>
/// <returns><see cref="ValidateResult"/> describing validity, transformed value, feedback or choices for clarification.</returns>
public delegate Task<ValidateResult> ValidateAsyncDelegate<T>(T state, object value);
/// <summary>
/// A delegate called when a form is completed.
/// </summary>
/// <typeparam name="T">Form state type.</typeparam>
/// <param name="context">Session where form dialog is taking place.</param>
/// <param name="state">Completed form state.</param>
/// <remarks>
/// This delegate gives an opportunity to take an action on a completed form
/// such as sending it to your service. It cannot be used to create a new
/// dialog or return a value to the parent dialog.
/// </remarks>
public delegate Task OnCompletionAsyncDelegate<T>(DialogContext context, T state);
/// <summary>
/// Interface for controlling a FormFlow dialog.
/// </summary>
/// <typeparam name="T">Form state type.</typeparam>
/// <remarks>
/// <see cref="FormDialog{T}"/> is an implementation of this interface.
/// </remarks>
/// <exception cref="FormCanceledException{T}">Thrown when the user quits while filling in a form, or there is an underlying exception in the code.</exception>
public interface IFormDialog<T>
where T : class
{
/// <summary>
/// The form specification.
/// </summary>
IForm<T> Form { get; }
}
/// <summary>
/// Commands supported in form dialogs.
/// </summary>
public enum FormCommand
{
/// <summary>
/// Move back to the previous step.
/// </summary>
Backup,
/// <summary>
/// Ask for help on responding to the current field.
/// </summary>
Help,
/// <summary>
/// Quit filling in the current form and return failure to parent dialog.
/// </summary>
Quit,
/// <summary>
/// Reset the status of the form dialog.
/// </summary>
Reset,
/// <summary>
/// Provide feedback to the user on the current form state.
/// </summary>
Status
};
/// <summary>
/// Description of all the information needed for a built-in command.
/// </summary>
public class CommandDescription
{
/// <summary>
/// Description of the command.
/// </summary>
public string Description;
/// <summary>
/// Regexs for matching the command.
/// </summary>
public string[] Terms;
/// <summary>
/// Help string for the command.
/// </summary>
public string Help;
/// <summary>
/// Construct the description of a built-in command.
/// </summary>
/// <param name="description">Description of the command.</param>
/// <param name="terms">Terms that match the command.</param>
/// <param name="help">Help on what the command does.</param>
public CommandDescription(string description, string[] terms, string help)
{
Description = description;
Terms = terms;
Help = help;
}
}
#region Documentation
/// <summary> Exception generated when form filling is canceled by user quit or exception. </summary>
/// <remarks>In the case of user quit or an exception the strongly typed exception <see cref="FormCanceledException{T}"/>
/// is actually thrown, but this provides simple access to the Last step.</remarks>
#endregion
[Serializable]
public class FormCanceledException : OperationCanceledException
{
#region Documentation
/// <summary> Constructor with message and inner exception. </summary>
/// <param name="message">Exception message.</param>
/// <param name="inner">Inner exception.</param>
/// <remarks>In the case of quit by the user, the inner exception will be null.</remarks>
#endregion
public FormCanceledException(string message, Exception inner)
: base(message, inner)
{
}
/// <summary> The names of completed steps. </summary>
public IEnumerable<string> Completed { get; internal set; }
/// <summary> Name of the step that quit or threw an exception. </summary>
public string Last { get; internal set; }
}
#region Documentation
/// <summary> Exception generated when form filling is canceled by user quit or exception. </summary>
/// <typeparam name="T"> Underlying form type. </typeparam>
#endregion
[Serializable]
public class FormCanceledException<T> : FormCanceledException
{
/// <summary> Constructor with message and inner exception. </summary>
/// <param name="message">Exception message.</param>
/// <param name="inner">Inner exception.</param>
/// <remarks>In the case of user quit, the inner exception will be null.</remarks>
public FormCanceledException(string message, Exception inner = null)
: base(message, inner)
{
LastForm = default(T);
}
/// <summary> Gets the partial form when the user quits or there is an exception. </summary>
public T LastForm { get; internal set; }
}
}