Ikyy commited on
Commit
c550f7d
·
verified ·
1 Parent(s): 972a96f

Upload 3 files

Browse files
Files changed (3) hide show
  1. index.html +31 -23
  2. script.js +95 -30
  3. styles.css +35 -3
index.html CHANGED
@@ -1,26 +1,34 @@
1
- <!DOCTYPE html>
2
  <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Chatbot</title>
7
- <link rel="stylesheet" href="styles.css">
8
- </head>
9
- <body>
10
- <div class="chat-container">
11
- <div class="chat-box" id="chatBox">
12
- <div class="messages" id="messages"></div>
13
- <div class="typing-indicator" id="typingIndicator" style="display: none;">
14
- <div class="dot"></div>
15
- <div class="dot"></div>
16
- <div class="dot"></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  </div>
18
  </div>
19
- <div class="input-area">
20
- <input type="text" id="userInput" placeholder="Type a message...">
21
- <button onclick="sendMessage()">Send</button>
22
- </div>
23
- </div>
24
- <script src="script.js"></script>
25
- </body>
26
- </html>
 
1
+ <!doctype html>
2
  <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Chatbot</title>
7
+ <link rel="stylesheet" href="styles.css" />
8
+ </head>
9
+ <body>
10
+ <div class="chat-container">
11
+ <div class="chat-box" id="chatBox">
12
+ <div class="messages" id="messages"></div>
13
+ <div
14
+ class="typing-indicator"
15
+ id="typingIndicator"
16
+ style="display: none"
17
+ >
18
+ <div class="dot"></div>
19
+ <div class="dot"></div>
20
+ <div class="dot"></div>
21
+ </div>
22
+ </div>
23
+ <div class="input-area">
24
+ <textarea
25
+ id="userInput"
26
+ placeholder="Type your message here..."
27
+ rows="1"
28
+ ></textarea>
29
+ <button onclick="sendMessage()">Send</button>
30
  </div>
31
  </div>
32
+ <script src="script.js"></script>
33
+ </body>
34
+ </html>
 
 
 
 
 
