backend code refactoring
This commit is contained in:
@@ -17,19 +17,18 @@ var alexaApp = new alexa.app ('saburly'); // this means we still work with one s
|
||||
|
||||
module.exports = {
|
||||
init: function (express) {
|
||||
|
||||
alexaApp.express ({
|
||||
expressApp: express,
|
||||
|
||||
|
||||
// verifies requests come from amazon alexa. Must be enabled for production.
|
||||
// You can disable this if you're running a dev environment and want to POST
|
||||
// things to test behavior. enabled by default.
|
||||
checkCert: false,
|
||||
|
||||
|
||||
// sets up a GET route when set to true. This is handy for testing in
|
||||
// development, but not recommended for production. disabled by default
|
||||
debug: true,
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
updateIntentsJSON: function () {
|
||||
|
||||
@@ -8,8 +8,8 @@ config.REFRESH_TOKEN = 'Atzr|IwEBICA3kDhfSJxlwvnQp9AD1o115AC_KBbFd5GBg8oN_QHWn2o
|
||||
config.TOKEN_EXPIRES_IN = 1515100500;
|
||||
|
||||
config.SKILL_ID = 'amzn1.ask.skill.efbf0564-a732-4ba9-958f-57939138adae';
|
||||
config.SKILL_DB_ID = '5a5016e775becaef2015da10'; //for server
|
||||
//config.SKILL_DB_ID = '5a232fb86ce046c749739455'; //for local
|
||||
//config.SKILL_DB_ID = '5a5016e775becaef2015da10'; //for server
|
||||
config.SKILL_DB_ID = '5a232fb86ce046c749739455'; //for local
|
||||
|
||||
config.CLIENT_ID = 'amzn1.application-oa2-client.c748ca56ded04a95b236979898585ff7';
|
||||
config.CLIENT_SECRET = '6dea8125cecd049d3c4cff7bb5bdfd3ff17bc6fed246c4c8f6b519d9ed08d0b3';
|
||||
|
||||
6
backend/controllers/index.js
Normal file
6
backend/controllers/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
var express = require ('express'), router = express.Router ();
|
||||
|
||||
router.use ('/getSkill', require ('./skill'));
|
||||
router.use ('/updateSkill', require ('./skill')); // this will be fixed in next code refactoring
|
||||
|
||||
module.exports = router;
|
||||
91
backend/controllers/skill.js
Normal file
91
backend/controllers/skill.js
Normal file
@@ -0,0 +1,91 @@
|
||||
var express = require ('express'), router = express.Router ();
|
||||
const constants = require ('../config/constants');
|
||||
var databaseHelper = require ('../helpers/database');
|
||||
var amazonHelper = require ('../helpers/amazon');
|
||||
var bodyParser = require ('body-parser');
|
||||
var alexa = require ('../components/alexa');
|
||||
|
||||
router.get ('/:id', async (req, res, next) => {
|
||||
const id = req.params.id;
|
||||
|
||||
if (id.length !== constants.skillIDLength) {
|
||||
res.json ([]);
|
||||
} else {
|
||||
databaseHelper
|
||||
.getSkill (id)
|
||||
.then (result => {
|
||||
res.json (result);
|
||||
})
|
||||
.catch (err => {
|
||||
res.json ([]);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post ('/:id', bodyParser.json (), async (req, res, next) => {
|
||||
let id = req.params.id;
|
||||
let dataFromWeb = JSON.stringify (req.body);
|
||||
let skill = JSON.parse (dataFromWeb);
|
||||
let updateOnAmazon = skill.updateOnAmazon;
|
||||
|
||||
delete skill.updateOnAmazon;
|
||||
delete skill._id;
|
||||
|
||||
console.log ('id = ' + id);
|
||||
|
||||
if (id !== '-1') {
|
||||
if (updateOnAmazon) {
|
||||
amazonHelper
|
||||
.updateSkill (skill)
|
||||
.then (amazonResult => {
|
||||
console.log ('Amazon : ' + amazonResult);
|
||||
if (
|
||||
amazonResult === constants.amazonResultCodes.ok ||
|
||||
amazonResult === constants.amazonResultCodes.accepted
|
||||
) {
|
||||
//Skill uploaded, it's ok to update databaseI
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.databaseError,
|
||||
message: '',
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.amazonError,
|
||||
message: amazonResult,
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.amazonError,
|
||||
message: 'unknown',
|
||||
});
|
||||
});
|
||||
} else {
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({
|
||||
result: constants.apiResultCodes.databaseError,
|
||||
message: '',
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
//no new skills for now
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -5,18 +5,16 @@ var databaseHelper = require ('./database');
|
||||
|
||||
var getBuildStatus = function (skillID) {
|
||||
fetch (
|
||||
`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: config.TOKEN,
|
||||
},
|
||||
}
|
||||
)
|
||||
.then (l => l.text ())
|
||||
.then (result => {
|
||||
return result;
|
||||
});
|
||||
`https://api.amazonalexa.com/v0/skills/${skillID}/interactionModel/locales/en-US/status`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: config.TOKEN,
|
||||
},
|
||||
}
|
||||
).then (result => {
|
||||
return result.text ();
|
||||
});
|
||||
};
|
||||
|
||||
var refreshTokens = function () {
|
||||
@@ -45,9 +43,9 @@ var refreshTokens = function () {
|
||||
parsedResponse.access_token,
|
||||
parsedResponse.expires_in
|
||||
);
|
||||
console.log('Token refresh failed');
|
||||
console.log(body);
|
||||
reject(body);
|
||||
console.log ('Token refresh failed');
|
||||
console.log (body);
|
||||
reject (body);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -80,52 +78,49 @@ var generateInteractionModel = function (skill) {
|
||||
allIntents.push ({name: intent.intentName, samples: intent.questions});
|
||||
});
|
||||
|
||||
|
||||
//Special Email Intents :
|
||||
allIntents.push({
|
||||
allIntents.push ({
|
||||
name: 'EmailIntentLaunch',
|
||||
slots:[],
|
||||
slots: [],
|
||||
samples: [
|
||||
'I want to send a message',
|
||||
'I would like to send a message',
|
||||
'I would like to leave a message',
|
||||
'Leave a message'
|
||||
]
|
||||
'Leave a message',
|
||||
],
|
||||
});
|
||||
|
||||
allIntents.push({
|
||||
allIntents.push ({
|
||||
name: 'EmailIntent',
|
||||
slots:[
|
||||
slots: [
|
||||
{
|
||||
'name': 'Name',
|
||||
'type': 'AMAZON.US_FIRST_NAME'
|
||||
name: 'Name',
|
||||
type: 'AMAZON.US_FIRST_NAME',
|
||||
},
|
||||
{
|
||||
'name': 'Email',
|
||||
'type': 'AMAZON.LITERAL'
|
||||
name: 'Email',
|
||||
type: 'AMAZON.LITERAL',
|
||||
},
|
||||
{
|
||||
'name': 'Message',
|
||||
'type': 'AMAZON.LITERAL'
|
||||
name: 'Message',
|
||||
type: 'AMAZON.LITERAL',
|
||||
},
|
||||
{
|
||||
'name': 'Data',
|
||||
'type': 'AMAZON.LITERAL'
|
||||
}
|
||||
name: 'Data',
|
||||
type: 'AMAZON.LITERAL',
|
||||
},
|
||||
],
|
||||
|
||||
|
||||
|
||||
samples: [
|
||||
'My name is {Name}',
|
||||
'I am {Name}',
|
||||
'{exampleww at wwdwdw|Data}',
|
||||
'My email is {example at efefegedd|Email}',
|
||||
'Send replay to {example at abcdefg|Email}',
|
||||
'My message is {The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.|Message}'
|
||||
]
|
||||
|
||||
'My message is {The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.|Message}',
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
result.interactionModel = {};
|
||||
|
||||
result.interactionModel.languageModel = {
|
||||
|
||||
7
backend/middleware/index.js
Normal file
7
backend/middleware/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
module.exports = function (req, res, next) {
|
||||
res.header ('Access-Control-Allow-Origin', '*');
|
||||
res.header ('Access-Control-Allow-Headers', 'Origin, Content-Type');
|
||||
res.header ('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
||||
res.header ('Access-Control-Allow-Credentials', 'true');
|
||||
next ();
|
||||
};
|
||||
@@ -5,7 +5,8 @@ const constants = require ('./config/constants');
|
||||
require ('isomorphic-fetch');
|
||||
var express = require ('express');
|
||||
var alexa = require('./components/alexa');
|
||||
var bodyParser = require ('body-parser');
|
||||
|
||||
|
||||
var MongoClient = require ('mongodb').MongoClient;
|
||||
var ObjectID = require ('mongodb').ObjectID;
|
||||
|
||||
@@ -13,105 +14,18 @@ const router = express.Router ();
|
||||
var app = express ();
|
||||
|
||||
// ALWAYS setup the alexa app and attach it to express before anything else.
|
||||
alexa.init(app);
|
||||
|
||||
// from here on you can setup any other express routes or middlewares as nor
|
||||
alexa.init (app);
|
||||
|
||||
app.set ('view engine', 'ejs');
|
||||
|
||||
/*
|
||||
router.get ('/deleteSkill/:skillID', async (req, res, next) => {
|
||||
databaseHelper
|
||||
.deleteSkill (req.params.id)
|
||||
.then (result => {
|
||||
res.json (result);
|
||||
})
|
||||
.catch (err => {
|
||||
res.json (err);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
router.get ('/getSkill/:id', async (req, res, next) => {
|
||||
const id = req.params.id;
|
||||
|
||||
if (id.length !== constants.skillIDLength) {
|
||||
res.json ([]);
|
||||
} else {
|
||||
databaseHelper
|
||||
.getSkill (id)
|
||||
.then (result => {
|
||||
res.json (result);
|
||||
})
|
||||
.catch (err => {
|
||||
res.json ([]);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post ('/updateSkill/:id', async (req, res, next) => {
|
||||
let id = req.params.id;
|
||||
let dataFromWeb = JSON.stringify(req.body);
|
||||
let skill = JSON.parse(dataFromWeb);
|
||||
let updateOnAmazon = skill.updateOnAmazon;
|
||||
delete skill.updateOnAmazon;
|
||||
delete skill._id;
|
||||
console.log('id = ' + id);
|
||||
if (id !== '-1') {
|
||||
if (updateOnAmazon){
|
||||
amazonHelper.updateSkill(skill).then(amazonResult=>{
|
||||
console.log('Amazon : ' + amazonResult);
|
||||
if (amazonResult === constants.amazonResultCodes.ok || amazonResult === constants.amazonResultCodes.accepted) {
|
||||
//Skill uploaded, it's ok to update databaseI
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({result: constants.apiResultCodes.databaseError, message: ''});
|
||||
});
|
||||
}else{
|
||||
res.json({result: constants.apiResultCodes.amazonError, message: amazonResult});
|
||||
}
|
||||
}).catch(e=>{
|
||||
res.json ({result: constants.apiResultCodes.amazonError, message: 'unknown'});
|
||||
});
|
||||
}else{
|
||||
databaseHelper
|
||||
.updateSkill (id, skill)
|
||||
.then (result => {
|
||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
||||
alexa.updateIntentsJSON ();
|
||||
})
|
||||
.catch (e => {
|
||||
res.json ({result: constants.apiResultCodes.databaseError, message: ''});
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
//no new skills for now
|
||||
}
|
||||
});
|
||||
|
||||
app.use (function (req, res, next) {
|
||||
res.header ('Access-Control-Allow-Origin', '*');
|
||||
res.header ('Access-Control-Allow-Headers', 'Origin, Content-Type');
|
||||
res.header ('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
||||
res.header ('Access-Control-Allow-Credentials', 'true');
|
||||
next ();
|
||||
});
|
||||
|
||||
app.use (bodyParser.json ());
|
||||
app.use ('/', router);
|
||||
app.use (require ('./middleware')); //common middleware for all requests
|
||||
app.use (require ('./controllers')); //all routes
|
||||
|
||||
MongoClient.connect (config.dbURL)
|
||||
.then (database => {
|
||||
databaseHelper.initModule (database);
|
||||
|
||||
app.listen (config.PORT, () => {
|
||||
console.log ('Express server running on port ' + config.PORT);
|
||||
console.log ('Express server running on port ' + config.PORT);
|
||||
alexa.updateIntentsJSON ();
|
||||
databaseHelper.loadTokens ();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user