Skip to content

Commit db78780

Browse files
author
Botium
authored
Merge pull request #11 from codeforequity-at/develop
feat: attachment upload to directline
2 parents 5c70dc6 + a4c4df8 commit db78780

File tree

3 files changed

+75
-18
lines changed

3 files changed

+75
-18
lines changed

index.js

Lines changed: 59 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,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

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,30 @@
1919
},
2020
"homepage": "https://github.com/codeforequity-at/botium-connector-directline3#readme",
2121
"devDependencies": {
22+
"@babel/core": "^7.2.2",
23+
"@babel/node": "^7.2.2",
24+
"@babel/plugin-transform-runtime": "^7.2.0",
25+
"@babel/preset-env": "^7.2.3",
2226
"eslint": "^5.12.0",
2327
"eslint-config-standard": "^12.0.0",
2428
"eslint-plugin-import": "^2.14.0",
2529
"eslint-plugin-node": "^8.0.1",
2630
"eslint-plugin-promise": "^4.0.1",
2731
"eslint-plugin-standard": "^4.0.0",
2832
"rollup": "^0.58.2",
29-
"rollup-plugin-buble": "^0.19.2",
33+
"rollup-plugin-babel": "^4.3.2",
3034
"rollup-plugin-commonjs": "^9.1.3",
3135
"rollup-plugin-json": "^2.3.0",
3236
"rollup-plugin-node-resolve": "^3.3.0"
3337
},
3438
"dependencies": {
39+
"@babel/runtime": "^7.2.0",
3540
"botframework-directlinejs": "^0.9.17",
3641
"debug": "^3.1.0",
42+
"form-data": "^2.3.3",
3743
"lodash": "^4.17.11",
3844
"mime-types": "^2.1.20",
45+
"node-fetch": "^2.3.0",
3946
"uuid": "^3.3.2",
4047
"xhr2": "^0.1.4"
4148
}

rollup.config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import buble from 'rollup-plugin-buble';
2-
import commonjs from 'rollup-plugin-commonjs';
3-
import json from 'rollup-plugin-json';
1+
import babel from 'rollup-plugin-babel'
2+
import commonjs from 'rollup-plugin-commonjs'
3+
import json from 'rollup-plugin-json'
44

55
export default {
66
input: 'index.js',
@@ -20,7 +20,10 @@ export default {
2020
commonjs({
2121
exclude: 'node_modules/**'
2222
}),
23-
buble(),
23+
babel({
24+
exclude: 'node_modules/**',
25+
runtimeHelpers: true
26+
}),
2427
json()
2528
]
26-
};
29+
}

0 commit comments

Comments
 (0)