Files
old-tellall/backend/models/alexa.js
2018-01-18 21:56:56 +01:00

59 lines
1.8 KiB
JavaScript

var Alexa = require ('alexa-sdk');
const config = require ('../config/config');
var databaseHelper = require ('../helpers/database');
var handlers = {};
module.exports = {
run: function (req, res) {
// 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 alexa = Alexa.handler (req.body, context);
alexa.appId = config.SKILL_ID;
alexa.registerHandlers (handlers);
alexa.execute ();
},
updateModel: function () {
//TODO : Alexa-sdk has handlers like SessionEndedRequest, Unhandled ,... that can be used
//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)
.listen (' Would you like to continue ?');
//TODO : Maybe to ask user does he want to hear possible intents
//For this functionality, we need explanation text for each intent (Question)
this.emit (':responseReady');
},
};
activeSkill.intents.map (intent => {
handlers[intent.intentName] = function () {
this.response
.speak (intent.answer)
.listen ('Would you like to continue ?');
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');
});
},
};