-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
145 lines (120 loc) · 3.36 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BYOS (Build Your Own Siri) - API.AI</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/cover.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
// copy/paste: https://gist.github.com/ystrot/e799f338ab9849c90b04
var accessToken = "YOUR ACCESS TOKEN HERE";
var subscriptionKey = "YOUR SUBSCRIPTION TOKEN HERE";
var baseUrl = "https://api.api.ai/v1/";
$(document).ready(function() {
$("#input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
send();
}
});
$("#rec").click(function(event) {
switchRecognition();
});
});
var recognition;
function startRecognition() {
recognition = new webkitSpeechRecognition();
recognition.onstart = function(event) {
updateRec();
};
recognition.onresult = function(event) {
var text = "";
for (var i = event.resultIndex; i < event.results.length; ++i) {
text += event.results[i][0].transcript;
}
setInput(text);
stopRecognition();
};
recognition.onend = function() {
stopRecognition();
};
recognition.lang = "en-US";
recognition.start();
}
function stopRecognition() {
if (recognition) {
recognition.stop();
recognition = null;
}
updateRec();
}
function switchRecognition() {
if (recognition) {
stopRecognition();
} else {
startRecognition();
}
}
function setInput(text) {
$("#input").val(text);
send();
}
function updateRec() {
$("#rec").prop('disabled',(recognition ? true: false));
}
function send() {
var text = $("#input").val();
$.ajax({
type: "POST",
url: baseUrl + "query/",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken,
"ocp-apim-subscription-key": subscriptionKey
},
data: JSON.stringify({ q: text, lang: "en" }),
success: function(data) {
$("#debug").text(JSON.stringify(data, undefined, 2));
lookup(data);
},
error: function() {
$("#debug").text("Internal Server Error");
}
});
}
function lookup(api_ai_output) {
$.ajax({
type: "POST",
url: "lookup.php",
data: {q: JSON.stringify(api_ai_output)},
success: function(data) {
$("#output").text(JSON.stringify(data, undefined, 2));
},
error: function() {
$("#output").text("Internal Server Error");
}
});
}
</script>
</head>
<body>
<div class="container">
<button type="button" class="btn btn-success btn-circle btn-xl" id="rec">
<img src="assets/images/record.png" width="200" style="outline:none;"/>
</button>
<br />
<br />
<br />
<h2>BYOS input</h2>
<input class="form-control input-lg" type="text" placeholder="" id="input">
<h2>BYOS ouput</h2>
<textarea class="form-control input-lg" id="output" cols="40" rows="15"></textarea>
<h2>Debug info</h2>
<textarea class="form-control input-lg" id="debug" cols="40" rows="15"></textarea>
</div>
</body>
</html>