Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -181,6 +181,137 @@ app.get('/get/message', (req, res) => {
|
|
181 |
});
|
182 |
|
183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
app.listen(port, () => {
|
185 |
console.log(`Server is running on port ${port}`);
|
186 |
});
|
|
|
181 |
});
|
182 |
|
183 |
|
184 |
+
const db_chatHistory = {};
|
185 |
+
|
186 |
+
function generateUID(length) {
|
187 |
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
188 |
+
const charactersLength = characters.length;
|
189 |
+
let uid = '';
|
190 |
+
for (let i = 0; i < length; i++) {
|
191 |
+
uid += characters.charAt(Math.floor(Math.random() * charactersLength));
|
192 |
+
}
|
193 |
+
return uid;
|
194 |
+
}
|
195 |
+
|
196 |
+
async function sendMessageV2(data) {
|
197 |
+
try {
|
198 |
+
const response = await axios.post('https://chat.chatgptdemo.net/chat_api_stream', {
|
199 |
+
data
|
200 |
+
}, {
|
201 |
+
headers: {
|
202 |
+
'Content-Type': 'application/json',
|
203 |
+
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
|
204 |
+
'X-Forwarded-For': generateRandomIP()
|
205 |
+
}
|
206 |
+
});
|
207 |
+
return response.data;
|
208 |
+
} catch (error) {
|
209 |
+
console.error(error);
|
210 |
+
}
|
211 |
+
}
|
212 |
+
|
213 |
+
async function cleanInactiveUsersV2() {
|
214 |
+
const currentTime = DateTime.local();
|
215 |
+
|
216 |
+
Object.keys(db_chatHistory).forEach((userId) => {
|
217 |
+
const user = db_chatHistory[userId];
|
218 |
+
|
219 |
+
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
220 |
+
// User's last message was more than 10 minutes ago, remove the user from db_chatHistory
|
221 |
+
delete db_chatHistory[userId];
|
222 |
+
}
|
223 |
+
});
|
224 |
+
|
225 |
+
// Schedule the next execution after 1 minute
|
226 |
+
setTimeout(cleanInactiveUsersV2, 180000);
|
227 |
+
}
|
228 |
+
|
229 |
+
// Start the continuous execution of cleanInactiveUsers
|
230 |
+
cleanInactiveUsersV2();
|
231 |
+
|
232 |
+
app.get('/v2/send', async (req, res) => {
|
233 |
+
try {
|
234 |
+
const { id, message } = req.query;
|
235 |
+
if (!id) return res.status(400).json({
|
236 |
+
success: false,
|
237 |
+
response: "input param id & input id user"
|
238 |
+
});
|
239 |
+
if (!message) return res.status(400).json({
|
240 |
+
success: false,
|
241 |
+
response: "input param message & input a message from user"
|
242 |
+
});
|
243 |
+
if (!db_chatHistory[id]) {
|
244 |
+
db_chatHistory[id] = {
|
245 |
+
lastChat: DateTime.local(),
|
246 |
+
data: {
|
247 |
+
botId: 'default',
|
248 |
+
customId: null,
|
249 |
+
session: 'N/A',
|
250 |
+
chatId: generateUID(11),
|
251 |
+
contextId: 58,
|
252 |
+
messages: [{
|
253 |
+
id: generateUID(10),
|
254 |
+
role: 'assistant',
|
255 |
+
content: 'Hi, How can I help you?',
|
256 |
+
who: 'AI: ',
|
257 |
+
timestamp: Date.now() - (4 * 60 * 1000)
|
258 |
+
}],
|
259 |
+
newMessage: '',
|
260 |
+
newFileId: null,
|
261 |
+
stream: false
|
262 |
+
}
|
263 |
+
};
|
264 |
+
}
|
265 |
+
db_chatHistory[id].lastChat = DateTime.local();
|
266 |
+
db_chatHistory[id].data.newMessage = message;
|
267 |
+
const botResponse = await sendMessageV2(db_chatHistory[id].data);
|
268 |
+
//setelah terkirim simpan pesan user
|
269 |
+
db_chatHistory[id].data.messages.push({
|
270 |
+
id: generateUID(10),
|
271 |
+
role: 'user',
|
272 |
+
content: message,
|
273 |
+
who: 'User: ',
|
274 |
+
timestamp: Date.now()
|
275 |
+
});
|
276 |
+
db_chatHistory[id].data.newMessage = ""; //set ke semula sesudah post
|
277 |
+
//save pesan bot juga
|
278 |
+
db_chatHistory[id].data.messages.push({
|
279 |
+
id: generateUID(10),
|
280 |
+
role: 'assistant',
|
281 |
+
content: botResponse.reply,
|
282 |
+
who: 'AI: ',
|
283 |
+
timestamp: Date.now()
|
284 |
+
});
|
285 |
+
db_chatHistory[id].lastChat = DateTime.local();
|
286 |
+
|
287 |
+
res.status(200).json({
|
288 |
+
success: true,
|
289 |
+
response: botResponse.reply
|
290 |
+
});
|
291 |
+
} catch (e) {
|
292 |
+
res.status(400).json({
|
293 |
+
success: false,
|
294 |
+
response: format(e)
|
295 |
+
});
|
296 |
+
}
|
297 |
+
});
|
298 |
+
|
299 |
+
app.get('/v2/listuser', (req, res) => {
|
300 |
+
const userList = Object.keys(db_chatHistory);
|
301 |
+
res.status(200).json({ userList });
|
302 |
+
});
|
303 |
+
|
304 |
+
app.get('/v2/get/message', (req, res) => {
|
305 |
+
const id = req.query.id;
|
306 |
+
if (!id) return res.status(400).json({ success: false, response: "input id" });
|
307 |
+
if (!db_chatHistory[id]) {
|
308 |
+
return res.status(404).json({ error: 'User not found' });
|
309 |
+
}
|
310 |
+
const userMessages = db_chatHistory[id].data.messages;
|
311 |
+
res.status(200).json({ messages: userMessages });
|
312 |
+
});
|
313 |
+
|
314 |
+
|
315 |
app.listen(port, () => {
|
316 |
console.log(`Server is running on port ${port}`);
|
317 |
});
|