-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversations.js
149 lines (111 loc) · 3.49 KB
/
conversations.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
'use strict'
require('dotenv').config();
const dialogFlow = require('dialogflow');
const projectId = process.env.GCLOUD_PROJECT_ID;
const googleAppCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
const STREAM_TIMEOUT_SEC = 50;
//-----------------
class Conversation {
constructor(original_uuid) {
this.init = this.init.bind(this);
this.isWritingAudioStream = true;
this.analyzeContentStream = null;
this.streamingTimer = null;
this.originalCallUuid = '_call_uuid_' + original_uuid; // voice call type conversation
}
//-- original call uuid xxx...xxx is passed to Dialogflow as "call_uuid_xxx...xxx"
async init() {
try {
console.log('>>> init this.originalCallUuid: ', this.originalCallUuid, ' for credentials at: ', googleAppCredentials);
console.log("*#*#*#*# GCloud ID in conversation: " + projectId);
this.sessionClient = new dialogFlow.SessionsClient();
console.log("Got sessionClient...");
this.sessionPath = this.sessionClient.sessionPath(projectId, this.originalCallUuid);
} catch (error) {
console.log(" Conversation error caught: ");
console.log(error);
}
}
//--------
startStreamingTimer(callBackFunction) {
var _this = this;
this.streamingTimer = setInterval(function () {
_this.restartStream(callBackFunction)
}, 1000 * STREAM_TIMEOUT_SEC)
_this.restartStream(callBackFunction)
}
//--------
stopStreamingTimer() {
clearInterval(this.streamingTimer)
}
//---------
startAnalyzeStream(callBackFunction) {
console.log("startAnalyzeStream")
let request = {
session: this.sessionPath,
queryInput: {
audioConfig: {
"audioEncoding": "AUDIO_ENCODING_LINEAR_16",
"sampleRateHertz": 16000,
"languageCode": "en-US",
},
singleUtterance: true
},
outputAudioConfig: {
audioEncoding: "OUTPUT_AUDIO_ENCODING_LINEAR_16",
sampleRateHertz: 16000,
languageCode: "en-US",
},
};
this.analyzeContentStream = this.sessionClient.streamingDetectIntent()
this.isWritingAudioStream = true
this.analyzeContentStream.on('data', (response) => {
if (response.recognitionResult) {
if (response.recognitionResult.messageType == 'END_OF_SINGLE_UTTERANCE'
|| response.recognitionResult.isFinal) {
this.stopStreamingTimer()
this.startStreamingTimer(callBackFunction)
}
}
else {
callBackFunction(response);
};
})
this.analyzeContentStream.on('error', (err) => {
console.error('-------');
console.error(err);
console.error('-------');
});
this.analyzeContentStream.on('finish', (res) => {
console.log('stream_closed');
});
this.analyzeContentStream.write(request);
return this.analyzeContentStream;
}
//---------
tryEndAudioStream() {
this.isWritingAudioStream = false;
if (this.analyzeContentStream) {
this.analyzeContentStream.end();
this.analyzeContentStream = null;
}
clearTimeout(this.streamingTimer);
}
//---------
restartStream(callBackFunction) {
this.tryEndAudioStream();
this.startAnalyzeStream(callBackFunction)
}
//---------
sendMessage(msg) {
if (this.isWritingAudioStream && this.analyzeContentStream) {
this.analyzeContentStream.write({ inputAudio: msg });
}
}
//---------
closeConversation() {
this.tryEndAudioStream()
}
}
//--------
module.exports = Conversation;