Skip to content

Commit 38ec83a

Browse files
author
treml
committed
adaptive card support
1 parent e771d77 commit 38ec83a

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

index.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const mime = require('mime-types')
2+
const _ = require('lodash')
23
const { DirectLine, ConnectionStatus } = require('botframework-directlinejs')
34
const debug = require('debug')('botium-connector-directline3')
45

@@ -60,13 +61,13 @@ class BotiumConnectorDirectline3 {
6061

6162
const mapButton = (b) => ({
6263
text: b.title,
63-
payload: b.value,
64-
imageUri: b.image
64+
payload: b.value || b.url || b.data,
65+
imageUri: b.image || b.iconUrl
6566
})
6667
const mapImage = (i) => ({
6768
mediaUri: i.url,
6869
mimeType: mime.lookup(i.url) || 'application/unknown',
69-
altText: i.alt
70+
altText: i.alt || i.altText
7071
})
7172
const mapMedia = (m) => ({
7273
mediaUri: m.url,
@@ -78,11 +79,18 @@ class BotiumConnectorDirectline3 {
7879
if (a.contentType === 'application/vnd.microsoft.card.hero') {
7980
botMsg.cards.push({
8081
text: a.content.title,
81-
image: a.content.images && a.content.images.length > 0 && [ mapImage(a.content.images[0]) ],
82+
image: a.content.images && a.content.images.length > 0 && mapImage(a.content.images[0]),
8283
buttons: a.content.buttons && a.content.buttons.map(mapButton)
8384
})
8485
} else if (a.contentType === 'application/vnd.microsoft.card.adaptive') {
85-
// TODO
86+
const textBlocks = this._deepFilter(a.content.body, (t) => t.type, (t) => t.type === 'TextBlock')
87+
const imageBlocks = this._deepFilter(a.content.body, (t) => t.type, (t) => t.type === 'Image')
88+
89+
botMsg.cards.push({
90+
text: textBlocks && textBlocks.map(t => t.text),
91+
image: imageBlocks && imageBlocks.length > 0 && mapImage(imageBlocks[0]),
92+
buttons: a.content.actions && a.content.actions.map(mapButton)
93+
})
8694
} else if (a.contentType === 'application/vnd.microsoft.card.animation' ||
8795
a.contentType === 'application/vnd.microsoft.card.audio' ||
8896
a.contentType === 'application/vnd.microsoft.card.video') {
@@ -175,6 +183,24 @@ class BotiumConnectorDirectline3 {
175183
this.connSubscription = null
176184
}
177185
}
186+
187+
_deepFilter (item, selectFn, filterFn) {
188+
let result = []
189+
if (_.isArray(item)) {
190+
item.filter(selectFn).forEach(subItem => {
191+
result = result.concat(this._deepFilter(subItem, selectFn, filterFn))
192+
})
193+
} else if (selectFn(item)) {
194+
if (filterFn(item)) {
195+
result.push(item)
196+
} else {
197+
Object.getOwnPropertyNames(item).forEach(key => {
198+
result = result.concat(this._deepFilter(item[key], selectFn, filterFn))
199+
})
200+
}
201+
}
202+
return result
203+
}
178204
}
179205

180206
module.exports = {

package-lock.json

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.4",
3+
"version": "0.0.5",
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",
@@ -34,6 +34,7 @@
3434
"dependencies": {
3535
"botframework-directlinejs": "^0.9.17",
3636
"debug": "^3.1.0",
37+
"lodash": "^4.17.11",
3738
"mime-types": "^2.1.20",
3839
"xhr2": "^0.1.4"
3940
}

samples/botiumFluent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ driver.BuildFluent()
99
.WaitBotSays((msg) => console.log(JSON.stringify(msg, null, 2)))
1010
.WaitBotSays((msg) => console.log(JSON.stringify(msg, null, 2)))
1111
.WaitBotSays((msg) => console.log(JSON.stringify(msg, null, 2)))
12+
.WaitBotSays((msg) => console.log(JSON.stringify(msg, null, 2)))
1213
.Stop()
1314
.Clean()
1415
.Exec()

0 commit comments

Comments
 (0)