Skip to content

Commit

Permalink
Merge pull request #8 from omenocal/master
Browse files Browse the repository at this point in the history
Fixed duplication sending issue when triggering a tell response
  • Loading branch information
omenocal authored Jun 28, 2018
2 parents 5899fba + 3f8325d commit 4b7fb88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
17 changes: 10 additions & 7 deletions lib/Voxa-Dashbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const _ = require('lodash');
const DashbotAnalytics = require('dashbot');
const debug = require('debug')('voxa:dashbot');
const lambdaLog = require('lambda-log');

const defaultConfig = {
ignoreUsers: [],
Expand All @@ -21,32 +21,35 @@ function register(skill, config) {
const Dashbot = DashbotAnalytics(pluginConfig.api_key, dashbotConfig).alexa;

skill.onBeforeReplySent(track);
skill.onSessionEnded(track);
skill.onSessionEnded((request, reply, transition) => {
track(request, reply, transition, true);
});

function track(request, reply) {
function track(request, reply, transition, isSessionEndedRequest) {
if (_.includes(pluginConfig.ignoreUsers, request.user.userId)) return Promise.resolve(null);
if (pluginConfig.suppressSending) return Promise.resolve(null);
if (isSessionEndedRequest && request.request.type !== 'SessionEndedRequest') return Promise.resolve(null);

debug('Sending to dashbot');
lambdaLog.info('Sending to dashbot');

const newRequestObject = _.pick(request, ['version', 'session', 'context', 'request']);

// PROCESSING INCOMING RESPONSE
return Dashbot.logIncoming(newRequestObject)
.then(response => response.text())
.then((response) => {
console.log('Dashbot', 'logIncoming response', response);
lambdaLog.info('logIncoming response', { response });

// PROCESSING OUTGOING RESPONSE
return Dashbot.logOutgoing(newRequestObject, reply.toJSON());
})
.then(response => response.text())
.then((response) => {
console.log('Dashbot', 'logOutgoing response', response);
lambdaLog.info('logOutgoing response', { response });
return response;
})
.catch((err) => {
console.log('voxa-dashbot', 'err', err);
lambdaLog.error(err);
});
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "voxa-dashbot",
"version": "0.1.7",
"version": "0.1.8",
"description": "Integrate Dashbot analytics into your Alexa apps using the voxa framework",
"main": "index.js",
"scripts": {
"test": "istanbul cover _mocha -- --recursive tests",
"test-ci": "JUNIT_REPORT_PATH=junit.xml istanbul cover _mocha -- --recursive --colors --reporter mocha-jenkins-reporter tests",
"cobertura": "istanbul report cobertura --root coverage",
"lint": "eslint --fix lib/ tests/",
"lint-fix": "eslint lib/ tests/"
"lint": "eslint lib/ tests/",
"lint-fix": "eslint --fix lib/ tests/"
},
"repository": {
"type": "git",
Expand All @@ -31,8 +31,8 @@
},
"license": "MIT",
"dependencies": {
"dashbot": "^9.8.0",
"debug": "^2.6.9",
"dashbot": "^9.9.1",
"lambda-log": "^1.4.0",
"lodash": "^4.17.10"
},
"devDependencies": {
Expand Down
35 changes: 33 additions & 2 deletions tests/voxa-dashbot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const voxaDashbot = require('../lib/Voxa-Dashbot');
const views = require('./views');
const version = require('../package').dependencies.dashbot.replace('^', '');

console.log('DASHBOT VERSION', version);

const expect = chai.expect;
const DASHBOT_URL = 'https://tracker.dashbot.io';
const dashbotConfig = {
Expand Down Expand Up @@ -100,6 +98,39 @@ describe('Voxa-Dashbot plugin', () => {
});
});

it('should register DashbotAnalytics on StopIntent and end the session', () => {
const spy = simple.spy(() => ({ reply: 'ExitIntent.GeneralExit' }));
voxaStateMachine.onIntent('SomeIntent', spy);

const event = {
request: {
type: 'IntentRequest',
intent: {
name: 'SomeIntent',
},
},
session: {
new: false,
application: {
applicationId: 'appId',
},
user: {
userId: 'user-id',
},
},
};

voxaDashbot(voxaStateMachine, dashbotConfig);
return voxaStateMachine.execute(event)
.then((reply) => {
expect(spy.called).to.be.true;
expect(reply.session.new).to.equal(false);
expect(reply.session.attributes.state).to.equal('die');
expect(reply.msg.statements).to.have.lengthOf(1);
expect(reply.msg.statements[0]).to.equal('Ok. Goodbye.');
});
});

it('should register DashbotAnalytics on SessionEndedRequest', () => {
const spy = simple.spy(() => ({ reply: 'ExitIntent.GeneralExit' }));
voxaStateMachine.onSessionEnded(spy);
Expand Down

0 comments on commit 4b7fb88

Please sign in to comment.