-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai-text-bot.php
271 lines (220 loc) · 9.02 KB
/
ai-text-bot.php
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
require_once 'vendor/autoload.php';
use App\Service\ChatGPT\Client;
$config = require_once 'config/config.php';
$apiKey = $config['apiKey'];
$chatGPTService = new Client($apiKey);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$userInput = $_POST['user_input'] ?? '';
// Process the user input
try {
$response = $chatGPTService->getTextResponse($userInput);
} catch (Exception $e) {
http_response_code(400);
$response = $e->getMessage();
}
if ($response == '--') {
http_response_code(400);
$response = 'Sorry, I could\'t understand that.';
}
// Output the response
echo json_encode(['response' => $response]);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Text Bot</title>
<!-- Bootstrap CSS via CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS for styling enhancements -->
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
}
#chat-log {
padding: 15px;
border: 1px solid #ccc;
border-radius: 8px;
margin-bottom: 15px;
min-height: 200px;
}
.message {
display: flex;
padding: 10px;
border-radius: 8px;
margin-bottom: 10px;
max-width: 100%;
}
.user-message {
align-self: flex-end;
max-width: 70%;
}
.bot-message {
align-self: flex-start;
max-width: 70%;
}
#user-input {
display: flex;
}
#user-input-text {
flex: 1;
margin-right: 10px;
border: 1px solid #b5b5b5;
border-radius: 8px;
}
#user-input-text[disabled] {
cursor: not-allowed;
}
#loading-message {
display: none;
margin-top: 10px;
font-size: 14px;
}
#start-over-button {
position: fixed;
top: 10px;
right: 10px;
z-index: 1000; /* Ensure it's above other elements */
}
.sticky-btn {
position: fixed;
left: 10px;
top: 10px;
z-index: 1000;
}
</style>
</head>
<body>
<div class="container col-8 mt-5" id="chat-container">
<h1 class="mb-4">AI Text Bot</h1>
<div id="chat-log" class="bg-light"></div>
<div id="loading-message" style="display: none;">
<div class="d-flex justify-content-center">
<div class="spinner-border" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
<div class="sticky-bottom bg-light">
<div id="user-input" class="my-2">
<div class="input-group">
<label for="user-input-text"></label><input type="text" id="user-input-text"
class="form-control form-control-lg"
placeholder="Type your message...">
<button class="btn btn-primary btn-lg" onclick="sendMessage()">Send</button>
</div>
</div>
</div>
</div>
<button id="start-over-button" class="btn btn-warning" onclick="startOver()">Start Over</button>
<a class="btn btn-primary btn-sm sticky-btn" href="index.php">Back to Main</a>
<!-- Bootstrap JS and Popper.js via CDN (required for Bootstrap components) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Get the input field
let userInput;
userInput = document.getElementById('user-input-text');
// Execute a function when the user presses a key on the keyboard
userInput.addEventListener("keypress", function (event) {
// If the user presses the "Enter" key on the keyboard
if (event.key === "Enter") {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the sendMessage function
sendMessage();
}
});
function sendMessage() {
const userInput = document.getElementById('user-input-text');
const userInputText = userInput.value;
const chatLog = document.getElementById('chat-log');
if (typeof userInputText === "string" && userInputText.length === 0) {
alert('Please type the instructions to generate image.');
return;
}
// Add user message to the chat log
chatLog.innerHTML += `<div class="d-flex justify-content-end"><div class="message user-message bg-primary text-white mb-2 p-2 rounded">${userInputText}</div></div>`;
// Add a placeholder message to the chat log
chatLog.innerHTML += `<div class="d-flex justify-content-start placeholder-glow"><div class="placeholder message bot-message mb-2 p-2 rounded w-75"><span class="text-light p-2"><strong>Bot is typing...</strong></span></div></div>`;
chatLog.scrollTop = chatLog.scrollHeight; // Scroll to the bottom of the chat log
// Disable the input field and send button
userInput.disabled = true;
document.querySelector('button').disabled = true;
// Make an AJAX request to the server
const xhr = new XMLHttpRequest();
xhr.open('POST', 'ai-text-bot.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
// Remove the placeholder message
const placeholderMessage = chatLog.querySelector('.placeholder-glow');
if (placeholderMessage) {
chatLog.removeChild(placeholderMessage);
}
// Enable the input field and send button
userInput.disabled = false;
document.querySelector('button').disabled = false;
if (xhr.status === 200) {
// Successful response
const response = JSON.parse(xhr.responseText);
// preparing chat log message
const messageContent = document.createElement('div');
const classList = ['message', 'bot-message', 'bg-dark', 'text-light', 'mb-2', 'p-2', 'rounded', 'w-100'];
messageContent.classList.add(...classList);
if (isCode(response.response)) {
// If the response is code-like, display as a code block
const codeBlock = document.createElement('pre');
const codeElement = document.createElement('code');
codeElement.innerText = response.response;
codeBlock.appendChild(codeElement);
messageContent.appendChild(codeBlock);
} else {
messageContent.innerText = response.response;
}
chatLog.innerHTML += `
<div class="d-flex justify-content-start">
${messageContent.outerHTML}
</div>`;
// Empty user input field after receiving a response
userInput.value = '';
// Focus on the user input field after receiving a response
userInput.focus();
} else {
// Handle error response
const errorMessage = 'Error communicating with the server. Please try again.';
chatLog.innerHTML += `<div class="d-flex justify-content-start"><div class="message bg-danger text-light mb-2 p-2 rounded">${errorMessage}</div></div>`;
}
// scroll to last chat log
chatLog.scrollTop = chatLog.scrollHeight;
}
};
xhr.send(`user_input=${encodeURIComponent(userInputText)}`);
}
function startOver() {
window.location.reload();
}
// Function to check if the response is code-like
function isCode(response) {
// Simple check for code-like content using regular expressions
const codePatterns = [
/^\s*function\s*\([^\)]*\)\s*{/i, // JavaScript function
/^\s*class\s+[A-Za-z_][A-Za-z0-9_]*\s*{/i, // JavaScript class
/<[^>]+>/, // HTML tags
/^\s*#/ // Python comments (you can add more patterns as needed)
];
return codePatterns.some(pattern => pattern.test(response));
}
function nl2br(str, replaceMode, isXhtml) {
const breakTag = (isXhtml) ? '<br />' : '<br>';
const replaceStr = (replaceMode) ? '$1' + breakTag : '$1' + breakTag + '$2';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, replaceStr);
}
</script>
</body>
</html>