-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.html
More file actions
220 lines (139 loc) · 5.21 KB
/
chatbot.html
File metadata and controls
220 lines (139 loc) · 5.21 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
<!DOCTYPE html>
<html>
<head>
<title>Chatbot</title>
<link rel="stylesheet" href="chatbot.css">
</head>
<body>
<div class="js-container"></div>
<script src="react-1.js"></script>
<script src="https://unpkg.com/supersimpledev/react.js"></script>
<script src="https://unpkg.com/supersimpledev/react-dom.js"></script>
<script src="https://unpkg.com/supersimpledev/chatbot.js"></script>
<script src="https://unpkg.com/supersimpledev/babel.js"></script>
<script type="text/babel">
// ChatInput Component
function ChatInput({ chatMessages,setChatMessages }){
const [inputText,setInputText] = React.useState('');
function saveInputText(event){
// event.target = gives us the element that we are typing in
const value = event.target.value;
setInputText(value);
}
function sendMessage(){
// creating a variable so that it can remember the value . state doesnt update immediately , it updated when all the code is finished. if we dont
// save the data , react will not update this and will update the response data only .
const newChatMessages = [
...chatMessages,
{
message : inputText,
sender : 'user',
id : crypto.randomUUID()
}
]
setChatMessages(newChatMessages);
const response = Chatbot.getResponse(inputText); // responses come from external library
setChatMessages([
...newChatMessages,
{
message : response,
sender : 'robot',
id : crypto.randomUUID()
}
]);
setInputText(''); // removes text in the input field after the send button clicked
}
return (
<div className="input-container">
<input
className = "chat-input"
type="text"
placeholder="Send a message to Chatbot"
size="30"
//onChange = runs a function when we changes the text inside an <input>
onChange = {saveInputText}
value = {inputText}
/>
<button
onClick = {sendMessage}
className = "send-button"
>Send</button>
</div>
);
}
function ChatMessage({ message, sender }){
return (
<div className = {sender==='user' ? 'chat-message-user' : 'chat-message-robot'}>
{sender==='robot' && (
<img src="robot.png" className="chat-message-profile" alt="robot" />
)}
<div className="chat-message-text">
{message}
</div>
{sender==='user' && (
<img src="user.png" className="chat-message-profile" alt="user" />
)}
</div>
);
}
// ChatMessages component
function ChatMessages({ chatMessages }){
const chatMessagesRef = React.useRef(null);
React.useEffect(() => {
const containerElem = chatMessagesRef.current;
if(containerElem){
containerElem.scrollTop = containerElem.scrollHeight;
}
},[chatMessages])
return(
<div className="chat-messages-container" ref={chatMessagesRef}>
{chatMessages.map((chatMessage) => {
return(
<ChatMessage
message = {chatMessage.message}
sender = {chatMessage.sender}
key = {chatMessage.id}
/>
);
})}
</div>
);
}
// main component
function App(){
//const array = React.useState([{
const [chatMessages,setChatMessages] = React.useState([{
message : 'hello chatbot' ,
sender : 'user',
id : crypto.randomUUID()
},{
message : 'Hello! How can I help you?' ,
sender : 'robot' ,
id : crypto.randomUUID()
},{
message : 'test',
sender : 'user' ,
id : crypto.randomUUID()
},{
message : 'Sorry, I didnt quite understand that. Currently, I only know how to flip a coin, roll a dice, or get todays date. Let me know how I can help!',
sender : 'robot',
id : crypto.randomUUID()
}]);
return(
<div className="app-container">
<ChatMessages
chatMessages = {chatMessages}
/>
<ChatInput
chatMessages = {chatMessages}
setChatMessages = {setChatMessages}
/>
</div>
);
}
const container = document.querySelector('.js-container');
ReactDOM.createRoot(container).render(<App />);
</script>
</body>
</html>
</html>