Skip to content

Commit ebaad56

Browse files
author
Botium
authored
Merge pull request #6 from codeforequity-at/feature/BOT-429-directline-connector-button-valu
Feature/BOT-429 directline connector button valu
2 parents 509dacc + 7f838f6 commit ebaad56

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

index.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const uuidv4 = require('uuid/v4')
12
const mime = require('mime-types')
23
const _ = require('lodash')
34
const { DirectLine, ConnectionStatus } = require('botframework-directlinejs')
@@ -9,13 +10,15 @@ const Capabilities = {
910
DIRECTLINE3_SECRET: 'DIRECTLINE3_SECRET',
1011
DIRECTLINE3_WEBSOCKET: 'DIRECTLINE3_WEBSOCKET',
1112
DIRECTLINE3_POLLINGINTERVAL: 'DIRECTLINE3_POLLINGINTERVAL',
13+
DIRECTLINE3_GENERATE_USERNAME: 'DIRECTLINE3_GENERATE_USERNAME',
1214
DIRECTLINE3_BUTTON_TYPE: 'DIRECTLINE3_BUTTON_TYPE',
1315
DIRECTLINE3_BUTTON_VALUE_FIELD: 'DIRECTLINE3_BUTTON_VALUE_FIELD'
1416
}
1517

1618
const Defaults = {
1719
[Capabilities.DIRECTLINE3_WEBSOCKET]: true,
1820
[Capabilities.DIRECTLINE3_POLLINGINTERVAL]: 1000,
21+
[Capabilities.DIRECTLINE3_GENERATE_USERNAME]: false,
1922
[Capabilities.DIRECTLINE3_BUTTON_TYPE]: 'event',
2023
[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD]: 'name'
2124
}
@@ -51,9 +54,15 @@ class BotiumConnectorDirectline3 {
5154
pollingInterval: this.caps['DIRECTLINE3_POLLINGINTERVAL']
5255
})
5356

