@@ -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,64 @@ 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+ const { body } = await fetch ( attachment . mediaUri )
235+
236+ formData . append ( 'file' , body , {
237+ filename : attachmentName
238+ } )
239+ }
240+ }
241+
242+ // Ensure directline is connected!
243+ await this . directLine . checkConnection ( true )
244+ fetch ( `${ this . directLine . domain } /conversations/${ this . directLine . conversationId } /upload?userId=${ activity . from . id } ` , {
245+ method : 'POST' ,
246+ headers : {
247+ 'Authorization' : `Bearer ${ this . directLine . token } `
248+ } ,
249+ body : formData
250+ } ) . catch ( err => {
221251 debug ( 'Error posting activity' , err )
222252 reject ( new Error ( `Error posting activity: ${ err } ` ) )
223- }
224- )
253+ } ) . then ( async ( res ) => {
254+ const json = await res . json ( )
255+ debug ( 'Posted activity, assigned ID:' , json . id )
256+ resolve ( )
257+ } )
258+ } else {
259+ debug ( 'Posting activity ' , JSON . stringify ( activity , null , 2 ) )
260+
261+ this . directLine . postActivity ( activity ) . subscribe (
262+ id => {
263+ debug ( 'Posted activity, assigned ID:' , id )
264+ resolve ( )
265+ } ,
266+ err => {
267+ debug ( 'Error posting activity' , err )
268+ reject ( new Error ( `Error posting activity: ${ err } ` ) )
269+ }
270+ )
271+ }
225272 } )
226273 }
227274
0 commit comments