Skip to content

Commit

Permalink
Merge pull request #673 from codeforequity-at/develop
Browse files Browse the repository at this point in the history
Botium Core 1.12.3
  • Loading branch information
Botium authored Feb 15, 2022
2 parents 2bfa4a4 + 88ec9ae commit 8735dde
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 190 deletions.
466 changes: 319 additions & 147 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botium-core",
"version": "1.12.2",
"version": "1.12.3",
"description": "The Selenium for Chatbots",
"main": "index.js",
"module": "dist/botium-es.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://www.botium.ai",
"dependencies": {
"@babel/runtime": "^7.16.7",
"@babel/runtime": "^7.17.2",
"async": "^3.2.3",
"body-parser": "^1.19.1",
"boolean": "^3.1.4",
Expand All @@ -42,7 +42,7 @@
"esprima": "^4.0.1",
"express": "^4.17.2",
"globby": "11.0.4",
"ioredis": "^4.28.3",
"ioredis": "^4.28.5",
"is-class": "^0.0.9",
"is-json": "^2.0.1",
"jsonpath": "^1.1.1",
Expand All @@ -65,21 +65,21 @@
"swagger-jsdoc": "^6.1.0",
"swagger-ui-express": "^4.3.0",
"uuid": "^8.3.2",
"vm2": "^3.9.5",
"vm2": "^3.9.7",
"write-yaml": "^1.0.0",
"xlsx": "^0.17.5",
"xlsx": "^0.18.2",
"xregexp": "^5.1.0",
"yaml": "^1.10.2"
},
"devDependencies": {
"@babel/core": "^7.16.12",
"@babel/core": "^7.17.2",
"@babel/node": "^7.16.8",
"@babel/plugin-transform-runtime": "^7.16.10",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.16.11",
"chai": "^4.3.4",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"cross-env": "^7.0.3",
"eslint": "^8.7.0",
"eslint": "^8.9.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-node": "^11.1.0",
Expand All @@ -88,10 +88,10 @@
"license-checker": "^25.0.1",
"license-compatibility-checker": "^0.3.5",
"mocha": "^9.2.0",
"nock": "^13.2.2",
"npm-check-updates": "^12.2.1",
"nock": "^13.2.4",
"npm-check-updates": "^12.3.0",
"nyc": "^15.1.0",
"rollup": "^2.66.0",
"rollup": "^2.67.2",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/BotDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ module.exports = class BotDriver {

Build () {
debug(`Build - Botium Core Version: ${version}`)
debug(`Build - Capabilites: ${util.inspect(this.caps)}`)
debug(`Build - Sources : ${util.inspect(this.sources)}`)
debug(`Build - Envs : ${util.inspect(this.envs)}`)
debug(`Build - Capabilites: ${JSON.stringify(_.pickBy(this.caps, (value, key) => Defaults.Capabilities[key] !== value), null, 2)}`)
debug(`Build - Sources: ${JSON.stringify(_.pickBy(this.sources, (value, key) => Defaults.Sources[key] !== value), null, 2)}`)
debug(`Build - Envs: ${JSON.stringify(_.pickBy(this.envs, (value, key) => Defaults.Envs[key] !== value), null, 2)}`)
this.eventEmitter.emit(Events.CONTAINER_BUILDING)

return new Promise((resolve, reject) => {
Expand Down
5 changes: 5 additions & 0 deletions src/Capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ module.exports = {
SIMPLEREST_RESPONSE_HOOK: 'SIMPLEREST_RESPONSE_HOOK',
SIMPLEREST_MEDIA_JSONPATH: 'SIMPLEREST_MEDIA_JSONPATH',
SIMPLEREST_BUTTONS_JSONPATH: 'SIMPLEREST_BUTTONS_JSONPATH',
SIMPLEREST_CARDS_JSONPATH: 'SIMPLEREST_CARDS_JSONPATH',
SIMPLEREST_CARD_TEXT_JSONPATH: 'SIMPLEREST_CARD_TEXT_JSONPATH',
SIMPLEREST_CARD_SUBTEXT_JSONPATH: 'SIMPLEREST_CARD_SUBTEXT_JSONPATH',
SIMPLEREST_CARD_BUTTONS_JSONPATH: 'SIMPLEREST_CARD_BUTTONS_JSONPATH',
SIMPLEREST_CARD_ATTACHMENTS_JSONPATH: 'SIMPLEREST_CARD_ATTACHMENTS_JSONPATH',
SIMPLEREST_CONTEXT_JSONPATH: 'SIMPLEREST_CONTEXT_JSONPATH',
SIMPLEREST_CONTEXT_MERGE_OR_REPLACE: 'SIMPLEREST_CONTEXT_MERGE_OR_REPLACE',
SIMPLEREST_CONVERSATION_ID_TEMPLATE: 'SIMPLEREST_CONVERSATION_ID_TEMPLATE',
Expand Down
1 change: 0 additions & 1 deletion src/Defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = {
[Capabilities.SIMPLEREST_METHOD]: 'GET',
[Capabilities.SIMPLEREST_IGNORE_EMPTY]: true,
[Capabilities.SIMPLEREST_TIMEOUT]: 10000,
[Capabilities.SIMPLEREST_EXTRA_OPTIONS]: {},
[Capabilities.SIMPLEREST_STRICT_SSL]: true,
[Capabilities.SIMPLEREST_INBOUND_UPDATE_CONTEXT]: true,
[Capabilities.SIMPLEREST_CONTEXT_MERGE_OR_REPLACE]: 'MERGE',
Expand Down
102 changes: 75 additions & 27 deletions src/containers/plugins/SimpleRestContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,34 +309,82 @@ module.exports = class SimpleRestContainer {
}

for (const jsonPathRoot of jsonPathRoots) {
const media = []
const buttons = []

const jsonPathsMedia = getAllCapValues(Capabilities.SIMPLEREST_MEDIA_JSONPATH, this.caps)
jsonPathsMedia.forEach(jsonPath => {
const responseMedia = jp.query(jsonPathRoot, jsonPath)
if (responseMedia) {
(_.isArray(responseMedia) ? _.flattenDeep(responseMedia) : [responseMedia]).forEach(m =>
media.push({
mediaUri: m,
mimeType: mime.lookup(m) || 'application/unknown'
})
)
debug(`found response media: ${util.inspect(media)}`)
const _retrieveMedia = (jsonPathMediaRoot, jsonPathsMedia) => {
const retrievedMedia = []
jsonPathsMedia.forEach(jsonPath => {
const responseMedia = jp.query(jsonPathMediaRoot, jsonPath)
if (responseMedia) {
(_.isArray(responseMedia) ? _.flattenDeep(responseMedia) : [responseMedia]).forEach(m =>
retrievedMedia.push({
mediaUri: m,
mimeType: mime.lookup(m) || 'application/unknown'
})
)
}
})
return retrievedMedia
}

const _retrieveButtons = (jsonPathButtonRoot, jsonPathsButtons) => {
const retrievedButtons = []
jsonPathsButtons.forEach(jsonPath => {
const responseButtons = jp.query(jsonPathButtonRoot, jsonPath)
if (responseButtons) {
(_.isArray(responseButtons) ? _.flattenDeep(responseButtons) : [responseButtons]).forEach(b =>
retrievedButtons.push({
text: b
})
)
}
})
return retrievedButtons
}

const _getCardText = (responseCardText) => {
if (responseCardText) {
const texts = _.isArray(responseCardText) ? _.flattenDeep(responseCardText) : [responseCardText]
if (texts.length > 1) {
debug(`more than one text found for card: ${util.inspect(texts)}`)
}
if (texts.length > 0) {
return texts[0]
}
}
})
const jsonPathsButtons = getAllCapValues(Capabilities.SIMPLEREST_BUTTONS_JSONPATH, this.caps)
jsonPathsButtons.forEach(jsonPath => {
const responseButtons = jp.query(jsonPathRoot, jsonPath)
if (responseButtons) {
(_.isArray(responseButtons) ? _.flattenDeep(responseButtons) : [responseButtons]).forEach(b =>
buttons.push({
text: b
}

const media = _retrieveMedia(jsonPathRoot, getAllCapValues(Capabilities.SIMPLEREST_MEDIA_JSONPATH, this.caps))
debug(`found response media: ${util.inspect(media)}`)
const buttons = _retrieveButtons(jsonPathRoot, getAllCapValues(Capabilities.SIMPLEREST_BUTTONS_JSONPATH, this.caps))
debug(`found response buttons: ${util.inspect(buttons)}`)
const cards = []

const jsonPathsCards = getAllCapValues(Capabilities.SIMPLEREST_CARDS_JSONPATH, this.caps)
jsonPathsCards.forEach(jsonPath => {
const responseCards = jp.query(jsonPathRoot, jsonPath)
if (responseCards) {
(_.isArray(responseCards) ? _.flattenDeep(responseCards) : [responseCards]).forEach(c => {
const card = {}

const jsonPathsCardText = getAllCapValues(Capabilities.SIMPLEREST_CARD_TEXT_JSONPATH, this.caps)
jsonPathsCardText.forEach(jsonPath => {
card.text = _getCardText(jp.query(c, jsonPath))
})

const jsonPathsCardSubText = getAllCapValues(Capabilities.SIMPLEREST_CARD_SUBTEXT_JSONPATH, this.caps)
jsonPathsCardSubText.forEach(jsonPath => {
card.subtext = _getCardText(jp.query(c, jsonPath))
})
)
debug(`found response buttons: ${util.inspect(buttons)}`)

card.buttons = _retrieveButtons(c, getAllCapValues(Capabilities.SIMPLEREST_CARD_BUTTONS_JSONPATH, this.caps))
card.media = _retrieveMedia(c, getAllCapValues(Capabilities.SIMPLEREST_CARD_ATTACHMENTS_JSONPATH, this.caps))

if (_.keys(card).length > 0) {
cards.push(card)
}
})
}
})
debug(`found response cards: ${util.inspect(cards)}`)

let hasMessageText = false
const jsonPathsTexts = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, this.caps)
Expand All @@ -351,18 +399,18 @@ module.exports = class SimpleRestContainer {
if (!messageText) continue

hasMessageText = true
const botMsg = { sourceData: body, messageText, media, buttons }
const botMsg = { sourceData: body, messageText, media, buttons, cards }
await executeHook(this.caps, this.responseHook, Object.assign({ botMsg, botMsgRoot: jsonPathRoot, messageTextIndex }, this.view))
result.push(botMsg)
}
}

if (!hasMessageText) {
const botMsg = { messageText: '', sourceData: body, media, buttons }
const botMsg = { messageText: '', sourceData: body, media, buttons, cards }
const beforeHookKeys = Object.keys(botMsg)
await executeHook(this.caps, this.responseHook, Object.assign({ botMsg, botMsgRoot: jsonPathRoot }, this.view))
const afterHookKeys = Object.keys(botMsg)
if (beforeHookKeys.length !== afterHookKeys.length || !!(botMsg.messageText && botMsg.messageText.length > 0) || botMsg.media.length > 0 || botMsg.buttons.length > 0 || !this.caps[Capabilities.SIMPLEREST_IGNORE_EMPTY]) {
if (beforeHookKeys.length !== afterHookKeys.length || !!(botMsg.messageText && botMsg.messageText.length > 0) || botMsg.media.length > 0 || botMsg.buttons.length > 0 || botMsg.cards.length > 0 || !this.caps[Capabilities.SIMPLEREST_IGNORE_EMPTY]) {
result.push(botMsg)
}
}
Expand Down
64 changes: 64 additions & 0 deletions test/connectors/simplerest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,70 @@ describe('connectors.simplerest.processBody', function () {
assert.equal(msgs[2].messageText, 'text 3')
assert.equal(msgs[2].media[0].mediaUri, 'http://botium.at/3.jpg')

await container.Clean()
})
it('should process card responses', async function () {
const myCaps = Object.assign({}, myCapsGet, {
[Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
[Capabilities.SIMPLEREST_CARDS_JSONPATH]: '$.cards',
[Capabilities.SIMPLEREST_CARD_TEXT_JSONPATH]: '$.title',
[Capabilities.SIMPLEREST_CARD_SUBTEXT_JSONPATH]: '$.subTitle',
[Capabilities.SIMPLEREST_CARD_ATTACHMENTS_JSONPATH]: '$.media',
[Capabilities.SIMPLEREST_CARD_BUTTONS_JSONPATH]: '$.buttons[*].text'
})
const driver = new BotDriver(myCaps)
const container = await driver.Build()
assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')

await container.Start()
const msgs = await container.pluginInstance._processBodyAsyncImpl({
cards: [
{
title: 'card1',
subTitle: 'card1 sub',
media: 'http://botium.at/1.jpg',
buttons: [
{
text: 'c1b1'
},
{
text: 'c1b2'
}
]
},
{
title: 'card2',
subTitle: 'card2 sub',
media: 'http://botium.at/2.jpg',
buttons: [
{
text: 'c2b1'
}
]
}
]
}, true)

assert.exists(msgs)
assert.equal(msgs.length, 1)
assert.equal(msgs[0].cards.length, 2)
assert.equal(msgs[0].cards[0].text, 'card1')
assert.equal(msgs[0].cards[0].subtext, 'card1 sub')
assert.equal(msgs[0].cards[0].media.length, 1)
assert.equal(msgs[0].cards[0].media[0].mediaUri, 'http://botium.at/1.jpg')
assert.equal(msgs[0].cards[0].media[0].mimeType, 'image/jpeg')
assert.equal(msgs[0].cards[0].buttons.length, 2)
assert.equal(msgs[0].cards[0].buttons[0].text, 'c1b1')
assert.equal(msgs[0].cards[0].buttons[1].text, 'c1b2')

assert.equal(msgs[0].cards[1].text, 'card2')
assert.equal(msgs[0].cards[1].subtext, 'card2 sub')
assert.equal(msgs[0].cards[1].media.length, 1)
assert.equal(msgs[0].cards[1].media[0].mediaUri, 'http://botium.at/2.jpg')
assert.equal(msgs[0].cards[1].media[0].mimeType, 'image/jpeg')
assert.equal(msgs[0].cards[1].buttons.length, 1)
assert.equal(msgs[0].cards[1].buttons[0].text, 'c2b1')

await container.Clean()
})
})
Expand Down

0 comments on commit 8735dde

Please sign in to comment.