57+
if (this.caps['DIRECTLINE3_GENERATE_USERNAME']) {
58+
this.me = uuidv4()
59+
} else {
60+
this.me = 'me'
61+
}
62+
5463
this.receivedMessageIds = {}
5564
this.subscription = this.directLine.activity$
56-
.filter(activity => activity.type === 'message' && activity.from.id !== 'me')
65+
.filter(activity => activity.type === 'message' && activity.from.id !== this.me)
5766
.subscribe(
5867
message => {
5968
if (this.receivedMessageIds[message.id]) {
@@ -84,8 +93,11 @@ class BotiumConnectorDirectline3 {
8493
if (a.contentType === 'application/vnd.microsoft.card.hero') {
8594
botMsg.cards.push({
8695
text: a.content.title || a.content.text,
96+
subtext: a.content.subtitle,
97+
content: a.content.text,
8798
image: a.content.images && a.content.images.length > 0 && mapImage(a.content.images[0]),
88-
buttons: a.content.buttons && a.content.buttons.map(mapButton)
99+
buttons: a.content.buttons && a.content.buttons.map(mapButton),
100+
media: a.content.images && a.content.images.map(mapImage)
89101
})
90102
} else if (a.contentType === 'application/vnd.microsoft.card.adaptive') {
91103
const textBlocks = this._deepFilter(a.content.body, (t) => t.type, (t) => t.type === 'TextBlock')
@@ -99,11 +111,23 @@ class BotiumConnectorDirectline3 {
99111
} else if (a.contentType === 'application/vnd.microsoft.card.animation' ||
100112
a.contentType === 'application/vnd.microsoft.card.audio' ||
101113
a.contentType === 'application/vnd.microsoft.card.video') {
102-
botMsg.media = botMsg.media.concat((a.content.media && a.content.media.map(mapMedia)) || [])
103-
botMsg.buttons = botMsg.buttons.concat((a.content.buttons && a.content.buttons.map(mapButton)) || [])
114+
botMsg.cards.push({
115+
text: a.content.title || a.content.text,
116+
subtext: a.content.subtitle,
117+
content: a.content.text,
118+
image: a.content.image && mapImage(a.content.image),
119+
buttons: a.content.buttons && a.content.buttons.map(mapButton),
120+
media: a.content.media && a.content.media.map(mapMedia)
121+
})
104122
} else if (a.contentType === 'application/vnd.microsoft.card.thumbnail') {
105-
botMsg.media = botMsg.media.concat((a.content.images && a.content.images.map(mapImage)) || [])
106-
botMsg.buttons = botMsg.buttons.concat((a.content.buttons && a.content.buttons.map(mapButton)) || [])
123+
botMsg.cards.push({
124+
text: a.content.title || a.content.text,
125+
subtext: a.content.subtitle,
126+
content: a.content.text,
127+
image: a.content.images && a.content.images.length > 0 && mapImage(a.content.images[0]),
128+
buttons: a.content.buttons && a.content.buttons.map(mapButton),
129+
media: a.content.images && a.content.images.map(mapImage)
130+
})
107131
} else if (a.contentType && a.contentUrl) {
108132
botMsg.media.push({
109133
mediaUri: a.contentUrl,
@@ -167,11 +191,16 @@ class BotiumConnectorDirectline3 {
167191
debug('UserSays called')
168192
return new Promise((resolve, reject) => {
169193
const activity = {
170-
from: { id: msg.sender }
194+
from: { id: this.me }
171195
}
172-
if (msg.buttons && msg.buttons.length > 0 && msg.buttons[0].text) {
196+
if (msg.buttons && msg.buttons.length > 0 && (msg.buttons[0].text || msg.buttons[0].payload)) {
197+
let payload = msg.buttons[0].payload || msg.buttons[0].text
198+
try {
199+
payload = JSON.parse(payload)
200+
} catch (err) {
201+
}
173202
activity.type = this.caps[Capabilities.DIRECTLINE3_BUTTON_TYPE]
174-
activity[this.caps[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD]] = msg.buttons[0].text
203+
activity[this.caps[Capabilities.DIRECTLINE3_BUTTON_VALUE_FIELD]] = payload
175204
} else {
176205
activity.type = 'message'
177206
activity.text = msg.messageText
@@ -188,7 +217,7 @@ class BotiumConnectorDirectline3 {
188217
},
189218
err => {
190219
debug('Error posting activity', err)
191-
reject(err)
220+
reject(new Error(`Error posting activity: ${err}`))
192221
}
193222
)
194223
})

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "botium-connector-directline3",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
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",
@@ -36,6 +36,7 @@
3636
"debug": "^3.1.0",
3737
"lodash": "^4.17.11",
3838
"mime-types": "^2.1.20",
39+
"uuid": "^3.3.2",
3940
"xhr2": "^0.1.4"
4041
}
4142
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
button
22

33
#me
4-
help
4+
BUTTON card markdown
55

66
#bot
77
Welcome to Mockbot v4!
88

9-
#bot
10-
BUTTONS Show Markdown using Adaptive Card
11-
12-
#me
13-
BUTTON card markdown
14-
159
#bot
1610
Showing markdown
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
card
2+
3+
#me
4+
BUTTON thumbnailcard
5+
6+
#bot
7+
Welcome to Mockbot v4!
8+
9+
#bot
10+
Microsoft Surface Pro
11+
MEDIA surface1.jpg

samples/convo/spec/convo/help.convo.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,3 @@ Welcome to Mockbot v4!
88

99
#bot
1010
BUTTONS Show Markdown using Adaptive Card
11-
12-
#me
13-
BUTTON card markdown
14-
15-
#bot
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
jsonbutton
2+
3+
#me
4+
BUTTON {"hello": "World!"}
5+
6+
#bot
7+
You posted

0 commit comments

Comments
 (0)