Nexchan commited on
Commit
90a2010
·
verified ·
1 Parent(s): 3fbc29d

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +41 -26
index.js CHANGED
@@ -181,6 +181,12 @@ app.get('/get/message', (req, res) => {
181
  });
182
 
183
 
 
 
 
 
 
 
184
  const db_chatHistory = {};
185
 
186
  function generateUID(length) {
@@ -232,16 +238,18 @@ cleanInactiveUsersV2();
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',
@@ -249,40 +257,46 @@ app.get('/v2/send', async (req, res) => {
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,
@@ -296,6 +310,7 @@ app.get('/v2/send', async (req, res) => {
296
  }
297
  });
298
 
 
299
  app.get('/v2/listuser', (req, res) => {
300
  const userList = Object.keys(db_chatHistory);
301
  res.status(200).json({ userList });
 
181
  });
182
 
183
 
184
+
185
+
186
+
187
+
188
+
189
+
190
  const db_chatHistory = {};
191
 
192
  function generateUID(length) {
 
238
  app.get('/v2/send', async (req, res) => {
239
  try {
240
  const { id, message } = req.query;
241
+ if (!id || !message) {
242
+ return res.status(400).json({
243
+ success: false,
244
+ response: "Input parameter id and message are required."
245
+ });
246
+ }
247
+
248
+ let chatData = db_chatHistory[id];
249
+
250
+ if (!chatData) {
251
+ // Jika data riwayat obrolan tidak ada, buat data baru
252
+ chatData = {
253
  lastChat: DateTime.local(),
254
  data: {
255
  botId: 'default',
 
257
  session: 'N/A',
258
  chatId: generateUID(11),
259
  contextId: 58,
260
+ messages: [],
 
 
 
 
 
 
261
  newMessage: '',
262
  newFileId: null,
263
  stream: false
264
  }
265
  };
266
  }
267
+ //set pesan baru dulu
268
+ chatData.data.newMessage = message;
269
+
270
+ const botResponse = await sendMessageV2(chatData.data);
271
+
272
+ // Simpan pesan user ke riwayat obrolan
273
+ chatData.data.messages.push({
274
  id: generateUID(10),
275
  role: 'user',
276
  content: message,
277
  who: 'User: ',
278
  timestamp: Date.now()
279
  });
280
+
281
+ // Kirim riwayat obrolan lengkap ke API
282
+
283
+ // Simpan bot response ke riwayat obrolan
284
+ chatData.data.messages.push({
285
  id: generateUID(10),
286
  role: 'assistant',
287
  content: botResponse.reply,
288
  who: 'AI: ',
289
  timestamp: Date.now()
290
  });
291
+
292
+ // Set pesan baru ke kosong setelah terkirim
293
+ chatData.data.newMessage = "";
294
+
295
+ // Update waktu terakhir obrolan
296
+ chatData.lastChat = DateTime.local();
297
+
298
+ // Simpan data riwayat obrolan ke dalam database
299
+ db_chatHistory[id] = chatData;
300
 
301
  res.status(200).json({
302
  success: true,
 
310
  }
311
  });
312
 
313
+
314
  app.get('/v2/listuser', (req, res) => {
315
  const userList = Object.keys(db_chatHistory);
316
  res.status(200).json({ userList });