-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (202 loc) · 6.33 KB
/
index.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
// Chatbot.
(function () {
var button = document.getElementById('send');
var input = document.getElementById('message');
var output = document.getElementById('output-box');
var muted = false;
var startSpeaking = null;
var finishSpeaking = null;
var bot = new Bot.Bot();
Patterns.patterns.forEach(function (pattern) {
bot.learn(pattern);
});
function mute () {
muted = true;
}
function unmute () {
muted = false;
}
function say (words) {
try {
if (words === null)
words = 'null';
else if (words === undefined)
words = 'undefined';
else if (typeof words != 'string')
words = eval(words).toString();
words = words
.replaceAll('\'', '')
.replaceAll('\"', '')
.replaceAll('\n', '')
.replaceAll(' ', '')
.replaceAll(' ', '');
while (true) {
var open = words.indexOf('<');
var close = open >= 0 ? words.indexOf('>', open + 1) : -1;
if (open < 0 || close < 0)
break;
open = words.substr(0, open);
close = words.substr(close + 1);
words = open + ' ' + close;
}
setTimeout(function () {
if (typeof Sam != 'undefined') {
if (startSpeaking)
startSpeaking();
Sam.sam.say(words, function () {
if (finishSpeaking)
finishSpeaking()
});
}
}, 500);
} catch (ex) {
console.warn('Speech synthesis error:\n ' + ex.toString());
}
return words;
}
function send (req, think) {
if (!req)
req = input.value;
if (!think)
think = bot.think.bind(bot);
if (!req || req.length == 0)
return;
output.innerHTML += '<br>You: ' + req + '<br>';
output.scrollTop = output.scrollHeight;
input.value = '';
var words = '';
think(req, function (rsp) {
if (typeof Sam != 'undefined')
Sam.sam.setup();
if (req == 'mute')
mute();
else if (req == 'unmute')
unmute();
words += rsp + ' ';
rsp = rsp.replaceAll('\n', '<br>');
setTimeout(function () {
output.innerHTML += 'Bot: ' + rsp + '<br>';
output.scrollTop = output.scrollHeight;
}, 500);
});
if (!muted && typeof Sam != 'undefined')
say(words);
}
function clear () {
output.innerHTML = '';
return 'Okey.';
}
button.onclick = function () {
send();
};
input.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
send();
}
});
window.enableSpeechInput = function () {
var failed = function () {
var msg = 'Speech recognition is not supported by your browser.';
output.innerHTML += '<br>Bot: ' + msg + '.<br>';
output.scrollTop = output.scrollHeight;
say(msg);
};
var Recognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!Recognition) {
failed();
return;
}
try {
var recognition = new Recognition();
recognition.lang = 'en-US';
recognition.continuous = true;
recognition.interimResults = false;
recognition.maxAlternatives = 10;
var GrammerList = window.SpeechGrammarList || window.webkitSpeechGrammarList;
if (GrammerList) {
var KEYWORDS = [
'tony', 'toby', 'chengdu',
'contact', 'support'
];
var speechRecognitionList = new GrammerList();
var grmr = '';
KEYWORDS.forEach(function (val, i) {
grmr += val;
if (i != KEYWORDS.length - 1)
grmr += ' | ';
});
speechRecognitionList.addFromString(grmr, 1);
recognition.grammars = speechRecognitionList;
}
recognition.addEventListener('end', () => {
if (typeof Sam == 'undefined') {
try {
recognition.start();
console.log('Restart listening.');
} catch (_) {
}
}
});
recognition.addEventListener('result', function (event) {
for (var i = event.resultIndex; i < event.results.length; ++i) {
var identificated = event.results[i][0].transcript;
console.log('Got: ' + identificated);
send(identificated);
}
recognition.abort();
console.log('Stop listening.');
});
recognition.start();
console.log('Start listening.');
startSpeaking = function () {
recognition.abort();
console.log('Stop listening.');
};
finishSpeaking = function () {
try {
recognition.start();
console.log('Restart listening.');
} catch (_) {
}
};
output.innerHTML += '<br>Bot: I am lisening.<br>';
output.scrollTop = output.scrollHeight;
// speechSynthesis.speak(new SpeechSynthesisUtterance('I am listening.'));
// say('I am lisening.');
window.enableSpeechInput = function () {
output.innerHTML += '<br>Bot: I am lisening.<br>';
output.scrollTop = output.scrollHeight;
};
} catch (ex) {
failed();
console.warn('Speech recognition error:\n ' + ex.toString());
}
};
output.innerHTML += 'Tony: ';
output.innerHTML += 'You can chat with my agent bot here, ';
output.innerHTML += 'i.e. enter "How to contact Tony?" to get my email address, ';
output.innerHTML += 'or try whatever you want. ';
output.innerHTML += 'Click to <a class="link" onclick="enableSpeechInput()">enable speech input</a>. ';
output.innerHTML += 'Also click to <a href="message.html">leave a message</a>.<br>';
window.mute = mute;
window.unmute = unmute;
window.say = say;
window.clear = clear;
})();
// Show more buttons.
(function () {
function toggle (btnId, moreId) {
var more = document.getElementById(moreId);
var btn = document.getElementById(btnId);
btn.onclick = function () {
if (!more.style.display || more.style.display === 'inline-block') {
more.style.display = 'inline';
btn.innerHTML = 'Show less';
} else {
more.style.display = 'inline-block';
btn.innerHTML = 'Show more...';
}
};
}
toggle('toggle-tiny', 'more-tiny');
})();