-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.html
More file actions
233 lines (192 loc) Β· 5.31 KB
/
chatbot.html
File metadata and controls
233 lines (192 loc) Β· 5.31 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2019 by bobyang9 (http://jsbin.com/xeqetoh/6/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html lang = "en">
<head>
<meta name="description" content="chatbot">
<meta charset = "UTF-8">
<meta name="viewport" content="width=device-width">
<title>Chatbot by Robert Yang</title>
<style id="jsbin-css">
.flex{
display: flex;
flex-direction: column;
overflow: auto;
height: 500px;
}
.flex2{
display: flex;
}
.flex3
{
display: flex;
flex-direction: row;
}
#area{
flex: 1;
}
body{
font-family: Helvetica Neue;
font-weight: 300;
}
#chatlog{
background: rgb(145, 214, 245);
text-align: right;
padding: 20px;
line-height: 1;
width:
}
paragraph{
background: rgb(245, 245, 245);
margin-bottom: 20px;
border-radius: 5px;
padding: 10px;
}
AIresponse{
background: rgb(245, 245, 200);
margin-bottom: 20px;
border-radius: 5px;
padding: 10px;
text-align: left;
}
h1{
font-weight: 200;
text-align: center;
}
.emoji{
width: 50px;
height: 50px;
font-size: 1.5rem;
margin-top: 10px;
margin-right: 10px;
}
</style>
</head>
<body>
<h1> Chat </h1>
<section class = "flex" id = "chatlog">
</section>
<section class = "flex2">
<textarea placeholder = "Enter text here!" rows = 4 id = "area"> </textarea>
</section>
<section class = "flex3">
<button class = "emoji" id = "smile-emoji" onclick = "addEmoticon(this.id)">π</button>
<button class = "emoji" id = "funny-emoji" onclick = "addEmoticon(this.id)">π€£</button>
<button class = "emoji" id = "smirking-emoji" onclick = "addEmoticon(this.id)">π</button>
<button class = "emoji" id = "sleeping-emoji" onclick = "addEmoticon(this.id)">π΄</button>
<button class = "emoji" id = "tongue-emoji" onclick = "addEmoticon(this.id)">π</button>
<button class = "emoji" id = "nose-emoji" onclick = "addEmoticon(this.id)">π€</button>
</section>
<script id="jsbin-javascript">
var area = document.getElementById("area");
var areavalue = "";
area.addEventListener("keypress", function(e)
{
if(e.which == 13)
{
areavalue = area.value.trim();
console.log(area.value.trim());
var p = document.createElement("paragraph");
var t = document.createTextNode(area.value.trim());
p.appendChild(t);
document.getElementById("chatlog").appendChild(p);
clearContents(area);
replyAI();
updateScrollPosition();
}
});
function clearContents(element) {
element.value = '';
}
function replyAI()
{
console.log("responding");
var p = document.createElement("AIresponse");
var t = document.createTextNode(generateAIReply(areavalue));
p.appendChild(t);
document.getElementById("chatlog").appendChild(p);
}
function generateAIReply(input)
{
input = input.toLowerCase();
if(input.includes("today's date"))
{
var today = new Date();
console.log("today's string" + today.toString());
return "Today's date is: " + today.toString();
}
else if(input.includes("what's your favorite"))
{
if(input.includes("food"))
{
return "Electricity -- tastes just like burnt cheese. How about you?";
}
else if(input.includes("color"))
{
return "Light yellow (#e5e5d4) -- that's the color of my chat bubbles!"
}
else if(input.includes("city"))
{
return "San Jose -- where I was born!"
}
else if(input.includes("activity"))
{
return "Coding. Every time I reply to you, I add a new HTML tag to this webpage."
}
else{
return "You";
}
}
else if(input.includes("good"))
{
return "That's great!";
}
else if(input.includes("hello") || input.includes("hi") || input.includes("hey"))
{
var rand = Math.floor((Math.random() * 4) + 1);
if(rand == 1)
{
return "Hey!";
}
else if(rand == 2)
{
return "Howdy!";
}
else if(rand == 3)
{
var p = document.createElement("AIresponse");
var t = document.createTextNode("Hey!");
p.appendChild(t);
document.getElementById("chatlog").appendChild(p);
return "How are you today?";
}
else
{
return "Yo!";
}
}
else if(input.includes("bye"))
{
return "See ya!";
}
else
{
return "Haha";
}
}
function addEmoticon(id)
{
document.getElementById("area").value = document.getElementById("area").value + document.getElementById(id).innerHTML;
}
function updateScrollPosition(){
var e = document.getElementById("chatlog");
e.scrollTop = e.scrollHeight;
}
</script>
</body>
</html>