-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (44 loc) · 1.38 KB
/
index.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
'use strict';
var dotenv = require('dotenv');
dotenv.load();
const express = require('express');
const app = express();
const uuidv1 = require('uuid/v1');
const ACCESS_TOKEN = "ecff7a6a031f4c338dce5bd0211d8839";
const AI_SESSION_ID = uuidv1();
const dialogflow = require('apiai');
const ai = dialogflow(ACCESS_TOKEN);
app.use(express.static(__dirname + '/views')); // HTML Pages
app.use(express.static(__dirname + '/public')); // CSS, JS & Images
const server = app.listen(3000, function(){
console.log('listening on port %d', server.address().port);
});
const socketio = require('socket.io')(server);
socketio.on('connection', function(socket){
console.log('a user connected');
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
//Serve UI
app.get('/chat', (req, res) => {
res.sendFile(__dirname + '/views/app.html');
});
socketio.on('connection', function(socket) {
socket.on('chat request', (text) => {
console.log('Message: ' + text);
// Get a reply from API.ai
let aiReq = ai.textRequest(text, {
sessionId: AI_SESSION_ID
});
aiReq.on('response', (response) => {
let aiResponse = response.result.fulfillment.speech;
console.log('AI Response: ' + aiResponse);
socket.emit('ai response', aiResponse);
});
aiReq.on('error', (error) => {
console.log(error);
});
aiReq.end();
});
});