diff --git a/backend/express.js b/backend/express.js index ff04e09..c138749 100644 --- a/backend/express.js +++ b/backend/express.js @@ -33,7 +33,27 @@ alexaApp.express({ // 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 + debug: true, + + preRequest: (request, response)=>{ + console.log('Pre req'); + /* + const alexaRequest = new alexa.request(request); + if (alexaRequest.type() === "IntentRequest") { + db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,skill)=>{ + if (skill) { + console.log(alexaRequest.data.request.intent); + 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'); + } + } + }); + }*/ + } }); // now POST calls to /test in express will be handled by the app.request() function @@ -42,17 +62,19 @@ alexaApp.express({ app.set("view engine", "ejs"); +/* alexaApp.launch(function(request, response) { console.log("Alexa launch"); console.log(request); const skill = db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,result)=>{ if (err){ - response.say("I could not find desired skill") + return response.say("I could not find desired skill") }else{ - response.say(skill.invocationAnswer); + return response.say(skill.invocationAnswer); } }); }); +*/ var findElementByAttr = function (data, attr, val){ for(var i = 0; i < data.length; i ++) { @@ -63,6 +85,7 @@ var findElementByAttr = function (data, attr, val){ return -1; } +/* alexaApp.request = (jsonRequest) => { const alexaRequest = new alexa.request(jsonRequest); if (alexaRequest.type() === "IntentRequest") { @@ -72,14 +95,30 @@ alexaApp.request = (jsonRequest) => { 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); + response.say(skill.intents[intentId].answer); }else{ - return response.say('Sorry, I could not find desired intent'); + response.say('Sorry, I could not find desired intent'); } } }); } }; +*/ + +alexaApp.intent("SimpleIntent", { + "slots": [], + "utterances": ["Tell me somethin"] + }, + function(request, response) { + response.say("This is sample"); + } +); + +/* +alexaApp.error = function(exception, request, response){ + console.log('Error ' + exception); + response.say('Sorry, there was error in Saburly skill'); // TODO : Rephrase this +};*/ router.get ('/getSkill/:id', async (req, res, next) => { try { diff --git a/backend/express.js.save b/backend/express.js.save new file mode 100644 index 0000000..afbfd33 --- /dev/null +++ b/backend/express.js.save @@ -0,0 +1,187 @@ +var amazonHelper = require('./helpers/amazon'); +var databaseHelper = require('./helpers/database'); + +const config = require('./config'); + +require('isomorphic-fetch'); + +var express = require('express'); +var alexa = require('alexa-app'); + +var bodyParser = require('body-parser'); + +var MongoClient = require ('mongodb').MongoClient; +var ObjectID = require ('mongodb').ObjectID; + +const router = express.Router (); + +var skillID = 'amzn1.ask.skill.7115bfc9-313e-4728-830b-ebd19ce96cb3'; +var skillDbID = '5a5016e775becaef2015da10'; //_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', '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 nor + +app.pre = function(request, response, type +}; + +app.set("view engine", "ejs"); + +alexaApp.launch(function(request, response) { + console.log("Alexa launch"); + console.log(request); + 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") { + db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,skill)=>{ + if (skill) { + console.log(alexaRequest.data.request.intent); + 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'); + } + } + }); + } +}; + +alexaApp.error = function(exception, request, response){ + console.log('Error ' + exception); + response.say('Sorry, there was error in Saburly skill'); // TODO : Rephrase this +}; + +router.get ('/getSkill/:id', async (req, res, next) => { + try { + const id = req.params.id; + + if (id.length !== 24){ + res.json([]); + throw("error"); + } + + db.collection ('skill_list').find({_id: ObjectID(id)}).toArray((err,result)=>{ + if (err){ + throw err; + res.json([]); + }else{ + res.json(result); + } + }); + + } catch (e) { + console.log ('error:', e); + next (e); + } +}); + +router.get ('/deleteSkill/:skillID', async (req, res, next) => { + try { + let id = req.params.id; + let result = db.collection('skill_list').remove({_id: ObjectID(id)},(err,result)=>{ + if (err) throw err; + res.json(result); + }); + } catch (e) { + console.log ('error:', e); + next (e); + } +}); + +router.post ('/updateSkill/:id', async (req, res, next) => { + + let id = req.params.id; + let skill = req.body; + delete skill._id; + if (id !== '-1'){ + amazonHelper.updateSkill(skill,db).then(amazonResult=>{ + if (amazonResult===200 || amazonResult===202){ + //Skill uploaded, it's ok to update database + let result = db.collection('skill_list').update({_id: ObjectID(id)}, skill,{upsert:true}, (err, result)=>{ + if (JSON.parse(result).nModified===1){ + //database update ok + res.json({result:0, message:'ok'}); + }else{ + res.json({result:-1, message:'ok'}); + } + }); + }else{ + res.json({result:-1, message:'Amazon result ' + amazonResult}); + } + }).catch(e=>{ + //skill upload went wrong, don't update database, send error + res.json({result:-1, message:e}); + }); + + }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); + + +MongoClient.connect (config.dbURL).then (database => { + db = database; + db.collection ('intent_list'); + + databaseHelper.initModule(db); + + app.listen (config.PORT, () =>{ + console.log ('Express server running on port ' + config.PORT); + databaseHelper.loadTokens(); + }); +}).catch(e=>{ + console.log("error : " + e); +}); diff --git a/backend/t_server.js b/backend/t_server.js new file mode 100644 index 0000000..e7e5db8 --- /dev/null +++ b/backend/t_server.js @@ -0,0 +1,144 @@ +var amazonHelper = require('./helpers/amazon'); +var databaseHelper = require('./helpers/database'); + +const config = require('./config'); + +require('isomorphic-fetch'); + +var express = require('express'); +var alexa = require('alexa-app'); + +var bodyParser = require('body-parser'); + +var MongoClient = require ('mongodb').MongoClient; +var ObjectID = require ('mongodb').ObjectID; + +const router = express.Router (); + +var skillID = 'amzn1.ask.skill.7115bfc9-313e-4728-830b-ebd19ce96cb3'; +var skillDbID = '5a5016e775becaef2015da10'; //_id in database + +//***********/ +//var express = require("express"); +//var alexa = require("alexa-app"); + +var PORT = process.env.port || 5000; +var app = express(); + +// ALWAYS setup the alexa app and attach it to express before anything else. +var alexaApp = new alexa.app("step3"); + +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, + + preRequest: function(request,response){ + console.log('pre'); + const alexaRequest = new alexa.request(request); + if (alexaRequest.type() === "IntentRequest") { + db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,skill)=>{ + if (skill) { + console.log(alexaRequest.data.request.intent); + 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'); + } + } + }); + } + } +}); + +// 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 +app.set("view engine", "ejs"); + +alexaApp.launch(function(request, response) { + console.log('launch'); + response.say("You launched the app!"); +}); + +//alexaApp.dictionary = { "names": ["matt", "joe", "bob", "bill", "mary", "jane", "dawn"] }; + +/* +alexaApp.intent("GetFirstQuestion", { + "slots":[], + "utterances": [ + "Tell me about your process", "Give me info about your process" + ] + }, + function(request, response) { + console.log('My name'); + response.say("Success!"); + } +); +*/ + +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") { + db.collection('skill_list').findOne({_id: ObjectID(skillDbID)}, (err,skill)=>{ + if (skill) { + console.log(alexaRequest.data.request.intent); + let intentId = findElementByAttr(skill.intents, 'intentName', alexaRequest.data.request.intent.name); + const response = new alexa.response(alexaRequest.getSession()); + if (intentId !== -1){ + response.say(skill.intents[intentId].answer); + }else{ + response.say('Sorry, I could not find desired intent'); + } + } + }); + } +}; +*/ + + +alexaApp.intent("SimpleIntent", { + "slots": [], + "utterances": ["Tell me somethin"] + }, + function(request, response) { + response.say("This is sample"); + } +); + +//app.listen(PORT); +//console.log("Listening on port " + PORT + ", try http://localhost:" + PORT + "/test"); + +MongoClient.connect (config.dbURL).then (database => { + db = database; + db.collection ('intent_list'); + + databaseHelper.initModule(db); + + app.listen (config.PORT, () =>{ + console.log ('Express server running on port ' + config.PORT); + databaseHelper.loadTokens(); + }); +}).catch(e=>{ + console.log("error : " + e); +})