Skip to content

Commit c130653

Browse files
author
Botium
authored
Merge pull request #36 from codeforequity-at/develop
Version 0.0.16
2 parents 425e062 + 9da7269 commit c130653

File tree

13 files changed

+59
-97
lines changed

13 files changed

+59
-97
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ The parameters are:
8989
2. The path to the activity field
9090
3. The value of the activity field
9191

92+
Another option for having full control over the activity is to use the **DIRECTLINE3_ACTIVITY_TEMPLATE** capability. It is a JSON structure used as a template when sending an activity to the bot.
93+
94+
```
95+
...
96+
"DIRECTLINE3_ACTIVITY_TEMPLATE": {
97+
"channelData": {
98+
"my-special-channel-data": "..."
99+
}
100+
}
101+
...
102+
```
103+
104+
92105
## Supported Capabilities
93106

94107
Set the capability __CONTAINERMODE__ to __directline3__ to activate this connector.
@@ -143,6 +156,11 @@ When using activity types other than _message_, this capability maps the activit
143156

144157
By default, for _event_ activities the _name_ attribute is used as message text, for other activity types the activity type itself.
145158

159+
### DIRECTLINE3_ACTIVITY_TEMPLATE
160+
_default: {}_
161+
162+
JSON object holding the activity template used for sending activities to the bot.
163+
146164
# Current Restrictions
147165

148166
* Only HTTP Polling supported (WebSocket not available in Node.js)

index.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const Capabilities = {
2525
DIRECTLINE3_BUTTON_TYPE: 'DIRECTLINE3_BUTTON_TYPE',
2626
DIRECTLINE3_BUTTON_VALUE_FIELD: 'DIRECTLINE3_BUTTON_VALUE_FIELD',
2727
DIRECTLINE3_HANDLE_ACTIVITY_TYPES: 'DIRECTLINE3_HANDLE_ACTIVITY_TYPES',
28-
DIRECTLINE3_ACTIVITY_VALUE_MAP: 'DIRECTLINE3_ACTIVITY_VALUE_MAP'
28+
DIRECTLINE3_ACTIVITY_VALUE_MAP: 'DIRECTLINE3_ACTIVITY_VALUE_MAP',
29+
DIRECTLINE3_ACTIVITY_TEMPLATE: 'DIRECTLINE3_ACTIVITY_TEMPLATE'
2930
}
3031