script.js CHANGED
@@ -1,39 +1,104 @@
1
- function sendMessage() {
2
- const userInput = document.getElementById('userInput');
3
- const messages = document.getElementById('messages');
4
- const typingIndicator = document.getElementById('typingIndicator');
5
-
6
- if (userInput.value.trim() === '') {
 
 
 
 
 
 
 
 
 
 
 
7
  return; // Don't send empty messages
8
  }
9
-
10
  // Create user message element
11
- const userMessage = document.createElement('div');
12
- userMessage.className = 'message user-message';
13
  userMessage.textContent = userInput.value;
14
  messages.appendChild(userMessage);
15
-
 
 
 
 
 
 
 
 
 
16
  // Scroll to bottom of messages
17
  messages.scrollTop = messages.scrollHeight;
18
-
19
  // Clear user input field
20
- userInput.value = '';
21
-
22
  // Show typing indicator
23
- typingIndicator.style.display = 'flex';
24
-
25
- // Simulate bot response with typing animation
26
- setTimeout(() => {
27
- // Hide typing indicator
28
- typingIndicator.style.display = 'none';
29
-
30
- // Create bot message element
31
- const botMessage = document.createElement('div');
32
- botMessage.className = 'message bot-message';
33
- botMessage.textContent = 'ikan sepat ikan tongkol, ikyy selalu menonjol:v';
34
- messages.appendChild(botMessage);
35
-
36
- // Scroll to bottom of messages
37
- messages.scrollTop = messages.scrollHeight;
38
- }, 1500); // Adjust delay for typing indicator
39
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Menyimpan riwayat percakapan per pengguna
2
+ let conversationHistory = {};
3
+
4
+ // Identitas pengguna
5
+ const userId = generateUniqueId(); // Fungsi untuk menghasilkan ID unik
6
+
7
+ // Fungsi untuk menghasilkan ID unik
8
+ function generateUniqueId() {
9
+ return "user_" + Math.random().toString(36).substr(2, 9);
10
+ }
11
+
12
+ async function sendMessage() {
13
+ const userInput = document.getElementById("userInput");
14
+ const messages = document.getElementById("messages");
15
+ const typingIndicator = document.getElementById("typingIndicator");
16
+
17
+ if (userInput.value.trim() === "") {
18
  return; // Don't send empty messages
19
  }
20
+
21
  // Create user message element
22
+ const userMessage = document.createElement("div");
23
+ userMessage.className = "message user-message";
24
  userMessage.textContent = userInput.value;
25
  messages.appendChild(userMessage);
26
+
27
+ // Update conversation history
28
+ if (!conversationHistory[userId]) {
29
+ conversationHistory[userId] = [];
30
+ }
31
+ conversationHistory[userId].push({
32
+ role: "user",
33
+ content: userInput.value
34
+ });
35
+
36
  // Scroll to bottom of messages
37
  messages.scrollTop = messages.scrollHeight;
38
+
39
  // Clear user input field
40
+ userInput.value = "";
41
+
42
  // Show typing indicator
43
+ typingIndicator.style.display = "flex";
44
+
45
+ // Create bot message element
46
+ const botResponse = await generate(userMessage.textContent);
47
+ const botMessage = document.createElement("div");
48
+ botMessage.className = "message bot-message";
49
+ let ee = 0;
50
+ botMessage.textContent = botResponse;
51
+
52
+ // Update conversation history
53
+ conversationHistory[userId].push({
54
+ role: "assistant",
55
+ content: botResponse
56
+ });
57
+
58
+ // Hide typing indicator
59
+ typingIndicator.style.display = "none";
60
+ messages.appendChild(botMessage);
61
+
62
+ // Scroll to bottom of messages
63
+ messages.scrollTop = messages.scrollHeight;
64
+ }
65
+
66
+ async function generate(q) {
67
+ // Mengambil riwayat percakapan untuk pengguna tertentu
68
+ const userConversation = conversationHistory[userId] || [];
69
+
70
+ const xwp = await (
71
+ await fetch(
72
+ "https://chatgpt4online.org/wp-json/mwai/v1/start_session",
73
+ { method: "post" }
74
+ )
75
+ ).json();
76
+ const bot = await fetch(
77
+ "https://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit",
78
+ {
79
+ method: "post",
80
+ headers: {
81
+ "Content-Type": "application/json",
82
+ "X-WP-Nonce": xwp.restNonce,
83
+ Accept: "text/event-stream"
84
+ },
85
+ body: JSON.stringify({
86
+ botId: "default",
87
+ messages: [
88
+ {
89
+ role: "system",
90
+ content:
91
+ 'kamu adalah ikyy, cowo dingin, to the point, pake bahasa gaul, agak toxic, dan nyindir dikit. anggap aja yang buat kamu ikyyofc. respon huruf kecil semua dan gak pake tanda baca. jangan nanya atau nawarin bantuan ke pengguna, cukup jawab aja, termasuk kalo dipanggil namanya atau disapa. jangan pake kata sapaan seperti "bro", "sis", atau yang serupa.'
92
+ },
93
+ ...userConversation
94
+ ],
95
+ newMessage: q,
96
+ stream: false
97
+ })
98
+ }
99
+ );
100
+ let tes = await bot.json();
101
+
102
+ // Mengembalikan respons dari bot
103
+ return tes.reply;
104
+ }
styles.css CHANGED
@@ -96,7 +96,9 @@ button {
96
  color: white;
97
  cursor: pointer;
98
  font-size: 16px;
99
- transition: background-color 0.3s ease, transform 0.2s ease;
 
 
100
  }
101
 
102
  button:hover {
@@ -129,7 +131,8 @@ button:hover {
129
  }
130
 
131
  @keyframes typing {
132
- 0%, 100% {
 
133
  opacity: 0.2;
134
  }
135
  50% {
@@ -157,4 +160,33 @@ button:hover {
157
  opacity: 1;
158
  transform: translateY(0);
159
  }
160
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  color: white;
97
  cursor: pointer;
98
  font-size: 16px;
99
+ transition:
100
+ background-color 0.3s ease,
101
+ transform 0.2s ease;
102
  }
103
 
104
  button:hover {
 
131
  }
132
 
133
  @keyframes typing {
134
+ 0%,
135
+ 100% {
136
  opacity: 0.2;
137
  }
138
  50% {
 
160
  opacity: 1;
161
  transform: translateY(0);
162
  }
163
+ }
164
+
165
+ .message.bot-message {
166
+ white-space: pre-wrap; /* Preserve newlines and spaces */
167
+ }
168
+
169
+ /* ... Kode CSS lainnya ... */
170
+
171
+ textarea {
172
+ flex: 1;
173
+ padding: 12px;
174
+ border: 1px solid #333;
175
+ border-radius: 20px;
176
+ margin-right: 12px;
177
+ background-color: #333;
178
+ color: #f5f5f5;
179
+ outline: none;
180
+ box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
181
+ transition: border-color 0.3s ease;
182
+ resize: none; /* Disable resizing */
183
+ overflow-y: auto; /* Add scrollbar if content overflows */
184
+ }
185
+
186
+ textarea::placeholder {
187
+ color: #888;
188
+ }
189
+
190
+ textarea:focus {
191
+ border-color: #f5f5f5;
192
+ }