-
Notifications
You must be signed in to change notification settings - Fork 34
/
app2.js
250 lines (205 loc) · 7.79 KB
/
app2.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
function isChrome() {
var isChromium = window.chrome,
winNav = window.navigator,
vendorName = winNav.vendor,
isOpera = winNav.userAgent.indexOf("OPR") > -1,
isIEedge = winNav.userAgent.indexOf("Edge") > -1,
isIOSChrome = winNav.userAgent.match("CriOS");
if(isIOSChrome){
return true;
} else if(isChromium !== null && isChromium !== undefined && vendorName === "Google Inc." && isOpera == false && isIEedge == false) {
return true;
} else {
return false;
}
}
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function uuid() {
function randomDigit() {
if (crypto && crypto.getRandomValues) {
var rands = new Uint8Array(1);
crypto.getRandomValues(rands);
return (rands[0] % 16).toString(16);
} else {
return ((Math.random() * 16) | 0).toString(16);
}
}
var crypto = window.crypto || window.msCrypto;
return 'xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx'.replace(/x/g, randomDigit);
}
function gotoListeningState() {
const micListening = document.querySelector(".mic .listening");
const micReady = document.querySelector(".mic .ready");
micListening.style.display = "block";
micReady.style.display = "none";
}
function gotoReadyState() {
const micListening = document.querySelector(".mic .listening");
const micReady = document.querySelector(".mic .ready");
micListening.style.display = "none";
micReady.style.display = "block";
}
function addBotItem(text) {
const appContent = document.querySelector(".app-content");
appContent.innerHTML += '<div class="item-container item-container-bot"><div class="item"><p>' + text + '</p></div></div>';
appContent.scrollTop = appContent.scrollHeight; // scroll to bottom
}
function addUserItem(text) {
const appContent = document.querySelector(".app-content");
appContent.innerHTML += '<div class="item-container item-container-user"><div class="item"><p>' + text + '</p></div></div>';
appContent.scrollTop = appContent.scrollHeight; // scroll to bottom
}
function displayCurrentTime() {
const timeContent = document.querySelector(".time-indicator-content");
const d = new Date();
const s = d.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
timeContent.innerHTML = s;
}
function addError(text) {
addBotItem(text);
const footer = document.querySelector(".app-footer");
footer.style.display = "none";
}
document.addEventListener("DOMContentLoaded", function(event) {
var GoogleAuth;
var SCOPE = 'https://www.googleapis.com/auth/dialogflow';
gapi.load('client:auth2', function() {
gapi.client.init({
'apiKey': 'AIzaSyDb7zyMT5m3SYnXAGpK6ni4Jryfzhcp5OU',
'clientId': '150540862694-engp2c2vvd23c6mbeot0ivg8k15utr87.apps.googleusercontent.com',
'scope': SCOPE
}).then(function() {
GoogleAuth = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
GoogleAuth.isSignedIn.listen(updateSigninStatus);
// Handle initial sign-in state. (Determine if user is already signed in.)
var user = GoogleAuth.currentUser.get();
setSigninStatus();
});
});
// test for relevant API-s
// for (let api of ['speechSynthesis', 'webkitSpeechSynthesis', 'speechRecognition', 'webkitSpeechRecognition']) {
// console.log('api ' + api + " and if browser has it: " + (api in window));
// }
displayCurrentTime();
// check for Chrome
if (!isChrome()) {
addError("This demo only works in Google Chrome.");
return;
}
if (!('speechSynthesis' in window)) {
addError("Your browser doesn’t support speech synthesis. This demo won’t work.");
return;
}
if (!('webkitSpeechRecognition' in window)) {
addError("Your browser cannot record voice. This demo won’t work.");
return;
}
// Now we’ve established that the browser is Chrome with proper speech API-s.
// Initial feedback message.
addBotItem("Hi! I’m voicebot. Tap the microphone and start talking to me.");
var sessionId = uuid();
var recognition = new webkitSpeechRecognition();
var recognizedText = null;
recognition.continuous = false;
recognition.onstart = function() {
recognizedText = null;
};
recognition.onresult = function(ev) {
recognizedText = ev["results"][0][0]["transcript"];
addUserItem(recognizedText);
ga('send', 'event', 'Message', 'add', 'user');
gapi.client.request({
path: 'https://dialogflow.googleapis.com/v2/projects/jaanusbot-v2/agent/sessions/' + sessionId + ':detectIntent',
method: "POST",
body: {
"queryInput": {
"text": {
"text": recognizedText,
"languageCode": "en"
}
}
}
}).then(handleResponse, handleError);
function handleResponse(serverResponse) {
// Set a timer just in case. so if there was an error speaking or whatever, there will at least be a prompt to continue
var timer = window.setTimeout(function() { startListening(); }, 5000);
const speech = serverResponse["result"]["queryResult"]["fulfillmentText"];
var msg = new SpeechSynthesisUtterance(speech);
addBotItem(speech);
ga('send', 'event', 'Message', 'add', 'bot');
msg.addEventListener("end", function(ev) {
window.clearTimeout(timer);
startListening();
});
msg.addEventListener("error", function(ev) {
window.clearTimeout(timer);
startListening();
});
window.speechSynthesis.speak(msg);
}
function handleError(serverError) {
console.log("Error from DialogFlow server: ", serverError);
}
};
recognition.onerror = function(ev) {
console.log("Speech recognition error", ev);
};
recognition.onend = function() {
gotoReadyState();
};
function startListening() {
gotoListeningState();
recognition.start();
}
const startButton = document.querySelector("#start");
startButton.addEventListener("click", function(ev) {
ga('send', 'event', 'Button', 'click');
startListening();
ev.preventDefault();
});
// Esc key handler - cancel listening if pressed
// http://stackoverflow.com/questions/3369593/how-to-detect-escape-key-press-with-javascript-or-jquery
document.addEventListener("keydown", function(evt) {
evt = evt || window.event;
var isEscape = false;
if ("key" in evt) {
isEscape = (evt.key == "Escape" || evt.key == "Esc");
} else {
isEscape = (evt.keyCode == 27);
}
if (isEscape) {
recognition.abort();
}
});
// Google Authentication
document.querySelector(".sign-in-or-out-link").addEventListener("click", handleAuthClick);
function setSigninStatus(isSignedIn) {
var user = GoogleAuth.currentUser.get();
var isAuthorized = user.hasGrantedScopes(SCOPE);
if (isAuthorized) {
console.log("User signed in. Updating UI to reflect that");
var email = user.getBasicProfile().getEmail();
document.querySelector(".sign-in-or-out-label").innerHTML = 'Signed in as ' + email + ".";
document.querySelector(".sign-in-or-out-link").innerHTML = 'Sign out';
document.querySelector(".app-footer").classList.remove("not-signed-in");
} else {
console.log("User not signed in. Updating UI to reflect that.");
document.querySelector(".sign-in-or-out-label").innerHTML = 'Not signed in. You must sign in with Google to continue.';
document.querySelector(".sign-in-or-out-link").innerHTML = 'Sign in';
document.querySelector(".app-footer").classList.add("not-signed-in");
}
}
function updateSigninStatus(isSignedIn) {
setSigninStatus();
}
function handleAuthClick() {
if (GoogleAuth.isSignedIn.get()) {
// User is authorized and has clicked 'Sign out' button.
GoogleAuth.signOut();
} else {
// User is not signed in. Start Google auth flow.
GoogleAuth.signIn();
}
}
});