3132
const Defaults = {
@@ -56,6 +57,17 @@ class BotiumConnectorDirectline3 {
5657
if (!this.caps.DIRECTLINE3_BUTTON_VALUE_FIELD) throw new Error('DIRECTLINE3_BUTTON_VALUE_FIELD capability required')
5758
if (!this.caps.DIRECTLINE3_HANDLE_ACTIVITY_TYPES) throw new Error('DIRECTLINE3_HANDLE_ACTIVITY_TYPES capability required')
5859

60+
if (this.caps.DIRECTLINE3_ACTIVITY_VALUE_MAP) {
61+
if (_.isString(this.caps.DIRECTLINE3_ACTIVITY_VALUE_MAP)) {
62+
this.caps.DIRECTLINE3_ACTIVITY_VALUE_MAP = JSON.parse(this.caps.DIRECTLINE3_ACTIVITY_VALUE_MAP)
63+
}
64+
}
65+
if (this.caps.DIRECTLINE3_ACTIVITY_TEMPLATE) {
66+
if (_.isString(this.caps.DIRECTLINE3_ACTIVITY_TEMPLATE)) {
67+
this.caps.DIRECTLINE3_ACTIVITY_TEMPLATE = JSON.parse(this.caps.DIRECTLINE3_ACTIVITY_TEMPLATE)
68+
}
69+
}
70+
5971
return Promise.resolve()
6072
}
6173

@@ -243,15 +255,15 @@ class BotiumConnectorDirectline3 {
243255
UserSays (msg) {
244256
debug('UserSays called')
245257
return new Promise(async (resolve, reject) => { // eslint-disable-line no-async-promise-executor
246-
const activity = msg.sourceData || {}
258+
const activity = Object.assign({}, msg.sourceData || this.caps.DIRECTLINE3_ACTIVITY_TEMPLATE || {})
247259
if (msg.buttons && msg.buttons.length > 0 && (msg.buttons[0].text || msg.buttons[0].payload)) {
248260
let payload = msg.buttons[0].payload || msg.buttons[0].text
249261
try {
250262
payload = JSON.parse(payload)
251263
} catch (err) {
252264
}
253265
activity.type = this.caps[Capabilities.DIRECTLINE3_BUTTON_TYPE]
254-
activity[this.caps[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD]] = payload
266+
_.set(activity, this.caps[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD], payload)
255267
} else {
256268
if (!activity.type) {
257269
activity.type = 'message'
@@ -265,9 +277,9 @@ class BotiumConnectorDirectline3 {
265277
}
266278

267279
if (msg.forms) {
268-
activity.value = {}
280+
activity.value = activity.value || {}
269281
msg.forms.forEach(f => {
270-
activity.value[f.name] = f.value
282+
_.set(activity.value, f.name, f.value)
271283
})
272284
}
273285

@@ -279,14 +291,14 @@ class BotiumConnectorDirectline3 {
279291

280292
if (msg.media && msg.media.length > 0) {
281293
debug('Posting activity with attachments ', JSON.stringify(activity, null, 2))
294+
msg.sourceData = Object.assign(msg.sourceData || {}, { activity })
295+
282296
const formData = new FormData()
283297

284-
if (activity.text) {
285-
formData.append('activity', Buffer.from(JSON.stringify(activity)), {
286-
contentType: 'application/vnd.microsoft.activity',
287-
filename: 'blob'
288-
})
289-
}
298+
formData.append('activity', Buffer.from(JSON.stringify(activity)), {
299+
contentType: 'application/vnd.microsoft.activity',
300+
filename: 'blob'
301+
})
290302

291303
for (let i = 0; i < msg.media.length; i++) {
292304
const attachment = msg.media[i]
@@ -298,11 +310,11 @@ class BotiumConnectorDirectline3 {
298310
filename: attachmentName
299311
})
300312
} else {
301-
const { body } = await fetch(attachment.mediaUri)
313+
const res = await fetch(attachment.mediaUri)
314+
const body = await res.buffer()
302315

303316
formData.append('file', body, {
304-
filename: attachmentName,
305-
contentType: 'image/png'
317+
filename: attachmentName
306318
})
307319
}
308320
}
@@ -313,8 +325,7 @@ class BotiumConnectorDirectline3 {
313325
fetch(uploadUrl, {
314326
method: 'POST',
315327
headers: {
316-
Authorization: `Bearer ${this.directLine.token}`,
317-
'Content-Type': 'multipart/form-data'
328+
Authorization: `Bearer ${this.directLine.token}`
318329
},
319330
body: formData
320331
}).then(async (res) => {
@@ -327,10 +338,11 @@ class BotiumConnectorDirectline3 {
327338
}
328339
}).catch(err => {
329340
debug('Error posting activity with attachments', err)
330-
reject(new Error(`Error posting activity: ${err.message}`))
341+
reject(new Error(`Error posting activity: ${err.message || err}`))
331342
})
332343
} else {
333344
debug('Posting activity ', JSON.stringify(activity, null, 2))
345+
msg.sourceData = Object.assign(msg.sourceData || {}, { activity })
334346

335347
this.directLine.postActivity(activity).subscribe(
336348
id => {
@@ -339,7 +351,7 @@ class BotiumConnectorDirectline3 {
339351
},
340352
err => {
341353
debug('Error posting activity', err)
342-
reject(new Error(`Error posting activity: ${err}`))
354+
reject(new Error(`Error posting activity: ${err.message || err}`))
343355
}
344356
)
345357
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "botium-connector-directline3",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"description": "Botium Connector for Bot Framework Direct Line 3 API",
55
"main": "dist/botium-connector-directline3-cjs.js",
66
"module": "dist/botium-connector-directline3-es.js",

samples/convo/botium.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
"DIRECTLINE3_SECRET": "my-directline-secret",
77
"DIRECTLINE3_BUTTON_TYPE": "message",
88
"DIRECTLINE3_BUTTON_VALUE_FIELD": "text",
9-
"DIRECTLINE3_HANDLE_ACTIVITY_TYPES": "message,event"
9+
"DIRECTLINE3_HANDLE_ACTIVITY_TYPES": "message,event",
10+
"DIRECTLINE3_ACTIVITY_TEMPLATE": {
11+
"channelData": {
12+
"my-special-channel-data": "..."
13+
}
14+
}
1015
}
1116
}
1217
}

samples/convo/spec/convo/adaptive.convo.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

samples/convo/spec/convo/button.convo.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

samples/convo/spec/convo/card.convo.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/convo/spec/convo/channeldata.convo.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

samples/convo/spec/convo/event.convo.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

samples/convo/spec/convo/form.convo.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)