Create webChat.js
Browse files- routes/webChat.js +34 -0
routes/webChat.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// C:\Users\Hi\OneDrive\Desktop\TCCS\Chatbot\chatbot_server\routes\webChat.js
|
| 2 |
+
const express = require('express');
|
| 3 |
+
const router = express.Router();
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const webService = require('../services/webService');
|
| 6 |
+
|
| 7 |
+
module.exports = (processChatLogicWithTrace) => {
|
| 8 |
+
|
| 9 |
+
// Giao diện Web chính
|
| 10 |
+
router.get('/', (req, res) => {
|
| 11 |
+
res.sendFile(path.join(__dirname, '../public', 'index.html'));
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
// 🌐 ROUTE KIỂM TRA CHAT TRÊN GIAO DIỆN WEB CỦA SPACE
|
| 15 |
+
router.post('/chat', async (req, res) => {
|
| 16 |
+
try {
|
| 17 |
+
const { userId, message } = req.body;
|
| 18 |
+
const result = await processChatLogicWithTrace(userId || "WEB_TEST", message);
|
| 19 |
+
const webDisplay = webService.formatForWeb(result.reply || "", result);
|
| 20 |
+
res.json({
|
| 21 |
+
...result,
|
| 22 |
+
reply: webDisplay.reply,
|
| 23 |
+
image: webDisplay.image,
|
| 24 |
+
buttons: webDisplay.buttons,
|
| 25 |
+
products: webDisplay.products
|
| 26 |
+
});
|
| 27 |
+
} catch (error) {
|
| 28 |
+
console.error("❌ Lỗi hiển thị Web:", error);
|
| 29 |
+
res.json({ reply: "Dạ em lỗi xíu, Mẹ nhắn lại nhen!", products: [] });
|
| 30 |
+
}
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
return router;
|
| 34 |
+
};
|