From 2990fad6cfe886e0645fe50c2233883845bc59f3 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Fri, 5 Jan 2018 20:15:56 +0100 Subject: [PATCH 1/2] package update --- backend/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/package.json b/backend/package.json index 16afe98..7783d05 100644 --- a/backend/package.json +++ b/backend/package.json @@ -9,6 +9,7 @@ "ejs": "^2.3.1", "express": "^4.13.0", "isomorphic-fetch": "^2.2.1", + "mongodb": "^2.2.33", "request": "^2.83.0" }, "author": "Matt Kruse (http://mattkruse.com/)", From c3bba7ffa7475f5e5ee84c5e380d297e2f272f30 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Fri, 5 Jan 2018 20:32:55 +0100 Subject: [PATCH 2/2] express routes reorder --- backend/express.js | 123 +++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/backend/express.js b/backend/express.js index 017d375..255734a 100644 --- a/backend/express.js +++ b/backend/express.js @@ -15,6 +15,68 @@ var ObjectID = require ('mongodb').ObjectID; const router = express.Router (); +var skillID = 'amzn1.ask.skill.7115bfc9-313e-4728-830b-ebd19ce96cb3'; +var skillDbID = '5a232fb86ce046c749739455'; //_id in database + +var app = express(); + +// ALWAYS setup the alexa app and attach it to express before anything else. +var alexaApp = new alexa.app('step3'); // this means we still work with one skill + +alexaApp.express({ + expressApp: app, + + // 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 +}); + +// now POST calls to /test in express will be handled by the app.request() function + +// from here on you can setup any other express routes or middlewares as normal + +alexaApp.launch(function(request, response) { + const skill = db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,result)=>{ + if (err){ + response.say("I could not find desired skill") + }else{ + response.say(skill.invocationAnswer); + } + }); +}); + +var findElementByAttr = function (data, attr, val){ + for(var i = 0; i < data.length; i ++) { + if(data[i][attr] === val) { + return i; + } + } + return -1; +} + +alexaApp.request = (jsonRequest) => { + const alexaRequest = new alexa.request(jsonRequest); + if (alexaRequest.type() === "IntentRequest") { + const skill = db.collection('skill_list').findOne({ + _id: skillDbID + }); + if (skill) { + let intentId = findElementByAttr(skill.intents, 'intentName', alexaRequest.data.request.intent.name); + const response = new alexa.response(alexaRequest.getSession()); + if (intentId !== -1){ + return response.say(skill.intents[intentId].answer); + }else{ + return response.say('Sorry, I could not find desired intent'); + } + } + } +}; + router.get ('/getSkill/:id', async (req, res, next) => { try { const id = req.params.id; @@ -82,67 +144,6 @@ router.post ('/updateSkill/:id', async (req, res, next) => { } }); -var skillID = 'amzn1.ask.skill.7115bfc9-313e-4728-830b-ebd19ce96cb3'; -var skillDbID = '5a232fb86ce046c749739455'; //_id in database - -var app = express(); - -// ALWAYS setup the alexa app and attach it to express before anything else. -var alexaApp = new alexa.app('step3'); // this means we still work with one skill - -alexaApp.express({ - expressApp: app, - - // 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 -}); - -// now POST calls to /test in express will be handled by the app.request() function - -// from here on you can setup any other express routes or middlewares as normal - -alexaApp.launch(function(request, response) { - const skill = db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,result)=>{ - if (err){ - response.say("I could not find desired skill") - }else{ - response.say(skill.invocationAnswer); - } - }); -}); - -var findElementByAttr = function (data, attr, val){ - for(var i = 0; i < data.length; i ++) { - if(data[i][attr] === val) { - return i; - } - } - return -1; -} - -alexaApp.request = (jsonRequest) => { - const alexaRequest = new alexa.request(jsonRequest); - if (alexaRequest.type() === "IntentRequest") { - const skill = db.collection('skill_list').findOne({ - _id: skillDbID - }); - if (skill) { - let intentId = findElementByAttr(skill.intents, 'intentName', alexaRequest.data.request.intent.name); - const response = new alexa.response(alexaRequest.getSession()); - if (intentId !== -1){ - return response.say(skill.intents[intentId].answer); - }else{ - return response.say('Sorry, I could not find desired intent'); - } - } - } -}; app.use (function (req, res, next) { res.header ('Access-Control-Allow-Origin', '*');