Skip to content

Commit 1ce37d4

Browse files
author
Botium
authored
Merge pull request #10 from alopix/master
feat: attachment upload to directline
2 parents 5c70dc6 + f2c0236 commit 1ce37d4

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

index.js

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const mime = require('mime-types')
33
const _ = require('lodash')
44
const { DirectLine, ConnectionStatus } = require('botframework-directlinejs')
55
const 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

711
global.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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
"dependencies": {
3535
"botframework-directlinejs": "^0.9.17",
3636
"debug": "^3.1.0",
37+
"form-data": "^2.3.3",
3738
"lodash": "^4.17.11",
3839
"mime-types": "^2.1.20",
40+
"node-fetch": "^2.3.0",
3941
"uuid": "^3.3.2",
4042
"xhr2": "^0.1.4"
4143
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default {
2020
commonjs({
2121
exclude: 'node_modules/**'
2222
}),
23-
buble(),
23+
buble({ transforms: { asyncAwait: false } }),
2424
json()
2525
]
2626
};

0 commit comments

Comments
 (0)