-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (107 loc) · 3.62 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
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
const express = require("express");
const app = express();
const morgan = require('morgan');
const path = require('path');
const { SessionsClient } = require('@google-cloud/dialogflow');
const uuid = require('uuid');
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const dotenv = require('dotenv');
const Intent = require('./models/intents');
const connectDB = require('./db/connect');
dotenv.config();
app.use(morgan('dev'));
const projectId = process.env.PROJECT_ID;
const credentials = require('./apigen-401021-b0aa0107351c.json');
const PORT = process.env.PORT || 4000;
const sessionClient = new SessionsClient({
projectId: projectId,
credentials: credentials
});
app.use(express.static(path.join(__dirname, 'public'), {
setHeaders: (res, path) => {
if (path.endsWith('.css')) {
res.setHeader('Content-Type', 'text/css');
}
if (path.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript');
}
}
}));
app.use(express.static(path.join(__dirname, 'views')));
app.get('/credentials', (req, res) => {
res.send(credentials).status(200);
});
async function detectIntent(projectId, text) {
const sessionId = uuid.v4();
const sessionPath = sessionClient.projectAgentSessionPath(projectId, sessionId);
const request = {
session: sessionPath,
queryInput: {
text: {
text: text,
languageCode: 'en-US',
},
},
};
try {
const responses = await sessionClient.detectIntent(request);
const result = responses[0].queryResult;
const available = await Intent.find({});
if (available.length > 0) {
if (result.fulfillmentMessages.length === 0) {
const mergedData = [...available, { intent: text }];
await Intent.create({ intent: mergedData });
return 'I have not been trained for that yet, check back later';
}
} else {
if (result.fulfillmentMessages.length === 0) {
await Intent.create({ intent: [{ intent: text }] });
return 'I have not been trained for that yet, check back later';
}
}
return result.fulfillmentText;
} catch (error) {
console.error('Error communicating with Dialogflow:', error.message);
return 'Sorry, there was an error.';
}
}
// Socket.IO connection handling
io.on('connection', function (socket) {
socket.on('chat message', async (text) => {
try {
const aiText = await detectIntent(projectId, text);
socket.emit('bot reply', aiText);
} catch (error) {
console.log(error);
socket.emit('bot reply', "I couldn't find a reply, Sir.");
}
});
socket.on('text',async(text)=>{
try{
const aiText = await detectIntent(projectId, text)
socket.emit('text-reply',aiText)
}catch(err){
console.log(err)
socket.emit('text-reply',err)
}
})
});
app.get('/test', (req, res) => {
console.log(process.env.PROJECT_ID);
res.status(200).json('check');
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
(async () => {
try {
const connectionString = process.env.MONGO_URI || undefined;
await connectDB(connectionString);
http.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
} catch (error) {
console.error("Something went wrong:", error);
}
})();