Skip to content

Commit 425e062

Browse files
author
Botium
authored
Merge pull request #30 from codeforequity-at/develop
botium-connector-directline3 0.0.15
2 parents 8047646 + 7393818 commit 425e062

File tree

12 files changed

+93
-78
lines changed

12 files changed

+93
-78
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ To check the configuration, run the emulator (Botium CLI required) to bring up a
7676

7777
Botium setup is ready, you can begin to write your [BotiumScript](https://github.com/codeforequity-at/botium-core/wiki/Botium-Scripting) files.
7878

79+
## Finetuning Directline3 Activity
80+
81+
For finetuning the [Activity object](https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-api-reference?view=azure-bot-service-4.0#activity-object) sent to your bot, you can use the [UPDATE_CUSTOM logic hook](https://botium.atlassian.net/wiki/spaces/BOTIUM/pages/48660497/Integrated+Logic+Hooks). This example will add some custom values to the [channelData](https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-channeldata?view=azure-bot-service-4.0):
82+
83+
#me
84+
do some channel specific thingy ...
85+
UPDATE_CUSTOM SET_ACTIVITY_VALUE|channelData|{"channelData1": "botium", "channelData2": "something else"}
86+
87+
The parameters are:
88+
1. SET_ACTIVITY_VALUE
89+
2. The path to the activity field
90+
3. The value of the activity field
91+
7992
## Supported Capabilities
8093

8194
Set the capability __CONTAINERMODE__ to __directline3__ to activate this connector.

index.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const util = require('util')
21
const uuidv4 = require('uuid/v4')
32
const mime = require('mime-types')
43
const _ = require('lodash')
@@ -37,7 +36,7 @@ const Defaults = {
3736
[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD]: 'name',
3837
[Capabilities.DIRECTLINE3_HANDLE_ACTIVITY_TYPES]: 'message',
3938
[Capabilities.DIRECTLINE3_ACTIVITY_VALUE_MAP]: {
40-
'event': 'name'
39+
event: 'name'
4140
}
4241
}
4342

@@ -50,12 +49,12 @@ class BotiumConnectorDirectline3 {
5049
Validate () {
5150
debug('Validate called')
5251

53-
this.caps = Object.assign({}, Defaults, _.pickBy(this.caps, (value, key) => !Defaults.hasOwnProperty(key) || !_.isString(value) || value !== ''))
52+
this.caps = Object.assign({}, Defaults, _.pickBy(this.caps, (value, key) => !Object.prototype.hasOwnProperty.call(Defaults, key) || !_.isString(value) || value !== ''))
5453

55-
if (!this.caps['DIRECTLINE3_SECRET']) throw new Error('DIRECTLINE3_SECRET capability required')
56-
if (!this.caps['DIRECTLINE3_BUTTON_TYPE']) throw new Error('DIRECTLINE3_BUTTON_TYPE capability required')
57-
if (!this.caps['DIRECTLINE3_BUTTON_VALUE_FIELD']) throw new Error('DIRECTLINE3_BUTTON_VALUE_FIELD capability required')
58-
if (!this.caps['DIRECTLINE3_HANDLE_ACTIVITY_TYPES']) throw new Error('DIRECTLINE3_HANDLE_ACTIVITY_TYPES capability required')
54+
if (!this.caps.DIRECTLINE3_SECRET) throw new Error('DIRECTLINE3_SECRET capability required')
55+
if (!this.caps.DIRECTLINE3_BUTTON_TYPE) throw new Error('DIRECTLINE3_BUTTON_TYPE capability required')
56+
if (!this.caps.DIRECTLINE3_BUTTON_VALUE_FIELD) throw new Error('DIRECTLINE3_BUTTON_VALUE_FIELD capability required')
57+
if (!this.caps.DIRECTLINE3_HANDLE_ACTIVITY_TYPES) throw new Error('DIRECTLINE3_HANDLE_ACTIVITY_TYPES capability required')
5958

6059
return Promise.resolve()
6160
}
@@ -69,20 +68,20 @@ class BotiumConnectorDirectline3 {
6968
debug('Start called')
7069
this._stopSubscription()
7170
this.directLine = new DirectLine({
72-
secret: this.caps['DIRECTLINE3_SECRET'],
73-
webSocket: this.caps['DIRECTLINE3_WEBSOCKET'],
74-
domain: this.caps['DIRECTLINE3_DOMAIN'],
75-
pollingInterval: this.caps['DIRECTLINE3_POLLINGINTERVAL']
71+
secret: this.caps.DIRECTLINE3_SECRET,
72+
webSocket: this.caps.DIRECTLINE3_WEBSOCKET,
73+
domain: this.caps.DIRECTLINE3_DOMAIN,
74+
pollingInterval: this.caps.DIRECTLINE3_POLLINGINTERVAL
7675
})
7776

78-
if (this.caps['DIRECTLINE3_GENERATE_USERNAME']) {
77+
if (this.caps.DIRECTLINE3_GENERATE_USERNAME) {
7978
this.me = uuidv4()
8079
} else {
8180
this.me = 'me'
8281
}
8382

8483
const isValidActivityType = (activityType) => {
85-
const filter = this.caps['DIRECTLINE3_HANDLE_ACTIVITY_TYPES']
84+
const filter = this.caps.DIRECTLINE3_HANDLE_ACTIVITY_TYPES
8685
if (_.isString(filter)) {
8786
return filter.indexOf(activityType) >= 0
8887
} else if (_.isArray(filter)) {
@@ -202,7 +201,7 @@ class BotiumConnectorDirectline3 {
202201
}
203202
}
204203
} else {
205-
const valueMap = this.caps['DIRECTLINE3_ACTIVITY_VALUE_MAP']
204+
const valueMap = this.caps.DIRECTLINE3_ACTIVITY_VALUE_MAP
206205
if (valueMap && valueMap[message.type]) {
207206
botMsg.messageText = message[valueMap[message.type]]
208207
} else {
@@ -243,7 +242,7 @@ class BotiumConnectorDirectline3 {
243242

244243
UserSays (msg) {
245244
debug('UserSays called')
246-
return new Promise(async (resolve, reject) => {
245+
return new Promise(async (resolve, reject) => { // eslint-disable-line no-async-promise-executor
247246
const activity = msg.sourceData || {}
248247
if (msg.buttons && msg.buttons.length > 0 && (msg.buttons[0].text || msg.buttons[0].payload)) {
249248
let payload = msg.buttons[0].payload || msg.buttons[0].text
@@ -272,14 +271,22 @@ class BotiumConnectorDirectline3 {
272271
})
273272
}
274273

274+
if (msg.SET_ACTIVITY_VALUE) {
275+
_.keys(msg.SET_ACTIVITY_VALUE).forEach(key => {
276+
_.set(activity, key, msg.SET_ACTIVITY_VALUE[key])
277+
})
278+
}
279+
275280
if (msg.media && msg.media.length > 0) {
276281
debug('Posting activity with attachments ', JSON.stringify(activity, null, 2))
277282
const formData = new FormData()
278283

279-
formData.append('activity', Buffer.from(JSON.stringify(activity)), {
280-
contentType: 'application/vnd.microsoft.activity',
281-
filename: 'blob'
282-
})
284+
if (activity.text) {
285+
formData.append('activity', Buffer.from(JSON.stringify(activity)), {
286+
contentType: 'application/vnd.microsoft.activity',
287+
filename: 'blob'
288+
})
289+
}
283290

284291
for (let i = 0; i < msg.media.length; i++) {
285292
const attachment = msg.media[i]
@@ -294,30 +301,33 @@ class BotiumConnectorDirectline3 {
294301
const { body } = await fetch(attachment.mediaUri)
295302

296303
formData.append('file', body, {
297-
filename: attachmentName
304+
filename: attachmentName,
305+
contentType: 'image/png'
298306
})
299307
}
300308
}
301309

302-
// Ensure directline is connected!
303310
await this.directLine.checkConnection(true)
304-
fetch(`${this.directLine.domain}/conversations/${this.directLine.conversationId}/upload?userId=${activity.from.id}`, {
311+
const uploadUrl = `${this.directLine.domain}/conversations/${this.directLine.conversationId}/upload?userId=${activity.from.id}`
312+
debug(`Uploading attachments to ${uploadUrl}`)
313+
fetch(uploadUrl, {
305314
method: 'POST',
306315
headers: {
307-
'Authorization': `Bearer ${this.directLine.token}`
316+
Authorization: `Bearer ${this.directLine.token}`,
317+
'Content-Type': 'multipart/form-data'
308318
},
309319
body: formData
310-
}).catch(err => {
311-
debug('Error posting activity with attachments', err)
312-
reject(new Error(`Error posting activity: ${err}`))
313320
}).then(async (res) => {
314321
const json = await res.json()
315-
if (json.id) {
322+
if (json && json.id) {
316323
debug('Posted activity with attachments, assigned ID:', json.id)
317324
resolve()
318325
} else {
319-
reject(new Error(`Error posting activity with attachments: ${util.inspect(json)}`))
326+
reject(new Error('Error posting activity with attachments, no activity id returned'))
320327
}
328+
}).catch(err => {
329+
debug('Error posting activity with attachments', err)
330+
reject(new Error(`Error posting activity: ${err.message}`))
321331
})
322332
} else {
323333
debug('Posting activity ', JSON.stringify(activity, null, 2))
@@ -359,6 +369,11 @@ class BotiumConnectorDirectline3 {
359369
this.connSubscription.unsubscribe()
360370
this.connSubscription = null
361371
}
372+
if (this.directLine) {
373+
debug('ending directline connection')
374+
this.directLine.end()
375+
this.directLine = null
376+
}
362377
}
363378

364379
_deepFilter (item, selectFn, filterFn) {

package.json

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "botium-connector-directline3",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
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",
77
"scripts": {
88
"build": "npm run eslint && rollup -c",
9-
"eslint": "eslint index.js"
9+
"eslint": "eslint index.js",
10+
"eslint-fix": "eslint --fix index.js"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -19,31 +20,31 @@
1920
},
2021
"homepage": "https://github.com/codeforequity-at/botium-connector-directline3#readme",
2122
"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",
26-
"eslint": "^5.12.0",
27-
"eslint-config-standard": "^12.0.0",
28-
"eslint-plugin-import": "^2.14.0",
29-
"eslint-plugin-node": "^8.0.1",
30-
"eslint-plugin-promise": "^4.0.1",
31-
"eslint-plugin-standard": "^4.0.0",
32-
"rollup": "^0.58.2",
33-
"rollup-plugin-babel": "^4.3.2",
34-
"rollup-plugin-commonjs": "^9.1.3",
35-
"rollup-plugin-json": "^2.3.0",
36-
"rollup-plugin-node-resolve": "^3.3.0"
23+
"@babel/core": "^7.6.0",
24+
"@babel/node": "^7.6.1",
25+
"@babel/plugin-transform-runtime": "^7.6.0",
26+
"@babel/preset-env": "^7.6.0",
27+
"eslint": "^6.3.0",
28+
"eslint-config-standard": "^14.1.0",
29+
"eslint-plugin-import": "^2.18.2",
30+
"eslint-plugin-node": "^10.0.0",
31+
"eslint-plugin-promise": "^4.2.1",
32+
"eslint-plugin-standard": "^4.0.1",
33+
"rollup": "^1.21.2",
34+
"rollup-plugin-babel": "^4.3.3",
35+
"rollup-plugin-commonjs": "^10.1.0",
36+
"rollup-plugin-json": "^4.0.0",
37+
"rollup-plugin-node-resolve": "^5.2.0"
3738
},
3839
"dependencies": {
39-
"@babel/runtime": "^7.2.0",
40-
"botframework-directlinejs": "^0.9.17",
41-
"debug": "^3.1.0",
42-
"form-data": "^2.3.3",
43-
"lodash": "^4.17.11",
44-
"mime-types": "^2.1.20",
45-
"node-fetch": "^2.3.0",
46-
"uuid": "^3.3.2",
47-
"xhr2": "^0.1.4"
40+
"@babel/runtime": "^7.6.0",
41+
"botframework-directlinejs": "^0.11.4",
42+
"debug": "^4.1.1",
43+
"form-data": "^2.5.1",
44+
"lodash": "^4.17.15",
45+
"mime-types": "^2.1.24",
46+
"node-fetch": "^2.6.0",
47+
"uuid": "^3.3.3",
48+
"xhr2": "^0.2.0"
4849
}
4950
}

samples/convo/botium.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"PROJECTNAME": "Directline3 Plugin Sample",
55
"CONTAINERMODE": "directline3",
66
"DIRECTLINE3_SECRET": "my-directline-secret",
7-
"DIRECTLINE3_WEBSOCKET": true,
8-
"DIRECTLINE3_POLLINGINTERVAL": 1000,
97
"DIRECTLINE3_BUTTON_TYPE": "message",
108
"DIRECTLINE3_BUTTON_VALUE_FIELD": "text",
119
"DIRECTLINE3_HANDLE_ACTIVITY_TYPES": "message,event"

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ adaptive
33
#me
44
BUTTON card bingsports
55

6-
#bot
7-
Welcome to Mockbot v4!
8-
96
#bot
107
BUTTONS Image action|Column action|Container action

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ button
33
#me
44
BUTTON card markdown
55

6-
#bot
7-
Welcome to Mockbot v4!
8-
96
#bot
107
Showing markdown

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ card
33
#me
44
BUTTON thumbnailcard
55

6-
#bot
7-
Welcome to Mockbot v4!
8-
96
#bot
107
Microsoft Surface Pro
118
MEDIA surface1.jpg
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
channeldata
2+
3+
#me
4+
channel-data
5+
UPDATE_CUSTOM SET_ACTIVITY_VALUE|channelData|{"channelData1": "botium", "channelData2": "something else"}
6+
7+
#bot
8+
Dump of the channel data from the activity sent by the user.
9+
JSON_PATH $.attachments

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ event
33
#me
44
sample:backchannel
55

6-
#bot
7-
Welcome to Mockbot v4!
8-
96
#bot
107
I am sending an `event` activity like below
118

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ form
33
#me
44
BUTTON card inputs
55

6-
#bot
7-
Welcome to Mockbot v4!
8-
96
#bot
107
BUTTONS Open Url|Submit|Show Card
118

0 commit comments

Comments
 (0)