forked from BraydenRoyston/hackthe6ix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeechrecognitionbanking.js
55 lines (44 loc) · 1.74 KB
/
speechrecognitionbanking.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
function speechToText() {
var message = document.querySelector("#message");
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var grammar = "#JSGF V1.0;";
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.onresult = function (event) {
var current = event.results.length - 1;
var command = event.results[current][0].transcript;
//'command' contains the string inputted by the user
message.textContent = "Voice Input: " + command;
setTimeout(function () {
var request = new XMLHttpRequest();
request.open("POST", "http://localhost:1337/sms");
request.setRequestHeader("phone", window.localStorage.getItem('phoneNumber'));
request.setRequestHeader("voice", "voice");
request.setRequestHeader("command", command);
request.send();
request.onreadystatechange = function () {
if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
console.log("Request was successful.");
} else {
console.log("Request was unsuccessful." + request.responseText);
}
};
console.log(command);
}, 6000);
};
recognition.onspeechend = function () {
recognition.stop();
};
recognition.onerror = function (event) {
message.textContent = "Error occurred in recognition: " + event.error;
};
document.querySelector("#btnCommand").addEventListener("click", function () {
recognition.start();
});
}
speechToText();