Skip to content

Commit

Permalink
Merge branch 'hotfix/0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
webus committed Aug 14, 2017
2 parents 6ea31c6 + fb18d61 commit 50f5f9d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
60 changes: 60 additions & 0 deletions fixtures/apiai/two_types_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"id": "6d4a69fc-a313-432c-9d69-0ad72734b86b",
"timestamp": "2017-08-14T16:31:50.134Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "hello test",
"action": "",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "9ee42165-3e65-4df1-ae90-fe02a413f86b",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "hello test"
},
"fulfillment": {
"speech": "Hello from demo",
"messages": [
{
"type": 0,
"platform": "facebook",
"speech": "Hello from demo"
},
{
"type": 1,
"platform": "facebook",
"title": "Hello",
"subtitle": "Hello",
"imageUrl": "http://coffeepedia.ru/wp-content/uploads/2014/02/11.jpg.png",
"buttons": [
{
"text": "One",
"postback": "https://ya.ru"
},
{
"text": "Two",
"postback": "https://google.com"
},
{
"text": "Three",
"postback": "https://youtube.com"
}
]
},
{
"type": 0,
"speech": "Hello from demo"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success"
},
"sessionId": "39636182-666f-4aef-ae39-63668ff4e8f5"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botstack",
"version": "0.0.2",
"version": "0.0.3",
"description": "Bot Stack",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/fb/apiai.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const lodash = require('lodash');
const log = require('../log');
const {
textMessage,
Expand All @@ -8,6 +9,8 @@ const {
} = require('./message_types');
const { reply } = require('./reply');

const useOnlyFacebookResponse = lodash.has(process.env, 'BOTSTACK_ONLY_FB_RESP');

async function processMessagesFromApiAi(apiaiResponse, senderID, dontSend = false) {
const allMessages = [];

Expand All @@ -31,6 +34,11 @@ async function processMessagesFromApiAi(apiaiResponse, senderID, dontSend = fals
message,
messageType: message.type
});
if(useOnlyFacebookResponse) {
if(!(lodash.get(message, 'platform') === 'facebook')) {
continue;
}
}
switch (message.type) {
case 0:
replyMessage = textMessage(message.speech);
Expand Down
57 changes: 57 additions & 0 deletions tests/test_fb_only_fb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const lodash = require('lodash');
const sinon = require('sinon');

const rewiremock = require('rewiremock').default;
const { addPlugin, plugins } = require('rewiremock');

addPlugin(plugins.relative);
addPlugin(plugins.usedByDefault);

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

const assert = chai.assert;

describe('Testing FB only', () => {
let oldProcessEnv = null;

beforeEach(() => {
oldProcessEnv = lodash.cloneDeep(process.env);
process.env.BOTSTACK_ONLY_FB_RESP = true;
});

afterEach(() => {
if (oldProcessEnv) {
process.env = oldProcessEnv;
}
});

it('testing fb only', async () => {
const apiAiTextResponse = require('../fixtures/apiai/two_types_response.json');
const data = [];
const apiai = require('../src/api-ai');
const apiAiResult = apiai.processResponse(apiAiTextResponse, '1234567890');

rewiremock('./reply').callThought().with({
reply: async (message, senderId) => {
data.push(message);
return true;
}
});

rewiremock.enable();
rewiremock.isolation();

const fb = require(rewiremock.resolve('../src/fb'));

const senderID = '1234567890';
const res = await fb.processMessagesFromApiAi(apiAiResult, senderID);

rewiremock.disable();
rewiremock.clear();

assert.equal(data.length, 2);
});
});

0 comments on commit 50f5f9d

Please sign in to comment.