Files
old-tellall/backend/models/alexa.js

53 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-01-18 21:33:44 +01:00
var Alexa = require ('alexa-sdk');
const config = require ('../config/config');
var databaseHelper = require ('../helpers/database');
var handlers = {};
module.exports = {
run: function (req, res) {
2018-01-18 21:43:25 +01:00
// 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
},
};
2018-01-18 21:33:44 +01:00
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 () {
2018-01-18 21:50:26 +01:00
this.response.listen (activeSkill.invocationAnswer);// Using listen so session doesn't end
//TODO : Maybe to ask user does he want to hear possible intents
//For this functionality, we need explanation text for each intent (Question)
2018-01-18 21:33:44 +01:00
this.emit (':responseReady');
},
};
activeSkill.intents.map (intent => {
handlers[intent.intentName] = function () {
2018-01-18 21:50:26 +01:00
this.response.listen (intent.answer); //Using listen so session doesn't end
2018-01-18 21:33:44 +01:00
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');
});
},
};