Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/handle unhandled intent #304

Merged
merged 6 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions functions/src/actions/unhandled.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const _ = require('lodash');

const dialog = require('../dialog');
const strings = require('../strings').intents.unhandled;
const {warning} = require('../utils/logger')('ia:actions:unhandled');

function handler (app) {
// TODO: should log unhandled itents to Sentry
warning('Catch unhandled intent');
warning(`we haven't found any valid handler`);
dialog.ask(app, _.sample(strings));
}

/**
* handle Alexa Unhandled intent
* TODO: could cover unhandled for Google Assistant as well
* cover unhandled for Google Assistant as well
*
* @type {{handler: handler}}
*/
module.exports = {
Expand Down
17 changes: 12 additions & 5 deletions functions/src/platform/alexa/handler/handlers-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const util = require('util');

const {App} = require('../app');
const jsonify = require('../../../utils/jsonify');
const {debug} = require('../../../utils/logger')('ia:platform:alexa:handler');
const {debug, warning} = require('../../../utils/logger')('ia:platform:alexa:handler');

const fsm = require('../../../state/fsm');
const kebabToCamel = require('../../../utils/kebab-to-camel');
Expand Down Expand Up @@ -191,14 +191,21 @@ module.exports = (actions) => {
handle: (handlerInput) => {
debug('catch all the rest');

let handlers;
let name;

const res = findHandlersByInput(actions, handlerInput);
if (!res) {
debug(`we haven't found any valid handler`);
return Promise.resolve();
warning(`we haven't found any valid handler`);
handlers = {
default: require('../../../actions/unhandled').handler,
};
name = 'unknown intent';
} else {
handlers = res.handlers;
name = res.name;
}

const {handlers, name} = res;

debug(`begin handle intent "${name}"`);
return fetchAttributes(handlerInput)
.then((persistentAttributes) => {
Expand Down
10 changes: 0 additions & 10 deletions functions/src/platform/alexa/handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ module.exports = (actions) => {
// alexa.dynamoDBTableName = 'InternetArchiveSessions';
}

// FIXME:
// temporal hack because bst doesn't suppor ASK SDK v2 yet
// https://github.com/bespoken/bst/issues/440
if (callback) {
skill.invoke(event, context).then(
res => callback(null, res),
err => callback(err)
);
return;
}
return skill.invoke(event, context);
};
};
19 changes: 14 additions & 5 deletions functions/src/platform/assistant/handler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const {dialogflow} = require('actions-on-google');
const bst = require('bespoken-tools');
const functions = require('firebase-functions');
const _ = require('lodash');
// const dashbotBuilder = require('dashbot');
// FIXME: temporal solution details below
// const domain = require('domain'); // eslint-disable-line
const Raven = require('raven');

const packageJSON = require('../../../package.json');

const {debug, error, warning} = require('./../../utils/logger')('ia:index');
const strings = require('../../strings');
const {debug, error, warning} = require('../../utils/logger')('ia:index');

const buildHandlers = require('./handler/builder');
const logRequest = require('./middlewares/log-request');
Expand Down Expand Up @@ -76,19 +78,26 @@ module.exports = (actionsMap) => {
// }
// });

const getHandlerByName = name => handlers.filter(h => h.intent === name);

app.fallback((conv) => {
const matchedHandlers = handlers.filter(h => h.intent === conv.action);
let matchedHandlers = getHandlerByName(conv.action);
if (matchedHandlers.length > 0) {
debug(`doesn't match intent name but matched manually by action name`);
return matchedHandlers[0].handler(conv);
}

// TODO: move to actions
// warning(`We missed action: "${app.getIntent()}".
warning(`we missed action: "${conv.action}".
Intent: "${conv.intent}"`);

conv.ask(`can you rephrase it?`);
matchedHandlers = getHandlerByName('unhandled');
if (matchedHandlers.length > 0) {
return matchedHandlers[0].handler(conv);
}

warning(`something wrong we don't have unhandled handler`);
// the last chance answer if we haven't found unhandled handler
conv.ask(_.sample(strings.intents.unhandled));
});

app.catch((conv, err) => {
Expand Down
4 changes: 4 additions & 0 deletions functions/src/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ module.exports = {
speech: "I'm sorry I'm having trouble here. Maybe we should try this again later.",
}],

unhandled: [{
speech: "Sorry, I'm afraid I don't follow you.",
}],

version: {
speech: 'Version is <say-as interpret-as="number">{{version}}</say-as>.',
},
Expand Down
2 changes: 1 addition & 1 deletion functions/tests/_utils/mocking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class MockResponse {
* just get plain speech text for matching with expectation
*/
speech () {
return this.body.speech || this.body.payload.google.richResponse.items.map(i => i.simpleResponse.textToSpeech).join('\n');
return this.body.speech || this.body.payload.google.richResponse.items.map(i => i.speech || i.simpleResponse.textToSpeech).join('\n');
}
}

Expand Down
6 changes: 6 additions & 0 deletions functions/tests/integration/alexa/dialog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@
- user: 'play jazz'
assistant:
directive: 'AudioPlayer.Play'

- scenario: 'unhandled intent -> graceful answer'
dialog:
- user:
intend: AMAZON.we_definitely_do_not_handle_it
assistant: "Sorry, I'm afraid I don't follow you."
40 changes: 24 additions & 16 deletions functions/tests/integration/alexa/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const yaml = require('js-yaml');
const path = require('path');
const sinon = require('sinon');
const VirtualAlexa = require('virtual-alexa').VirtualAlexa;
const util = require('util');

const DynamoDBMock = require('../../_utils/mocking/dynamodb');

Expand Down Expand Up @@ -95,22 +96,29 @@ describe('integration', () => {
}

dialog.forEach(({user, assistant}) => {
it(`should utter: "${user}" and get a response: "${JSON.stringify(assistant)}"`, () => {
return alexa
.utter(user)
.then(res => {
if (typeof assistant === 'string') {
expect(res.response.outputSpeech).to.exist;
expect(res.response.outputSpeech.ssml).to.exist;
expect(res.response.outputSpeech.ssml).to.include(assistant);
} else if ('directive' in assistant) {
expect(res.response).to.have.property('directives');
expect(res.response.directives).to.be.an('array');
expect(res.response.directives[0]).to.have.property('type', assistant.directive);
} else {
throw new Error(`doesn't support response:`, assistant);
}
});
it(`should utter: "${util.inspect(user, {depth: null})}" and get a response: "${JSON.stringify(assistant)}"`, () => {
let res;
if (typeof (user) === 'string') {
res = alexa.utter(user);
} else if ('intend' in user) {
res = alexa.intend(user.intend);
} else {
throw new Error(`We don't support user scheme ${util.inspect(user, {depth: null})}`);
}
return res.then(res => {
expect(res).to.have.property('response').to.be.not.undefined;
if (typeof assistant === 'string') {
expect(res.response.outputSpeech).to.exist;
expect(res.response.outputSpeech.ssml).to.exist;
expect(res.response.outputSpeech.ssml).to.include(assistant);
} else if ('directive' in assistant) {
expect(res.response).to.have.property('directives');
expect(res.response.directives).to.be.an('array');
expect(res.response.directives[0]).to.have.property('type', assistant.directive);
} else {
throw new Error(`doesn't support response:`, assistant);
}
});
});
});
});
Expand Down
20 changes: 19 additions & 1 deletion functions/tests/platform/assistant/handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,28 @@ describe('platform', () => {

return wait()
.then(() => {
expect(warning).to.be.calledOnce;
expect(warning).to.be.called;
expect(warning.getCall(0).args[0]).to.includes(action);
});
});

it('should gracefully ask in case of missed action', () => {
let warning = sinon.spy();
handlerBuilder.__set__('warning', warning);

const handler = handlerBuilder();
const action = 'on-definitely-uncovered-action';

handler(buildIntentRequest({
action,
lastSeen: null,
}), res);

return wait()
.then(() => {
expect(res.speech()).to.includes(`Sorry, I'm afraid I don't follow you.`);
});
});
});
});
});