-
Notifications
You must be signed in to change notification settings - Fork 0
/
df-connecting-server.js
229 lines (159 loc) · 6.27 KB
/
df-connecting-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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
'use strict'
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser')
const app = express();
const expressWs = require('express-ws')(app);
const request = require('request');
const dialogflow = require('dialogflow');
const Conversation = require('./conversations');
const language = require('@google-cloud/language');
const client = new language.LanguageServiceClient();
// Google Dialogflow credentials
const projectId = process.env.GCLOUD_PROJECT_ID;
const port = process.env.PORT || 5000; // This Node.js server port
//==========================================================
app.use(bodyParser.json());
//----
const reqHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
// app.use(function (req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// res.header("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,DELETE");
// res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// next();
// });
//-----------------------------------------
// This websocket receives the caller spoken audio,
// It connects to DialogFlow using the Dialogflow node SDK
// It will play back the bot audio response through the websocket
app.ws('/socket', async (ws, req) => {
const date = new Date().toLocaleString();
console.log("DialogFlow WebSocket call at " + date);
const uuid = req.query.original_uuid;
const vapiAppUrl = 'https://' + req.query.vapi_app_host + '/analytics';
const analyzeSentiment = req.query.analyze_sentiment;
function reqCallback(error, response, body) {
if (body != "Ok") {
console.log("Webhook call status to VAPI application:", body);
};
}
let conversation = new Conversation(uuid);
await conversation.init();
let callback = async function (response) {
try {
const timerIds = app.get(`playtimers_${uuid}`) || [];
for (const timerId of timerIds) {
clearTimeout(timerId);
};
if ( response.outputAudio.byteLength > 0 ) {
console.log(">>> Playback in progress for websocket:", uuid);
// Send Dialogflow audio response to caller via websocket
console.log("Sending back audio bytes:", response.outputAudio.byteLength);
const replyAudio = response.outputAudio;
const frames = replyAudio.length / 640;
let pos = 0;
const timerIds = [];
for (let i = 0; i < frames + 1; i++) {
const newpos = pos + 640;
const data = replyAudio.slice(pos, newpos);
timerIds.push(setTimeout(function () {
if (ws.readyState === 1) { // Send data only if websocket is up
ws.send(data);
}
}, i * 20)) // Send a frame every 20 ms
pos = newpos;
}
app.set(`playtimers_${uuid}`, timerIds);
}
} catch (e) {
console.log("Websocket resonse error:", e);
}
if (response.responseId != '') {
const queryText = response.queryResult.queryText;
let sentimentScore = "Request text is empty or sentiment analysis is not requested";
if (queryText != "" && analyzeSentiment == "true") {
const [sentiment] = await analyzeSentimentOfText(queryText);
sentimentScore = sentiment.documentSentiment.score;
}
// const docSentiment = sentiment.documentSentiment;
// console.log(`Document sentiment:`);
// console.log(` Score: ${docSentiment.score}`);
// console.log(` Magnitude: ${docSentiment.magnitude}`);
// const sentences = sentiment.sentences;
// console.log('Sentences: ', sentences);
// sentences.forEach(sentence => {
// console.log(`Sentence: ${sentence.text.content}`);
// console.log(` Score: ${sentence.sentiment.score}`);
// console.log(` Magnitude: ${sentence.sentiment.magnitude}`);
// });
const result = {
'uuid': uuid,
'queryText': queryText,
'fulfillmentText': response.queryResult.fulfillmentText,
'sentiment': sentimentScore,
'allRequiredParamsPresent': response.queryResult.allRequiredParamsPresent,
'action': response.queryResult.action,
'intent': response.queryResult.intent.displayName,
};
console.log("result:", JSON.stringify(result));
let Reqoptions = {
url: vapiAppUrl,
method: 'POST',
headers: reqHeaders,
body: JSON.stringify(result)
};
request(Reqoptions, reqCallback);
}
};
conversation.startStreamingTimer(callback);
ws.on('message', (msg) => {
if (typeof msg === "string") {
let config = JSON.parse(msg);
console.log(">>> Message:", msg);
}
else {
conversation.sendMessage(msg);
}
});
ws.on('close', () => {
console.log(">>> Websocket closed");
conversation.closeConversation();
})
})
//----------
async function analyzeSentimentOfText(text) {
// [START language_sentiment_text]
// Imports the Google Cloud client library
// const language = require('@google-cloud/language');
// Creates a client
// const client = new language.LanguageServiceClient();
// const text = 'Hello, world!';
// Prepares a document, representing the provided text
const document = {
content: text,
type: 'PLAIN_TEXT',
};
// Detects the sentiment of the document
const [result] = await client.analyzeSentiment({document});
// const sentiment = result.documentSentiment;
// console.log(`Document sentiment:`);
// console.log(` Score: ${sentiment.score}`);
// console.log(` Magnitude: ${sentiment.magnitude}`);
// const sentences = result.sentences;
// console.log('Sentences: ', sentences);
// sentences.forEach(sentence => {
// console.log(`Sentence: ${sentence.text.content}`);
// console.log(` Score: ${sentence.sentiment.score}`);
// console.log(` Magnitude: ${sentence.sentiment.magnitude}`);
// });
return [result];
}
//----------
app.use('/', express.static(__dirname));
//-----------
app.listen(port, () => console.log(`Server application listening on port ${port}!`));
//------------