Files
old-tellall/backend/express.js
2017-12-03 18:47:24 +01:00

143 lines
4.4 KiB
JavaScript

var amazonHelper = require('./helpers/amazon');
require('isomorphic-fetch');
var express = require('express');
var alexa = require('alexa-app');
var bodyParser = require('body-parser');
const dbURL = 'mongodb://localhost:27017/tellall';
const PORT = 5000;
const TOKEN = 'Atza|IwEBIMv0spn-eZhjP8-R2Jjkb4VUi-EY4V3MX-wlJyr2P6YBUmIChl7VKgRberEdQ-Wolp53SqfwHbxlSo4-rdUgFYFAxImB622boeqUBBCtwybP0OTBJBT1CVm_FBr48Li5LkwR9DxPciCnc052ddohpjGuGZxsCqIJo2c-7LPEhH0Lx63VOFgfPIfBsrVqjDbPNrr5ApPUijKYZWurktb6ytTks8eFUSTyv3FDbE5HEng0qpE4mgSjdgBkEc8BgUfpm2QGvctINH9SioUJOJonxgTPYhbD4BEd-jdQHsltKFzkb3-Rm3lFhEu3_CQFxCeQ6yGe1the_qID3vnPfpSY6hyblgF5L_5d7bE7BWg8fm6rSwXv67L2sMwBOji6cuR0wVVvXfYxmgGIMGkVQrq-SPSdtCpx2BvBz2SqqN6a_98svP8ukvhwc_oJsN6VwEmTDFgutNf-XuGkuVii-k9-DncwuwD00LvJG1FhBvbvSyuv-a3LAJSqTmroemTDG0xzLn7ULFY5p-93sM0_ZNGFeW-lL_r1ldqM_5lFRKDta1Tkg7lT9-rHftgnpGs4zv7vGLIPzHpNiXjsKyCk-wMQrihhWlR15kiz7oKDeTf6wCIETg';
const USER_ID = 'amzn1.ask.account.AGE34MG3VIQ75D4Q5CXFK25GUOXHZ2MIVSII3QUCOT3CP44IPE25PLD3DWW7HLWO2KVC7PC7VUXSZZEFPVHJ6PTDYVYYESJE35CE6VEXCE3BI3AK24TGO4CJXFF3SZ7IA4QVJRZC6EN4MUF2WUP4IB4CGDLRZZMYQENOFNBYWFXZHMZ5PA6S6ERK7NGEPUSDHXWKH26UGMIATMI';
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) => {
try {
let id = req.params.id;
let skill = req.body;
delete skill._id;
if (id !== '-1'){
let completeResult = {databaseUpdate:false, amazonUpdate:false, amazonMessage:''};
let result = db.collection('skill_list').update({_id: ObjectID(id)}, skill,{upsert:true}, (err, result)=>{
if (err) throw(err);
completeResult.databaseUpdate = (JSON.parse(result).nModified===1);
let generatedInteractionModel = amazonHelper.generateInteractionModel(skill);
fetch(`https://api.amazonalexa.com/v0/skills/${skill.skillID}/interactionModel/locales/en-US`, {
method: 'POST',
headers: {
Authorization: TOKEN
},
body: generatedInteractionModel
}).then(l=>l.text()).then(result=>{
completeResult.amazonUpdate = (JSON.stringify(result)===JSON.stringify({}));
completeResult.amazonMessage = JSON.stringify(result);
res.json(completeResult);
});
});
}else{
//no new skills for now
}
} catch (e) {
console.log ('error:', e);
next (e);
}
});
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
});
// 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.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 (dbURL).then (database => {
db = database;
db.collection ('intent_list');
app.listen (PORT, () =>
console.log ('Express server running on port ' + PORT)
);
});