forked from dialogflow/agent-human-handoff-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (56 loc) · 1.92 KB
/
app.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
// Load third party dependencies
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
// Load our custom classes
const CustomerStore = require('./customerStore.js');
const MessageRouter = require('./messageRouter.js');
// Grab the service account credentials path from an environment variable
var agentKey = process.env.agentKey;
if(process.env.AI_KEY_PATH){
var fs = require("fs");
agentKey = fs.readFileSync(process.env.AI_KEY_PATH);
console.log('credenciales OK');
}
else if(!agentKey) {
console.log('faltan las credenciales');
process.exit(1);
}
// Load and instantiate the Dialogflow client library
const { SessionsClient } = require('dialogflow');
var jsonContent = JSON.parse(agentKey);
console.log("private_key:", jsonContent.private_key);
console.log("client_email:", jsonContent.client_email);
console.log("project_id:", jsonContent.project_id);
const privateKey = jsonContent.private_key
const clientEmail = jsonContent.client_email
const projectId = jsonContent.project_id
let config = {
credentials: {
private_key: privateKey,
client_email: clientEmail
}
}
var dialogflowClient = new SessionsClient(config)
// Instantiate our app
const customerStore = new CustomerStore();
const messageRouter = new MessageRouter({
customerStore: customerStore,
dialogflowClient: dialogflowClient,
projectId: projectId,
customerRoom: io.of('/customer'),
operatorRoom: io.of('/operator')
});
// Serve static html files for the customer and operator clients
app.get('/customer', (req, res) => {
res.sendFile(`${__dirname}/static/customer.html`);
});
app.get('/operator', (req, res) => {
res.sendFile(`${__dirname}/static/operator.html`);
});
// Begin responding to websocket and http requests
messageRouter.handleConnections();
var number = process.env.PORT || 3000;
http.listen(number, () => {
console.log('Listening on *:',number);
});