@@ -3,6 +3,10 @@ const mime = require('mime-types')
33const _ = require ( 'lodash' )
44const { DirectLine, ConnectionStatus } = require ( 'botframework-directlinejs' )
55const debug = require ( 'debug' ) ( 'botium-connector-directline3' )
6+ const FormData = require ( 'form-data' )
7+ const fetch = require ( 'node-fetch' )
8+ const fs = require ( 'fs' )
9+ const path = require ( 'path' )
610
711global . XMLHttpRequest = require ( 'xhr2' )
812
@@ -191,7 +195,7 @@ class BotiumConnectorDirectline3 {
191195
192196 UserSays ( msg ) {
193197 debug ( 'UserSays called' )
194- return new Promise ( ( resolve , reject ) => {
198+ return new Promise ( async ( resolve , reject ) => {
195199 const activity = {
196200 from : { id : this . me }
197201 }
@@ -207,21 +211,62 @@ class BotiumConnectorDirectline3 {
207211 activity . type = 'message'
208212 activity . text = msg . messageText
209213 }
214+
210215 if ( msg . media && msg . media . length > 0 ) {
211- return reject ( new Error ( `Media Attachments currently not possible.` ) )
212- }
213- debug ( 'Posting activity ' , JSON . stringify ( activity , null , 2 ) )
216+ debug ( 'Posting activity with attachments ' , JSON . stringify ( activity , null , 2 ) )
217+ const formData = new FormData ( )
214218
215- this . directLine . postActivity ( activity ) . subscribe (
216- id => {
217- debug ( 'Posted activity, assigned ID ' , id )
218- resolve ( )
219- } ,
220- err => {
219+ formData . append ( 'activity' , Buffer . from ( JSON . stringify ( activity ) ) , {
220+ contentType : 'application/vnd.microsoft.activity' ,
221+ filename : 'blob'
222+ } )
223+
224+ for ( let i = 0 ; i < msg . media . length ; i ++ ) {
225+ const attachment = msg . media [ i ]
226+ const attachmentName = path . basename ( attachment . mediaUri )
227+
228+ if ( attachment . mediaUri . startsWith ( 'file://' ) ) {
229+ const filepath = attachment . mediaUri . split ( 'file://' ) [ 1 ]
230+ formData . append ( 'file' , fs . createReadStream ( filepath ) , {
231+ filename : attachmentName
232+ } )
233+ } else {
234+ formData . append ( 'file' , ( await fetch ( attachment . mediaUri ) ) . body , {
235+ filename : attachmentName
236+ } )
237+ }
238+ }
239+
240+ // Ensure directline is connected!
241+ await this . directLine . checkConnection ( true )
242+ fetch ( `${ this . directLine . domain } /conversations/${ this . directLine . conversationId } /upload?userId=${ activity . from . id } ` , {
243+ method : 'POST' ,
244+ headers : {
245+ 'Authorization' : `Bearer ${ this . directLine . token } `
246+ } ,
247+ body : formData
248+ } ) . catch ( err => {
221249 debug ( 'Error posting activity' , err )
222250 reject ( new Error ( `Error posting activity: ${ err } ` ) )
223- }
224- )
251+ } ) . then ( async ( res ) => {
252+ const json = await res . json ( )
253+ debug ( 'Posted activity, assigned ID:' , json . id )
254+ resolve ( )
255+ } )
256+ } else {
257+ debug ( 'Posting activity ' , JSON . stringify ( activity , null , 2 ) )
258+
259+ this . directLine . postActivity ( activity ) . subscribe (
260+ id => {
261+ debug ( 'Posted activity, assigned ID:' , id )
262+ resolve ( )
263+ } ,
264+ err => {
265+ debug ( 'Error posting activity' , err )
266+ reject ( new Error ( `Error posting activity: ${ err } ` ) )
267+ }
268+ )
269+ }
225270 } )
226271 }
227272
0 commit comments