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