51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
var Alexa = require ('alexa-sdk');
|
|
const config = require ('../config/config');
|
|
var databaseHelper = require ('../helpers/database');
|
|
|
|
// Build the context manually, because Amazon Lambda is missing
|
|
var context = {
|
|
succeed: function (result) {
|
|
console.log (result);
|
|
res.json (result);
|
|
},
|
|
fail: function (error) {
|
|
console.log (error);
|
|
//We could send error json from here
|
|
},
|
|
};
|
|
|
|
var handlers = {};
|
|
|
|
module.exports = {
|
|
run: function (req, res) {
|
|
var alexa = Alexa.handler (req.body, context);
|
|
alexa.appId = config.SKILL_ID;
|
|
alexa.registerHandlers (handlers);
|
|
alexa.execute ();
|
|
},
|
|
updateModel: function () {
|
|
//Get info from database, and store it for faster response on intent
|
|
databaseHelper
|
|
.getSkill (config.SKILL_DB_ID)
|
|
.then (activeSkill => {
|
|
handlers = {};
|
|
handlers = {
|
|
LaunchRequest: function () {
|
|
this.response.speak (activeSkill.invocationAnswer);
|
|
this.emit (':responseReady');
|
|
},
|
|
};
|
|
activeSkill.intents.map (intent => {
|
|
handlers[intent.intentName] = function () {
|
|
this.response.speak (intent.answer);
|
|
this.emit (':responseReady');
|
|
};
|
|
});
|
|
})
|
|
.catch (e => {
|
|
//Something is wrong, skill is not ready, use catch-all intent to inform user
|
|
console.log ('Error. Skill doesnt exist');
|
|
});
|
|
},
|
|
};
|