@@ -6,6 +6,8 @@ var __extends = (this && this.__extends) || function (d, b) {
6
6
var dialog = require ( './Dialog' ) ;
7
7
var consts = require ( '../consts' ) ;
8
8
var entities = require ( './EntityRecognizer' ) ;
9
+ var mb = require ( '../Message' ) ;
10
+ var Channel = require ( '../Channel' ) ;
9
11
( function ( PromptType ) {
10
12
PromptType [ PromptType [ "text" ] = 0 ] = "text" ;
11
13
PromptType [ PromptType [ "number" ] = 1 ] = "number" ;
@@ -18,6 +20,8 @@ var PromptType = exports.PromptType;
18
20
ListStyle [ ListStyle [ "none" ] = 0 ] = "none" ;
19
21
ListStyle [ ListStyle [ "inline" ] = 1 ] = "inline" ;
20
22
ListStyle [ ListStyle [ "list" ] = 2 ] = "list" ;
23
+ ListStyle [ ListStyle [ "button" ] = 3 ] = "button" ;
24
+ ListStyle [ ListStyle [ "auto" ] = 4 ] = "auto" ;
21
25
} ) ( exports . ListStyle || ( exports . ListStyle = { } ) ) ;
22
26
var ListStyle = exports . ListStyle ;
23
27
var SimplePromptRecognizer = ( function ( ) {
@@ -109,9 +113,10 @@ var Prompts = (function (_super) {
109
113
session . dialogData [ key ] = args [ key ] ;
110
114
}
111
115
}
112
- session . send ( args . prompt ) ;
116
+ session . send ( this . formatPrompt ( session , args ) ) ;
113
117
} ;
114
118
Prompts . prototype . replyReceived = function ( session ) {
119
+ var _this = this ;
115
120
var args = session . dialogData ;
116
121
Prompts . options . recognizer . recognize ( {
117
122
promptType : args . promptType ,
@@ -131,11 +136,90 @@ var Prompts = (function (_super) {
131
136
}
132
137
else {
133
138
args . maxRetries -- ;
134
- session . send ( args . retryPrompt || "I didn't understand. " + args . prompt ) ;
139
+ session . send ( _this . formatPrompt ( session , args , true ) ) ;
135
140
}
136
141
}
137
142
} ) ;
138
143
} ;
144
+ Prompts . prototype . formatPrompt = function ( session , args , retry ) {
145
+ if ( retry === void 0 ) { retry = false ; }
146
+ var prompt = args . prompt ;
147
+ if ( Array . isArray ( prompt ) ) {
148
+ prompt = mb . Message . randomPrompt ( prompt ) ;
149
+ }
150
+ if ( retry ) {
151
+ if ( args . retryPrompt ) {
152
+ prompt = args . retryPrompt ;
153
+ if ( Array . isArray ( prompt ) ) {
154
+ prompt = mb . Message . randomPrompt ( prompt ) ;
155
+ }
156
+ }
157
+ else if ( typeof prompt == 'string' ) {
158
+ prompt = Prompts . options . defaultRetryPrompt + ' ' + prompt ;
159
+ }
160
+ }
161
+ if ( typeof prompt == 'string' ) {
162
+ var msg = new mb . Message ( ) ;
163
+ if ( args . promptType == PromptType . choice ) {
164
+ var style = args . listStyle || ListStyle . auto ;
165
+ if ( style == ListStyle . auto ) {
166
+ var maxBtns = Channel . maxButtons ( session ) ;
167
+ if ( maxBtns > 0 && args . enumValues . length <= maxBtns ) {
168
+ style = ListStyle . button ;
169
+ }
170
+ else if ( args . enumValues . length < 4 ) {
171
+ style = ListStyle . inline ;
172
+ }
173
+ else {
174
+ style = ListStyle . list ;
175
+ }
176
+ }
177
+ var connector = '' , list ;
178
+ switch ( style ) {
179
+ case ListStyle . button :
180
+ var a = { actions : [ ] } ;
181
+ for ( var i = 0 ; i < session . dialogData . enumValues . length ; i ++ ) {
182
+ var action = session . dialogData . enumValues [ i ] ;
183
+ a . actions . push ( { title : action , message : action } ) ;
184
+ }
185
+ msg . setText ( session , prompt )
186
+ . addAttachment ( a ) ;
187
+ break ;
188
+ case ListStyle . inline :
189
+ list = ' ' ;
190
+ args . enumValues . forEach ( function ( value , index ) {
191
+ list += connector + ( index + 1 ) + '. ' + value ;
192
+ if ( index == args . enumValues . length - 2 ) {
193
+ connector = index == 0 ? ' or ' : ', or ' ;
194
+ }
195
+ else {
196
+ connector = ', ' ;
197
+ }
198
+ } ) ;
199
+ msg . setText ( session , prompt + '%s' , list ) ;
200
+ break ;
201
+ case ListStyle . list :
202
+ list = '\n ' ;
203
+ args . enumValues . forEach ( function ( value , index ) {
204
+ list += connector + ( index + 1 ) + '. ' + value ;
205
+ connector = '\n ' ;
206
+ } ) ;
207
+ msg . setText ( session , prompt + '%s' , list ) ;
208
+ break ;
209
+ default :
210
+ msg . setText ( session , prompt ) ;
211
+ break ;
212
+ }
213
+ }
214
+ else {
215
+ msg . setText ( session , prompt ) ;
216
+ }
217
+ return msg ;
218
+ }
219
+ else {
220
+ return prompt ;
221
+ }
222
+ } ;
139
223
Prompts . configure = function ( options ) {
140
224
if ( options ) {
141
225
for ( var key in options ) {
@@ -145,30 +229,30 @@ var Prompts = (function (_super) {
145
229
}
146
230
}
147
231
} ;
148
- Prompts . text = function ( ses , prompt ) {
149
- beginPrompt ( ses , {
232
+ Prompts . text = function ( session , prompt ) {
233
+ beginPrompt ( session , {
150
234
promptType : PromptType . text ,
151
235
prompt : prompt
152
236
} ) ;
153
237
} ;
154
- Prompts . number = function ( ses , prompt , options ) {
238
+ Prompts . number = function ( session , prompt , options ) {
155
239
var args = options || { } ;
156
240
args . promptType = PromptType . number ;
157
241
args . prompt = prompt ;
158
- beginPrompt ( ses , args ) ;
242
+ beginPrompt ( session , args ) ;
159
243
} ;
160
- Prompts . confirm = function ( ses , prompt , options ) {
244
+ Prompts . confirm = function ( session , prompt , options ) {
161
245
var args = options || { } ;
162
246
args . promptType = PromptType . confirm ;
163
247
args . prompt = prompt ;
164
- beginPrompt ( ses , args ) ;
248
+ beginPrompt ( session , args ) ;
165
249
} ;
166
- Prompts . choice = function ( ses , prompt , choices , options ) {
250
+ Prompts . choice = function ( session , prompt , choices , options ) {
167
251
var args = options || { } ;
168
252
args . promptType = PromptType . choice ;
169
253
args . prompt = prompt ;
170
254
args . enumValues = entities . EntityRecognizer . expandChoices ( choices ) ;
171
- args . listStyle = args . listStyle || ListStyle . list ;
255
+ args . listStyle = args . listStyle || ListStyle . auto ;
172
256
var connector = '' , list ;
173
257
switch ( args . listStyle ) {
174
258
case ListStyle . list :
@@ -193,20 +277,21 @@ var Prompts = (function (_super) {
193
277
args . prompt += list ;
194
278
break ;
195
279
}
196
- beginPrompt ( ses , args ) ;
280
+ beginPrompt ( session , args ) ;
197
281
} ;
198
- Prompts . time = function ( ses , prompt , options ) {
282
+ Prompts . time = function ( session , prompt , options ) {
199
283
var args = options || { } ;
200
284
args . promptType = PromptType . time ;
201
285
args . prompt = prompt ;
202
- beginPrompt ( ses , args ) ;
286
+ beginPrompt ( session , args ) ;
203
287
} ;
204
288
Prompts . options = {
205
- recognizer : new SimplePromptRecognizer ( )
289
+ recognizer : new SimplePromptRecognizer ( ) ,
290
+ defaultRetryPrompt : "I didn't understand."
206
291
} ;
207
292
return Prompts ;
208
293
} ) ( dialog . Dialog ) ;
209
294
exports . Prompts = Prompts ;
210
- function beginPrompt ( ses , args ) {
211
- ses . beginDialog ( consts . DialogId . Prompts , args ) ;
295
+ function beginPrompt ( session , args ) {
296
+ session . beginDialog ( consts . DialogId . Prompts , args ) ;
212
297
}
0 commit comments