1- const util = require ( 'util' )
21const uuidv4 = require ( 'uuid/v4' )
32const mime = require ( 'mime-types' )
43const _ = require ( 'lodash' )
@@ -37,7 +36,7 @@ const Defaults = {
3736 [ Capabilities . DIRECTLINE3_BUTTON_VALUE_FIELD ] : 'name' ,
3837 [ Capabilities . DIRECTLINE3_HANDLE_ACTIVITY_TYPES ] : 'message' ,
3938 [ Capabilities . DIRECTLINE3_ACTIVITY_VALUE_MAP ] : {
40- ' event' : 'name'
39+ event : 'name'
4140 }
4241}
4342
@@ -50,12 +49,12 @@ class BotiumConnectorDirectline3 {
5049 Validate ( ) {
5150 debug ( 'Validate called' )
5251
53- this . caps = Object . assign ( { } , Defaults , _ . pickBy ( this . caps , ( value , key ) => ! Defaults . hasOwnProperty ( key ) || ! _ . isString ( value ) || value !== '' ) )
52+ this . caps = Object . assign ( { } , Defaults , _ . pickBy ( this . caps , ( value , key ) => ! Object . prototype . hasOwnProperty . call ( Defaults , key ) || ! _ . isString ( value ) || value !== '' ) )
5453
55- if ( ! this . caps [ ' DIRECTLINE3_SECRET' ] ) throw new Error ( 'DIRECTLINE3_SECRET capability required' )
56- if ( ! this . caps [ ' DIRECTLINE3_BUTTON_TYPE' ] ) throw new Error ( 'DIRECTLINE3_BUTTON_TYPE capability required' )
57- if ( ! this . caps [ ' DIRECTLINE3_BUTTON_VALUE_FIELD' ] ) throw new Error ( 'DIRECTLINE3_BUTTON_VALUE_FIELD capability required' )
58- if ( ! this . caps [ ' DIRECTLINE3_HANDLE_ACTIVITY_TYPES' ] ) throw new Error ( 'DIRECTLINE3_HANDLE_ACTIVITY_TYPES capability required' )
54+ if ( ! this . caps . DIRECTLINE3_SECRET ) throw new Error ( 'DIRECTLINE3_SECRET capability required' )
55+ if ( ! this . caps . DIRECTLINE3_BUTTON_TYPE ) throw new Error ( 'DIRECTLINE3_BUTTON_TYPE capability required' )
56+ if ( ! this . caps . DIRECTLINE3_BUTTON_VALUE_FIELD ) throw new Error ( 'DIRECTLINE3_BUTTON_VALUE_FIELD capability required' )
57+ if ( ! this . caps . DIRECTLINE3_HANDLE_ACTIVITY_TYPES ) throw new Error ( 'DIRECTLINE3_HANDLE_ACTIVITY_TYPES capability required' )
5958
6059 return Promise . resolve ( )
6160 }
@@ -69,20 +68,20 @@ class BotiumConnectorDirectline3 {
6968 debug ( 'Start called' )
7069 this . _stopSubscription ( )
7170 this . directLine = new DirectLine ( {
72- secret : this . caps [ ' DIRECTLINE3_SECRET' ] ,
73- webSocket : this . caps [ ' DIRECTLINE3_WEBSOCKET' ] ,
74- domain : this . caps [ ' DIRECTLINE3_DOMAIN' ] ,
75- pollingInterval : this . caps [ ' DIRECTLINE3_POLLINGINTERVAL' ]
71+ secret : this . caps . DIRECTLINE3_SECRET ,
72+ webSocket : this . caps . DIRECTLINE3_WEBSOCKET ,
73+ domain : this . caps . DIRECTLINE3_DOMAIN ,
74+ pollingInterval : this . caps . DIRECTLINE3_POLLINGINTERVAL
7675 } )
7776
78- if ( this . caps [ ' DIRECTLINE3_GENERATE_USERNAME' ] ) {
77+ if ( this . caps . DIRECTLINE3_GENERATE_USERNAME ) {
7978 this . me = uuidv4 ( )
8079 } else {
8180 this . me = 'me'
8281 }
8382
8483 const isValidActivityType = ( activityType ) => {
85- const filter = this . caps [ ' DIRECTLINE3_HANDLE_ACTIVITY_TYPES' ]
84+ const filter = this . caps . DIRECTLINE3_HANDLE_ACTIVITY_TYPES
8685 if ( _ . isString ( filter ) ) {
8786 return filter . indexOf ( activityType ) >= 0
8887 } else if ( _ . isArray ( filter ) ) {
@@ -202,7 +201,7 @@ class BotiumConnectorDirectline3 {
202201 }
203202 }
204203 } else {
205- const valueMap = this . caps [ ' DIRECTLINE3_ACTIVITY_VALUE_MAP' ]
204+ const valueMap = this . caps . DIRECTLINE3_ACTIVITY_VALUE_MAP
206205 if ( valueMap && valueMap [ message . type ] ) {
207206 botMsg . messageText = message [ valueMap [ message . type ] ]
208207 } else {
@@ -243,7 +242,7 @@ class BotiumConnectorDirectline3 {
243242
244243 UserSays ( msg ) {
245244 debug ( 'UserSays called' )
246- return new Promise ( async ( resolve , reject ) => {
245+ return new Promise ( async ( resolve , reject ) => { // eslint-disable-line no-async-promise-executor
247246 const activity = msg . sourceData || { }
248247 if ( msg . buttons && msg . buttons . length > 0 && ( msg . buttons [ 0 ] . text || msg . buttons [ 0 ] . payload ) ) {
249248 let payload = msg . buttons [ 0 ] . payload || msg . buttons [ 0 ] . text
@@ -272,14 +271,22 @@ class BotiumConnectorDirectline3 {
272271 } )
273272 }
274273
274+ if ( msg . SET_ACTIVITY_VALUE ) {
275+ _ . keys ( msg . SET_ACTIVITY_VALUE ) . forEach ( key => {
276+ _ . set ( activity , key , msg . SET_ACTIVITY_VALUE [ key ] )
277+ } )
278+ }
279+
275280 if ( msg . media && msg . media . length > 0 ) {
276281 debug ( 'Posting activity with attachments ' , JSON . stringify ( activity , null , 2 ) )
277282 const formData = new FormData ( )
278283
279- formData . append ( 'activity' , Buffer . from ( JSON . stringify ( activity ) ) , {
280- contentType : 'application/vnd.microsoft.activity' ,
281- filename : 'blob'
282- } )
284+ if ( activity . text ) {
285+ formData . append ( 'activity' , Buffer . from ( JSON . stringify ( activity ) ) , {
286+ contentType : 'application/vnd.microsoft.activity' ,
287+ filename : 'blob'
288+ } )
289+ }
283290
284291 for ( let i = 0 ; i < msg . media . length ; i ++ ) {
285292 const attachment = msg . media [ i ]
@@ -294,30 +301,33 @@ class BotiumConnectorDirectline3 {
294301 const { body } = await fetch ( attachment . mediaUri )
295302
296303 formData . append ( 'file' , body , {
297- filename : attachmentName
304+ filename : attachmentName ,
305+ contentType : 'image/png'
298306 } )
299307 }
300308 }
301309
302- // Ensure directline is connected!
303310 await this . directLine . checkConnection ( true )
304- fetch ( `${ this . directLine . domain } /conversations/${ this . directLine . conversationId } /upload?userId=${ activity . from . id } ` , {
311+ const uploadUrl = `${ this . directLine . domain } /conversations/${ this . directLine . conversationId } /upload?userId=${ activity . from . id } `
312+ debug ( `Uploading attachments to ${ uploadUrl } ` )
313+ fetch ( uploadUrl , {
305314 method : 'POST' ,
306315 headers : {
307- 'Authorization' : `Bearer ${ this . directLine . token } `
316+ Authorization : `Bearer ${ this . directLine . token } ` ,
317+ 'Content-Type' : 'multipart/form-data'
308318 } ,
309319 body : formData
310- } ) . catch ( err => {
311- debug ( 'Error posting activity with attachments' , err )
312- reject ( new Error ( `Error posting activity: ${ err } ` ) )
313320 } ) . then ( async ( res ) => {
314321 const json = await res . json ( )
315- if ( json . id ) {
322+ if ( json && json . id ) {
316323 debug ( 'Posted activity with attachments, assigned ID:' , json . id )
317324 resolve ( )
318325 } else {
319- reject ( new Error ( ` Error posting activity with attachments: ${ util . inspect ( json ) } ` ) )
326+ reject ( new Error ( ' Error posting activity with attachments, no activity id returned' ) )
320327 }
328+ } ) . catch ( err => {
329+ debug ( 'Error posting activity with attachments' , err )
330+ reject ( new Error ( `Error posting activity: ${ err . message } ` ) )
321331 } )
322332 } else {
323333 debug ( 'Posting activity ' , JSON . stringify ( activity , null , 2 ) )
@@ -359,6 +369,11 @@ class BotiumConnectorDirectline3 {
359369 this . connSubscription . unsubscribe ( )
360370 this . connSubscription = null
361371 }
372+ if ( this . directLine ) {
373+ debug ( 'ending directline connection' )
374+ this . directLine . end ( )
375+ this . directLine = null
376+ }
362377 }
363378
364379 _deepFilter ( item , selectFn , filterFn ) {
0 commit comments