@@ -6,8 +6,8 @@ var __extends = (this && this.__extends) || function (d, b) {
6
6
var collection = require ( '../dialogs/DialogCollection' ) ;
7
7
var session = require ( '../Session' ) ;
8
8
var consts = require ( '../consts' ) ;
9
- var utils = require ( '../utils' ) ;
10
9
var request = require ( 'request' ) ;
10
+ var uuid = require ( 'node-uuid' ) ;
11
11
var BotConnectorBot = ( function ( _super ) {
12
12
__extends ( BotConnectorBot , _super ) ;
13
13
function BotConnectorBot ( options ) {
@@ -16,7 +16,6 @@ var BotConnectorBot = (function (_super) {
16
16
endpoint : process . env [ 'endpoint' ] || 'https://api.botframework.com' ,
17
17
appId : process . env [ 'appId' ] || '' ,
18
18
appSecret : process . env [ 'appSecret' ] || '' ,
19
- subscriptionKey : process . env [ 'subscriptionKey' ] || '' ,
20
19
defaultDialogId : '/'
21
20
} ;
22
21
this . configure ( options ) ;
@@ -68,7 +67,7 @@ var BotConnectorBot = (function (_super) {
68
67
this . configure ( options ) ;
69
68
return function ( req , res ) {
70
69
if ( req . body ) {
71
- _this . processMessage ( req . body , _this . options . defaultDialogId , _this . options . defaultDialogArgs , res ) ;
70
+ _this . dispatchMessage ( null , req . body , _this . options . defaultDialogId , _this . options . defaultDialogArgs , res ) ;
72
71
}
73
72
else {
74
73
var requestData = '' ;
@@ -78,7 +77,7 @@ var BotConnectorBot = (function (_super) {
78
77
req . on ( 'end' , function ( ) {
79
78
try {
80
79
var msg = JSON . parse ( requestData ) ;
81
- _this . processMessage ( msg , _this . options . defaultDialogId , _this . options . defaultDialogArgs , res ) ;
80
+ _this . dispatchMessage ( null , msg , _this . options . defaultDialogId , _this . options . defaultDialogArgs , res ) ;
82
81
}
83
82
catch ( e ) {
84
83
_this . emit ( 'error' , new Error ( 'Invalid Bot Framework Message' ) ) ;
@@ -100,14 +99,32 @@ var BotConnectorBot = (function (_super) {
100
99
if ( ! this . hasDialog ( dialogId ) ) {
101
100
throw new Error ( 'Invalid dialog passed to BotConnectorBot.beginDialog().' ) ;
102
101
}
103
- this . processMessage ( msg , dialogId , dialogArgs ) ;
102
+ this . dispatchMessage ( msg . to . id , msg , dialogId , dialogArgs ) ;
104
103
} ;
105
- BotConnectorBot . prototype . processMessage = function ( message , dialogId , dialogArgs , res ) {
104
+ BotConnectorBot . prototype . dispatchMessage = function ( userId , message , dialogId , dialogArgs , res ) {
106
105
var _this = this ;
107
106
try {
108
107
if ( ! message || ! message . type ) {
109
108
this . emit ( 'error' , new Error ( 'Invalid Bot Framework Message' ) ) ;
110
- return res . send ( 400 ) ;
109
+ return res ? res . send ( 400 ) : null ;
110
+ }
111
+ if ( ! userId ) {
112
+ if ( message . from && message . from . id ) {
113
+ userId = message . from . id ;
114
+ }
115
+ else {
116
+ this . emit ( 'error' , new Error ( 'Invalid Bot Framework Message' ) ) ;
117
+ return res ? res . send ( 400 ) : null ;
118
+ }
119
+ }
120
+ var sessionId ;
121
+ if ( message . botConversationData && message . botConversationData [ consts . Data . SessionId ] ) {
122
+ sessionId = message . botConversationData [ consts . Data . SessionId ] ;
123
+ }
124
+ else {
125
+ sessionId = uuid . v1 ( ) ;
126
+ message . botConversationData = message . botConversationData || { } ;
127
+ message . botConversationData [ consts . Data . SessionId ] = sessionId ;
111
128
}
112
129
this . emit ( message . type , message ) ;
113
130
if ( message . type == 'Message' ) {
@@ -117,42 +134,51 @@ var BotConnectorBot = (function (_super) {
117
134
dialogId : dialogId ,
118
135
dialogArgs : dialogArgs
119
136
} ) ;
120
- ses . on ( 'send' , function ( message ) {
121
- var reply = message || { } ;
122
- reply . botUserData = utils . clone ( ses . userData ) ;
123
- reply . botConversationData = utils . clone ( ses . conversationData ) ;
124
- reply . botPerUserInConversationData = utils . clone ( ses . perUserInConversationData ) ;
125
- reply . botPerUserInConversationData [ consts . Data . SessionState ] = ses . sessionState ;
126
- if ( reply . text && ! reply . language ) {
127
- reply . language = ses . message . language ;
128
- }
129
- if ( res ) {
130
- _this . emit ( 'reply' , reply ) ;
131
- res . send ( 200 , reply ) ;
132
- res = null ;
133
- }
134
- else if ( ses . message . conversationId ) {
135
- reply . from = ses . message . to ;
136
- reply . to = ses . message . replyTo ? ses . message . replyTo : ses . message . from ;
137
- reply . replyToMessageId = ses . message . id ;
138
- reply . conversationId = ses . message . conversationId ;
139
- reply . channelConversationId = ses . message . channelConversationId ;
140
- reply . channelMessageId = ses . message . channelMessageId ;
141
- reply . participants = ses . message . participants ;
142
- reply . totalParticipants = ses . message . totalParticipants ;
143
- _this . emit ( 'reply' , reply ) ;
144
- _this . post ( '/bot/v1.0/messages' , reply , function ( err ) {
145
- _this . emit ( 'error' , err ) ;
146
- } ) ;
147
- }
148
- else {
149
- reply . from = ses . message . from ;
150
- reply . to = ses . message . to ;
151
- _this . emit ( 'send' , reply ) ;
152
- _this . post ( '/bot/v1.0/messages' , reply , function ( err ) {
153
- _this . emit ( 'error' , err ) ;
154
- } ) ;
137
+ ses . on ( 'send' , function ( reply ) {
138
+ reply = reply || { } ;
139
+ reply . botConversationData = message . botConversationData ;
140
+ if ( reply . text && ! reply . language && message . language ) {
141
+ reply . language = message . language ;
155
142
}
143
+ var data = {
144
+ userData : ses . userData ,
145
+ conversationData : ses . conversationData ,
146
+ perUserConversationData : ses . perUserInConversationData
147
+ } ;
148
+ data . perUserConversationData [ consts . Data . SessionState ] = ses . sessionState ;
149
+ _this . saveData ( userId , sessionId , data , reply , function ( err ) {
150
+ if ( res ) {
151
+ _this . emit ( 'reply' , reply ) ;
152
+ res . send ( 200 , reply ) ;
153
+ res = null ;
154
+ }
155
+ else if ( ses . message . conversationId ) {
156
+ reply . from = ses . message . to ;
157
+ reply . to = ses . message . replyTo ? ses . message . replyTo : ses . message . from ;
158
+ reply . replyToMessageId = ses . message . id ;
159
+ reply . conversationId = ses . message . conversationId ;
160
+ reply . channelConversationId = ses . message . channelConversationId ;
161
+ reply . channelMessageId = ses . message . channelMessageId ;
162
+ reply . participants = ses . message . participants ;
163
+ reply . totalParticipants = ses . message . totalParticipants ;
164
+ _this . emit ( 'reply' , reply ) ;
165
+ post ( _this . options , '/bot/v1.0/messages' , reply , function ( err ) {
166
+ if ( err ) {
167
+ _this . emit ( 'error' , err ) ;
168
+ }
169
+ } ) ;
170
+ }
171
+ else {
172
+ reply . from = ses . message . from ;
173
+ reply . to = ses . message . to ;
174
+ _this . emit ( 'send' , reply ) ;
175
+ post ( _this . options , '/bot/v1.0/messages' , reply , function ( err ) {
176
+ if ( err ) {
177
+ _this . emit ( 'error' , err ) ;
178
+ }
179
+ } ) ;
180
+ }
181
+ } ) ;
156
182
} ) ;
157
183
ses . on ( 'error' , function ( err ) {
158
184
_this . emit ( 'error' , err , ses . message ) ;
@@ -163,33 +189,22 @@ var BotConnectorBot = (function (_super) {
163
189
ses . on ( 'quit' , function ( ) {
164
190
_this . emit ( 'quit' , ses . message ) ;
165
191
} ) ;
166
- var sessionState ;
167
- if ( message . botUserData ) {
168
- ses . userData = message . botUserData ;
169
- delete message . botUserData ;
170
- }
171
- else {
172
- ses . userData = { } ;
173
- }
174
- if ( message . botConversationData ) {
175
- ses . conversationData = message . botConversationData ;
176
- delete message . botConversationData ;
177
- }
178
- else {
179
- ses . conversationData = { } ;
180
- }
181
- if ( message . botPerUserInConversationData ) {
182
- if ( message . botPerUserInConversationData . hasOwnProperty ( consts . Data . SessionState ) ) {
183
- sessionState = message . botPerUserInConversationData [ consts . Data . SessionState ] ;
184
- delete message . botPerUserInConversationData [ consts . Data . SessionState ] ;
192
+ this . getData ( userId , sessionId , message , function ( err , data ) {
193
+ if ( ! err ) {
194
+ var sessionState ;
195
+ ses . userData = data . userData || { } ;
196
+ ses . conversationData = data . conversationData || { } ;
197
+ ses . perUserInConversationData = data . perUserConversationData || { } ;
198
+ if ( ses . perUserInConversationData . hasOwnProperty ( consts . Data . SessionState ) ) {
199
+ sessionState = ses . perUserInConversationData [ consts . Data . SessionState ] ;
200
+ delete ses . perUserInConversationData [ consts . Data . SessionState ] ;
201
+ }
202
+ ses . dispatch ( sessionState , message ) ;
185
203
}
186
- ses . perUserInConversationData = message . botPerUserInConversationData ;
187
- delete message . botPerUserInConversationData ;
188
- }
189
- else {
190
- ses . perUserInConversationData = { } ;
191
- }
192
- ses . dispatch ( sessionState , message ) ;
204
+ else {
205
+ _this . emit ( 'error' , err , message ) ;
206
+ }
207
+ } ) ;
193
208
}
194
209
else if ( res ) {
195
210
var msg ;
@@ -212,22 +227,69 @@ var BotConnectorBot = (function (_super) {
212
227
res . send ( 500 ) ;
213
228
}
214
229
} ;
215
- BotConnectorBot . prototype . post = function ( path , body , callback ) {
216
- var settings = this . options ;
217
- var options = {
218
- url : settings . endpoint + path ,
219
- body : body
220
- } ;
221
- if ( settings . appId && settings . appSecret ) {
222
- options . auth = {
223
- username : settings . appId ,
224
- password : settings . appSecret
225
- } ;
226
- options . headers = {
227
- 'Ocp-Apim-Subscription-Key' : settings . appSecret
228
- } ;
230
+ BotConnectorBot . prototype . getData = function ( userId , sessionId , msg , callback ) {
231
+ var botPath = '/' + this . options . appId ;
232
+ var userPath = botPath + '/users/' + userId ;
233
+ var convoPath = botPath + '/conversations/' + sessionId ;
234
+ var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId ;
235
+ var ops = 3 ;
236
+ var data = { } ;
237
+ function load ( id , field , store , botData ) {
238
+ data [ field ] = botData ;
239
+ if ( store ) {
240
+ store . get ( id , function ( err , item ) {
241
+ if ( callback ) {
242
+ if ( ! err ) {
243
+ data [ field ] = item ;
244
+ if ( -- ops == 0 ) {
245
+ callback ( null , data ) ;
246
+ }
247
+ }
248
+ else {
249
+ callback ( err , null ) ;
250
+ callback = null ;
251
+ }
252
+ }
253
+ } ) ;
254
+ }
255
+ else if ( callback && -- ops == 0 ) {
256
+ callback ( null , data ) ;
257
+ }
258
+ }
259
+ load ( userPath , 'userData' , this . options . userStore , msg . botUserData ) ;
260
+ load ( convoPath , 'conversationData' , this . options . conversationStore , msg . botConversationData ) ;
261
+ load ( perUserConvoPath , 'perUserConversationData' , this . options . perUserInConversationStore , msg . botPerUserInConversationData ) ;
262
+ } ;
263
+ BotConnectorBot . prototype . saveData = function ( userId , sessionId , data , msg , callback ) {
264
+ var botPath = '/' + this . options . appId ;
265
+ var userPath = botPath + '/users/' + userId ;
266
+ var convoPath = botPath + '/conversations/' + sessionId ;
267
+ var perUserConvoPath = botPath + '/conversations/' + sessionId + '/users/' + userId ;
268
+ var ops = 3 ;
269
+ function save ( id , field , store , botData ) {
270
+ if ( store ) {
271
+ store . save ( id , botData , function ( err ) {
272
+ if ( callback ) {
273
+ if ( ! err && -- ops == 0 ) {
274
+ callback ( null ) ;
275
+ }
276
+ else {
277
+ callback ( err ) ;
278
+ callback = null ;
279
+ }
280
+ }
281
+ } ) ;
282
+ }
283
+ else {
284
+ msg [ field ] = botData ;
285
+ if ( callback && -- ops == 0 ) {
286
+ callback ( null ) ;
287
+ }
288
+ }
229
289
}
230
- request . post ( options , callback ) ;
290
+ save ( userPath , 'botUserData' , this . options . userStore , data . userData ) ;
291
+ save ( convoPath , 'botConversationData' , this . options . conversationStore , data . conversationData ) ;
292
+ save ( perUserConvoPath , 'botPerUserInConversationData' , this . options . perUserInConversationStore , data . perUserConversationData ) ;
231
293
} ;
232
294
return BotConnectorBot ;
233
295
} ) ( collection . DialogCollection ) ;
@@ -240,3 +302,21 @@ var BotConnectorSession = (function (_super) {
240
302
return BotConnectorSession ;
241
303
} ) ( session . Session ) ;
242
304
exports . BotConnectorSession = BotConnectorSession ;
305
+ function post ( settings , path , body , callback ) {
306
+ var options = {
307
+ method : 'POST' ,
308
+ url : settings . endpoint + path ,
309
+ body : body ,
310
+ json : true
311
+ } ;
312
+ if ( settings . appId && settings . appSecret ) {
313
+ options . auth = {
314
+ username : settings . appId ,
315
+ password : settings . appSecret
316
+ } ;
317
+ options . headers = {
318
+ 'Ocp-Apim-Subscription-Key' : settings . appSecret
319
+ } ;
320
+ }
321
+ request ( options , callback ) ;
322
+ }
0 commit comments