Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
153 lines (123 loc) · 5.79 KB
/
Copy pathserver.js
File metadata and controls
153 lines (123 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var html_routes = require('./routes/html');
var wiki_functions = require('./routes/wikipedia')
var sassMiddleware = require('node-sass-middleware');
var apiai = require('apiai')('074deee29e8e4ea68310b5fcb2f87e60');
var app = express();
app.use(sassMiddleware({
/* Options */
src: path.join(__dirname, 'sass'),
dest: path.join(__dirname, 'public/stylesheets'),
debug: true,
prefix: '/public/stylesheets'
}));
app.use(express.static(__dirname + '/assets'));
app.use(express.static(__dirname + '/'));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// --------- Routes ------------ //
app.get('/', html_routes.homePage);
const server = app.listen(process.env.PORT || 3000);
var sessionId = makeid();
var currentCategories = [];
var currentSentences = [];
// ------------ Socket and DialogFlow connection ------------ //
/* Source: Adjusted from https://www.smashingmagazine.com/2017/08/ai-chatbot-web-speech-api-node-js/ */
const io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('a user connected');
});
// When a response from the user is recieved
io.on('connection', function(socket) {
socket.on('chat message', (text) => {
// Get a reply from API.AI
let apiaiReq = apiai.textRequest(text, {
sessionId: sessionId
});
apiaiReq.on('response', (response) => {
let res = response.result;
console.log(res);
let aiText = res.fulfillment.speech;
let intent = res.metadata.intentName;
let topic = "";
if (intent != "new-topic") {
topic = res.contexts.length != 0 ? res.contexts.find(function(c) {return c.name == "current-topic"}).parameters.topic : res.parameters.topic;
}
let result = "";
var emitResponseObject = {emitResponse: emitResponse, socket: socket, aiText: aiText};
// check the intent name and decide action based on it
if (intent == "request-topic") {
wiki_functions.checkForTopic(topic, emitResponseObject);
} else if (intent == 'list-categories' || intent == 'list-all-categories') {
wiki_functions.getCategories(topic, function (categories) {
currentCategories = categories;
if (intent == "list-categories") {
emitResponseObject.aiText = emitResponseObject.aiText + " There are " + categories.length + "categories in total. ";
wiki_functions.listCategories(currentCategories.splice(0, 5), emitResponseObject, false);
} else {
wiki_functions.listCategories(currentCategories, emitResponseObject, true);
}
});
} else if (intent == 'list-more-categories') {
if (currentCategories.length == 0) {
emitResponse(socket, "There are no more categories to list. Do you want to list them again?", "");
} else {
wiki_functions.listCategories(currentCategories.splice(0, 5), emitResponseObject);
}
} else if (intent == 'request-category') {
var category = res.parameters.category;
wiki_functions.getCategory(topic, category, function(sentences) {
// if the category exists
if (sentences.length > 0) {
currentSentences = sentences;
wiki_functions.saySection(currentSentences.splice(0, 3), emitResponseObject);
// else we tell user we have no categories
} else {
emitResponse(socket, "", "I'm sorry, I don't have any information on " + category + " for " + topic + ". Do you want to choose another category or pick a new topic?");
}
});
} else if (intent == 'request-summary') {
wiki_functions.getSummary(topic, function(sentences) {
currentSentences = sentences;
wiki_functions.saySection(currentSentences.splice(0, 3), emitResponseObject);
});
} else if (intent == 'request-more') {
if (currentSentences.length == 0) {
emitResponse(socket, "I've told you everything in this section. We can talk about a new topic, or I can tell you about a different section.", "");
} else {
console.log(currentSentences);
wiki_functions.saySection(currentSentences.splice(0, 3), emitResponseObject);
}
} else if (intent == 'correct-error') {
} else {
emitResponse(socket, aiText, "");
}
});
apiaiReq.on('error', (error) => {
console.log(error);
});
apiaiReq.end();
});
});
// --------------- Helpers ---------------- //
function emitResponse(socket, aiText, result) {
result = aiText + result;
socket.emit('bot reply', result); // Send the result back to the browser!
}
function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
console.log('Listening on port 3000');