Create retain.js
Browse files
processors/logicHandler/retain.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// processors/logicHandler/retain.js
|
| 2 |
+
const retainHandler = {
|
| 3 |
+
handle: async (session, message, dimConfig, retainConfig = [], globalInventory = null) => {
|
| 4 |
+
const text = (message || "").toLowerCase().trim();
|
| 5 |
+
// Lọc các quy tắc thuộc vùng RETAIN
|
| 6 |
+
const retainRules = (dimConfig || []).filter(d => d.zone === 'RETAIN');
|
| 7 |
+
|
| 8 |
+
let bestMatch = null;
|
| 9 |
+
|
| 10 |
+
// 1. Tìm theo từ khóa (Dùng vòng lặp for...of để dễ kiểm soát)
|
| 11 |
+
for (const rule of retainRules) {
|
| 12 |
+
// Ép kiểu sang String để tránh lỗi .split is not a function nếu keywords là số
|
| 13 |
+
const rawKeywords = String(rule.keywords || "");
|
| 14 |
+
const keywords = rawKeywords.split(',').map(k => k.trim().toLowerCase()).filter(Boolean);
|
| 15 |
+
|
| 16 |
+
if (keywords.some(k => text.includes(k))) {
|
| 17 |
+
// ƯU TIÊN: Template (viết hoa T) -> template -> content -> Body
|
| 18 |
+
bestMatch = rule.Template || rule.template || rule.content || rule.Body;
|
| 19 |
+
if (bestMatch) break;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
// 2. Nếu không khớp từ khóa cụ thể, lấy dòng mặc định (BẢN SỬA LỖI TẠI ĐÂY)
|
| 24 |
+
if (!bestMatch) {
|
| 25 |
+
const defaultRule = retainRules.find(d => {
|
| 26 |
+
const kw = String(d.keywords || "").trim(); // Ép kiểu an toàn trước khi trim
|
| 27 |
+
return kw === "";
|
| 28 |
+
});
|
| 29 |
+
bestMatch = defaultRule?.Template || defaultRule?.template || defaultRule?.content || defaultRule?.Body;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// 3. Trả về kết quả
|
| 33 |
+
if (bestMatch && String(bestMatch).trim() !== "") {
|
| 34 |
+
return { reply: String(bestMatch).trim() };
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
return null;
|
| 38 |
+
}
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
module.exports = retainHandler;
|