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

Fix Alexa Launch and SessionEnded Requests #5377

Merged
merged 3 commits into from
Jan 1, 2020
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
38 changes: 23 additions & 15 deletions lib/api/alexa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,29 @@ function configure (app, wares, ctx, env) {
}

switch (req.body.request.type) {
case 'IntentRequest':
onIntent(req.body.request.intent, function (title, response) {
res.json(ctx.alexa.buildSpeechletResponse(title, response, '', 'true'));
case 'SessionEndedRequest':
onSessionEnded(function () {
res.json('');
next( );
});
break;
case 'LaunchRequest':
onLaunch(req.body.request.intent, function (title, response) {
res.json(ctx.alexa.buildSpeechletResponse(title, response, '', 'true'));
next( );
});
break;
case 'SessionEndedRequest':
onSessionEnded(req.body.request.intent, function (alexaResponse) {
res.json(alexaResponse);
if (!req.body.request.intent) {
onLaunch(function () {
res.json(ctx.alexa.buildSpeechletResponse(
translate('virtAsstTitleLaunch'),
translate('virtAsstLaunch'),
translate('virtAsstLaunch'),
false
));
next( );
});
break;
}
// if intent is set then fallback to IntentRequest
case 'IntentRequest': // eslint-disable-line no-fallthrough
onIntent(req.body.request.intent, function (title, response) {
res.json(ctx.alexa.buildSpeechletResponse(title, response, '', true));
next( );
});
break;
Expand Down Expand Up @@ -120,10 +128,9 @@ function configure (app, wares, ctx, env) {
});


function onLaunch(intent, next) {
function onLaunch(next) {
console.log('Session launched');
console.log(JSON.stringify(intent));
handleIntent(intent.name, intent.slots, next);
next( );
}

function onIntent(intent, next) {
Expand All @@ -132,8 +139,9 @@ function configure (app, wares, ctx, env) {
handleIntent(intent.name, intent.slots, next);
}

function onSessionEnded() {
function onSessionEnded(next) {
console.log('Session ended');
next( );
}

function handleIntent(intentName, slots, next) {
Expand Down
52 changes: 52 additions & 0 deletions lib/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -13329,6 +13329,32 @@ function init() {
, zh_cn: 'Current IOB'
, zh_tw: 'Current IOB'
},
'virtAsstTitleLaunch': {
bg: 'Welcome to Nightscout'
, cs: 'Welcome to Nightscout'
, de: 'Welcome to Nightscout'
, dk: 'Welcome to Nightscout'
, el: 'Welcome to Nightscout'
, en: 'Welcome to Nightscout'
, es: 'Welcome to Nightscout'
, fi: 'Welcome to Nightscout'
, fr: 'Welcome to Nightscout'
, he: 'Welcome to Nightscout'
, hr: 'Welcome to Nightscout'
, it: 'Welcome to Nightscout'
, ko: 'Welcome to Nightscout'
, nb: 'Welcome to Nightscout'
, pl: 'Welcome to Nightscout'
, pt: 'Welcome to Nightscout'
, ro: 'Welcome to Nightscout'
, nl: 'Welcome to Nightscout'
, ru: 'Welcome to Nightscout'
, sk: 'Welcome to Nightscout'
, sv: 'Welcome to Nightscout'
, tr: 'Welcome to Nightscout'
, zh_cn: 'Welcome to Nightscout'
, zh_tw: 'Welcome to Nightscout'
},
'virtAsstTitleLoopForecast': {
bg: 'Loop Forecast'
, cs: 'Loop Forecast'
Expand Down Expand Up @@ -13719,6 +13745,32 @@ function init() {
, zh_cn: '%1 单位'
, zh_tw: '%1 units of'
},
'virtAsstLaunch': {
bg: 'What would you like to check on Nightscout?'
, cs: 'What would you like to check on Nightscout?'
, de: 'What would you like to check on Nightscout?'
, dk: 'What would you like to check on Nightscout?'
, el: 'What would you like to check on Nightscout?'
, en: 'What would you like to check on Nightscout?'
, es: 'What would you like to check on Nightscout?'
, fi: 'What would you like to check on Nightscout?'
, fr: 'What would you like to check on Nightscout?'
, he: 'What would you like to check on Nightscout?'
, hr: 'What would you like to check on Nightscout?'
, it: 'What would you like to check on Nightscout?'
, ko: 'What would you like to check on Nightscout?'
, nb: 'What would you like to check on Nightscout?'
, pl: 'What would you like to check on Nightscout?'
, pt: 'What would you like to check on Nightscout?'
, ro: 'What would you like to check on Nightscout?'
, nl: 'What would you like to check on Nightscout?'
, ru: 'What would you like to check on Nightscout?'
, sk: 'What would you like to check on Nightscout?'
, sv: 'What would you like to check on Nightscout?'
, tr: 'What would you like to check on Nightscout?'
, zh_cn: 'What would you like to check on Nightscout?'
, zh_tw: 'What would you like to check on Nightscout?'
},
'virtAsstPreamble': {
bg: 'Your'
, cs: 'Vaše'
Expand Down
96 changes: 96 additions & 0 deletions tests/api.alexa.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict';

var request = require('supertest');
var language = require('../lib/language')();
const bodyParser = require('body-parser');

require('should');

describe('Alexa REST api', function ( ) {
const apiRoot = require('../lib/api/root');
const api = require('../lib/api/');
before(function (done) {
var env = require('../env')( );
env.settings.enable = ['alexa'];
env.settings.authDefaultRoles = 'readable';
env.api_secret = 'this is my long pass phrase';
this.wares = require('../lib/middleware/')(env);
this.app = require('express')( );
this.app.enable('api');
var self = this;
require('../lib/server/bootevent')(env, language).boot(function booted (ctx) {
self.app.use('/api', bodyParser({
limit: 1048576 * 50
}), apiRoot(env, ctx));

self.app.use('/api/v1', bodyParser({
limit: 1048576 * 50
}), api(env, ctx));
done( );
});
});

it('Launch Request', function (done) {
request(this.app)
.post('/api/v1/alexa')
.send({
"request": {
"type": "LaunchRequest",
"locale": "en-US"
}
})
.expect(200)
.end(function (err, res) {
if (err) return done(err);

const launchText = 'What would you like to check on Nightscout?';

res.body.response.outputSpeech.text.should.equal(launchText);
res.body.response.reprompt.outputSpeech.text.should.equal(launchText);
res.body.response.shouldEndSession.should.equal(false);
done( );
});
});

it('Launch Request With Intent', function (done) {
request(this.app)
.post('/api/v1/alexa')
.send({
"request": {
"type": "LaunchRequest",
"locale": "en-US",
"intent": {
"name": "UNKNOWN"
}
}
})
.expect(200)
.end(function (err, res) {
if (err) return done(err);

const unknownIntentText = 'I\'m sorry, I don\'t know what you\'re asking for.';

res.body.response.outputSpeech.text.should.equal(unknownIntentText);
res.body.response.shouldEndSession.should.equal(true);
done( );
});
});

it('Session Ended', function (done) {
request(this.app)
.post('/api/v1/alexa')
.send({
"request": {
"type": "SessionEndedRequest",
"locale": "en-US"
}
})
.expect(200)
.end(function (err) {
if (err) return done(err);

done( );
});
});
});