from server
This commit is contained in:
144
backend/t_server.js
Normal file
144
backend/t_server.js
Normal file
@@ -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);
|
||||
})
|
||||
Reference in New Issue
Block a user