-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_test.js
148 lines (117 loc) · 3.67 KB
/
engine_test.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
let dotenv = require('dotenv')
dotenv.config()
const vosk = require('./callbot_vosk');
const engine = require('./engine');
const audio = require('./audio');
var notifier = require('./notifier')
//var audio_transcription;
var audio_transcription_o;
var audio_transcription_m;
var audio_transcription_mo;
var action_o;
var silence_ctr = undefined;
//var audio_is_playing;
var audio_is_playing_o;
var script_step = process.env.SCRIPT_STEP;
var processInterval;
//startInterval();
var trackerTimeout;
var audio_is_playing = false;
var caller;
var step_is_running = false;
notifier.on('audio-finished-playing', () => {
console.log('audio finished playing');
audio_is_playing = false;
stepFunction();
});
notifier.on('audio-started-playing', () => {
console.log('audio started playing');
audio_is_playing = true;
});
notifier.on('back-one-step', () => {
console.log('back one level');
script_step -= 1;
//stepFunction();
});
notifier.on('end-call', () => {
console.log('end call');
script_step -= 1;
});
notifier.on('transfer-call', () => {
console.log('success, transfer call...');
//script_step -=1;
});
stepFunction();
//function startInterval() {
//processInterval =
//try {
function stepFunction() {
//setTimeout(() => {
//timestamp = Date.now();
audio_is_playing = audio.audioIsPlaying();
// if (audio_is_playing) return;
//var play_audio_command = false;
var action = engine.getAction();
if (action != action_o) {
if (action == 'back-one') {
script_step -= 1;
// console.log("step started");
}
// console.log(action);
// console.log(action_o);
}
var audio_transcription = audio_is_playing == false ? vosk.audioTranscription() : undefined;
if (audio_transcription == audio_transcription_o) {
audio_transcription = undefined
};
//get audio transcription
if (audio_transcription == undefined) silence_ctr++;
if (audio_transcription) {
console.log('call active, user says: ', audio_transcription);
audio_transcription_m = audio_transcription;
silence_ctr = 0;
}
else {
if (silence_ctr >= 2 && audio_transcription_m) {
//play audio file
if (audio_is_playing != true) {
//play_audio_command = true;
// engine.evaluateResponse(script_step, audio_transcription_m);
console.log(process.env.SCRIPT_STEP);
engine.evaluateResponse(script_step, audio_transcription_m);
audio_transcription_m = undefined;
script_step++;
setEnvKey("SCRIPT_STEP", script_step);
}
}
}
if (audio_is_playing != audio_is_playing_o) {
if (audio_is_playing == false) {
//script_step++;
}
}
if (audio_transcription) audio_transcription_o = audio_transcription;
if (audio_transcription_m) audio_transcription_mo = audio_transcription_m;
audio_is_playing_o = audio_is_playing;
//console.log('step .99');
setTimeout(() => {
stepFunction();
}, 500);
}
const fs = require("fs");
const os = require("os");
// don't forget to install PM2 globally
function setEnvKey(key, value) {
const ENV_VARS = fs.readFileSync("./.env", "utf8").split(os.EOL);
const target = ENV_VARS.indexOf(ENV_VARS.find((line) => {
return line.match(new RegExp(key));
}));
ENV_VARS.splice(target, 1, `${key}=${value}`);
fs.writeFileSync("./.env", ENV_VARS.join(os.EOL));
}
process.on('unhandledRejection', (reason, promise) => {
console.log(reason)
})
process.on('uncaughtException', (reason) => {
console.log(reason)
})