-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.html
executable file
·94 lines (89 loc) · 3.13 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
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
<!DOCTYPE html>
<html>
<head>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style>
h1 {
margin-top: 10px !important;
}
#chat {
height: 500px;
width: 500px;
border: 1px solid #ced4da;
overflow-y: scroll;
margin: 10px auto;
}
.chat-message {
float: left;
}
#message_input {
height: 30px;
width: 500px;
padding-left: 10px;
display: block;
margin: 10px auto;
border: 1px solid #ced4da
}
</style>
</head>
<body>
<div class="container-fluid">
<h1 class="text-center">ChatBot</h1>
<div class="row">
<div id="chat"></div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="text-center">
<div class="input">
<input id="message_input" placeholder="what's your name?"></input>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="text-center">
<button type="button" id="quit" class="btn btn-danger">Quit</button>
</div>
</div>
</div>
<script>
var chat_window = document.getElementById("chat");
var message_input = document.getElementById("message_input");
//create new websocket, bind to server on this localhost port
var ws = new WebSocket("ws://127.0.0.1:5678");
// Add new message
ws.onmessage = function (message) {
chat_window.innerHTML += ('<p class="col chat-message">' + message.data + '</p>');
chat_window.scrollTop = chat_window.scrollHeight;
};
// send message on enter
message_input.onkeyup = function (event) {
if (event.keyCode == 13) {
if (message_input.placeholder == "what's your name?") {
message_input.placeholder = "ask or tell me something...";
}
ws.send(message_input.value);
//reset
message_input.value = "";
}
};
//close websocket after hitting click
var quit = document.getElementById("quit");
quit.onclick = function () {
if (ws.readyState === WebSocket.OPEN) {
if (document.getElementsByClassName("chat-message").length === 0) {
chat_window.innerHTML += ('<p class="col chat-message"><b>Barnard:</b>I was not given a chance :(</p>');
} else {
chat_window.innerHTML += ('<p class="col chat-message"><b>Barnard:</b> Nice chatting with you!</p>');
chat_window.scrollTop = chat_window.scrollHeight;
}
ws.close();
}
}
</script>
</div>
</body>
</html>