Skip to content

Commit

Permalink
Merge branch 'release/v1.6.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
patrinhani-ciandt committed Oct 26, 2017
2 parents 99a40ac + a3efb86 commit 1c01c68
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/controllers/abbott/botkit/worker.js
Expand Up @@ -59,12 +59,16 @@ module.exports = function (botkit, config) {
messageData.pipeData = message.pipeData;
}

if (message.custom) {
messageData.custom = message.custom;
}

if (message.text) {
messageData.text = message.text;

msgArgs.push(messageData);
}

msgArgs.push(messageData);

let fnResponse = null;

if ((message.endConversation) && (!msg.expectUserResponse)) {
Expand Down
1 change: 1 addition & 0 deletions lib/core/intent-flow-handler.js
Expand Up @@ -12,6 +12,7 @@ var localeval = require('localeval');
const TextBuilder = require('./utils/textBuilder');
const responseBuilders = {
default: require('./responseBuilders/default'),
abbott: require('./responseBuilders/abbott'),
google: require('./responseBuilders/google'),
slack: require('./responseBuilders/slack'),
gchats: require('./responseBuilders/gchats')
Expand Down
48 changes: 48 additions & 0 deletions lib/core/responseBuilders/abbott.js
@@ -0,0 +1,48 @@
module.exports = class {
get responseFormattersBySource() {
return {
'apiai': (responseMessage) => {
if (responseMessage.response.speech !== undefined) {
return responseMessage.response.speech;
}

return responseMessage.response;
},
'abbott': (responseMessage) => {
if (typeof responseMessage.response === 'string') {
if (responseMessage.response.trim().length > 0) {
responseMessage.response = responseMessage.response.trim();
}

return responseMessage.response;
}

return { custom: responseMessage.response };
}
};
}

constructor(intentFlowHandler) {
this.intentFlowHandler = intentFlowHandler;
}

build(responseMessage) {
var responseConvo = null;
var finalResponse = null;

if (responseMessage instanceof Array) {
responseMessage.forEach((respItem) => {
finalResponse = this.responseFormattersBySource[respItem.source](respItem);
responseConvo = respItem.convo || null;
});
} else {
responseConvo = responseMessage.convo || null;
finalResponse = this.responseFormattersBySource[responseMessage.source](responseMessage);
}

return {
convo: responseConvo,
response: finalResponse
};
}
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@abbott-platform/abbott-framework",
"version": "1.6.6",
"version": "1.6.7",
"description": "Abbott Framework is a framework to bring productivity and abstractions to help you to build awesome chatbots.",
"main": "index.js",
"engines": {
Expand Down
105 changes: 103 additions & 2 deletions test/intent-flow-handler-response.spec.js
Expand Up @@ -30,7 +30,7 @@ describe('IntentFlowHandler - Response', () => {
};

describe('Abbott', () => {
const customPayloadWeatherTemplateFmt = (payloadMapString) => {
const customPayloadWeatherTemplateTextFmt = (payloadMapString) => {
return `{
"messageFormat": "Weather in {{cityName}}, {{country}}: Temperature: {{temperature}}, Wind Speed: {{windSpeed}}, Humidity: {{humidity}}",
"api": {
Expand All @@ -48,6 +48,57 @@ describe('IntentFlowHandler - Response', () => {
}`;
};

const customPayloadWeatherTemplateCustomFmt = (payloadMapString) => {
return `{
"responseAsMessage": true,
"api": {
"response": {
"default": {
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": ""
}
},
{
"basicCard": {
"formattedText": "",
"image": {
"accessibilityText": "Weather image representation"
},
"buttons": []
}
}
],
"suggestions": []
}
},
"map": ${payloadMapString}
}
}
}`;
};

const validCustomResponseSchema = {
title: 'abbott schema custom v1',
type: 'object',
required: [ 'richResponse' ],
properties: {
richResponse: {
type: 'object',
properties: {
items: {
type: 'array'
},
suggestions: {
type: 'array'
}
}
}
}
};

var intentFlowHandler = null;

before(() => {
Expand All @@ -59,7 +110,7 @@ describe('IntentFlowHandler - Response', () => {
describe('#_handleFetchResponse()', () => {

it('should return a simple text response (weather)', () => {
let customPayload = normalizeCustomPayload(customPayloadWeatherTemplateFmt(`{
let customPayload = normalizeCustomPayload(customPayloadWeatherTemplateTextFmt(`{
"name": {
"key": "cityName"
},
Expand Down Expand Up @@ -87,6 +138,56 @@ describe('IntentFlowHandler - Response', () => {
result.resultMessage.should.equal("Weather in New York, US: Temperature: 10 °C, Wind Speed: 2.86 m/s, Humidity: 76 %");
});

it('should return a custom message response (weather)', () => {
let customPayload = normalizeCustomPayload(customPayloadWeatherTemplateCustomFmt(`{
"name": {
"key": "richResponse.items[0].simpleResponse.textToSpeech",
"transform": "'Weather in ' + src.name + ', ' + src.sys.country"
},
"main->temp": {
"key": "richResponse.items[1].basicCard.title",
"transform": "format.compile('{{data, number, integer}} °C')({{ data: (Number(value) - 273.15) }});"
},
"weather[0]->icon": {
"key": "richResponse.items[1].basicCard.image.url",
"transform": "'http://openweathermap.org/img/w/' + value + '.png'"
},
"wind->speed": {
"key": "richResponse.items[1].basicCard.formattedText",
"transform": "textBuilder.add('**Wind:** ', value, ' m/s', ' **Humidity:** ', src.main.humidity, ' %').toString()"
}
}`));

var result = intentFlowHandler._handleFetchResponse(customPayload, dataWeather, null);

result.should.be.jsonSchema(validRootResponseSchema);

result.resultMessage.should.be.jsonSchema(validCustomResponseSchema);

chai.expect(result.resultMessage).to.matchPattern({
richResponse: {
items: [
{
simpleResponse: {
textToSpeech: 'Weather in New York, US'
}
},
{
basicCard: {
title: '10 °C',
formattedText: '**Wind:** 2.86 m/s **Humidity:** 76 %',
image: {
url: 'http://openweathermap.org/img/w/01d.png',
accessibilityText: 'Weather image representation'
},
buttons: []
}
}
],
suggestions: []
}
});
});
});

});
Expand Down

0 comments on commit 1c01c68

Please sign in to comment.