-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchatbot.html
55 lines (54 loc) · 1.64 KB
/
chatbot.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
<html>
<head>
<title>API Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
var accessToken = "cfeddcbb926b433cb0d754160ed9f1ed";
var baseUrl = "https://api.api.ai/v1/";
$(document).ready(function() {
$("#input").keypress(function(event) {
if (event.which == 13) {
$('.chatBox').append('<span class="userInput">' + 'me:'+ $('input').val() + '</span><br><br>')
event.preventDefault();
let query = $('input').val()
$('input').val('')
send(query);
}
});
});
function send(query) {
var text = query;
$.ajax({
type: "POST",
url: baseUrl + "query?v=20180101",
contentType: "application/json; charset=utf-8",
dataType: "json",
headers: {
"Authorization": "Bearer " + accessToken
},
data: JSON.stringify({ query: text, lang: "en", sessionId: "somerandomthing" }),
success: function(data) {
setResponse(data);
}
});
}
function setResponse(val) {
$(".chatBox").append('<span class="responseData">'+ 'bot:' + val.result.fulfillment.speech + '</span><br><br>');
}
</script>
<style type="text/css">
body { width: 500px; margin: 0 auto;height: 500px; border: 2px solid black;padding: 10px; }
div { width: 400px;height: 450px;overflow: auto }
.userInput{float: left;}
.responseData{float:right};
#input { width: 500px; }
button { width: 50px; }
</style>
</head>
<body>
<div class = 'chatBox'>
</div>
<input id="input" type="text" style="width: 430px;"> <button id="rec">send</button>
</body>
</html>