-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (55 loc) · 1.98 KB
/
server.js
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
var express = require('express');
var app = express();
var fs = require('fs');
const twilio = require('twilio').twiml.VoiceResponse;
const urlencoded = require('body-parser').urlencoded;
app.use(urlencoded({extended: false}));
const RiveScript = require('rivescript');
var bot = new RiveScript();
bot.loadDirectory("brain", loading_done, loading_error);
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var reply;
var client = new twilio('XXX', 'XXX');
io.sockets.on('connection', function (socket) {
socket.on('answers', function (data) {
});
});
function loading_done (batch_num) {
console.log("Batch #" + batch_num + " has finished loading!");
bot.sortReplies();
}
function loading_error (error) {
console.log("Error when loading files: " + error);
}
app.post('/voice', (request, response) => {
const twiml = new twilio();
gather = twiml.gather({input: 'speech', action: '/gather', timeout:3});
gather.say('Hello. You are bothering me', {voice: 'alice'});
twiml.redirect('/voice');
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/gather', (request, response) => {
const twiml = new twilio();
var city = request.body.CallerCity.toString();
var state = request.body.FromState;
var speech = request.body.SpeechResult;
reply = bot.reply("local-user", request.body.SpeechResult);
console.log("The human says: " + request.body.SpeechResult);
console.log("The bot says: " + reply);
gather = twiml.gather({input: 'speech', action: '/gather', timeout:3});
//gather.say('I know you. Your are from' + city.toLowerCase() + state, {voice: 'alice'});
gather.say(reply, {voice: 'alice'});
//console.log(request.body);
response.type('text/xml');
response.send(twiml.toString());
});
app.post('/alice', (request, response) => {
console.log(request.body);
response.send(request.body);
});
server.listen(8800, function () {
console.log('Server listening on port 8800!')
})
app.use(express.static('public'));