forked from MattNikonorov/ChatBot_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 11
/
bot.html
102 lines (60 loc) · 1.89 KB
/
bot.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
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans:400,600" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Chatbot</h1>
<div id="chatbox">
</div>
<span class="loader" id="loader"></span>
<div id="ib">
<input id="userInput" type="text">
<button onclick="sendMessage()" id="send">Send</button>
</div>
<script>
async function sendMessage() {
try{
var pm1 = document.getElementsByClassName("p1")
var pmv = pm1[pm1.length-1].innerHTML
}
catch{
var pmv = "none"
}
const userInput = document.getElementById('userInput');
const message = userInput.value;
userInput.value = '';
document.getElementById('chatbox').innerHTML += `<p id="p1" class="p1">` + message + `</p>`;
document.getElementById('loader').style.display = "block"
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'message': message,
'pm': pmv
})
});
const responseData = await response.json();
document.getElementById('chatbox').innerHTML += `<p id="p2">` + responseData.message + `</p>`;
document.getElementById('loader').style.display = "none"
var objDiv = document.getElementById("chatbox");
objDiv.scrollTop = objDiv.scrollHeight;
}
</script>
<script>
const node = document.getElementById("userInput");
node.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
sendMessage()
}
});
</script>
</body>
</html>