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', '*');