Update services/facebookService.js
Browse files- services/facebookService.js +28 -31
services/facebookService.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
| 1 |
// C:\Users\Hi\OneDrive\Desktop\TCCS\Chatbot\chatbot_server\services\facebookService.js
|
| 2 |
-
const fetch = require('node-fetch');
|
| 3 |
-
const https = require('https'); // 🎯 BẮT BUỘC NHÚNG THƯ VIỆN HTTPS GỐC
|
| 4 |
-
|
| 5 |
-
// 🎯 TẠO AGENT ÉP PHÁ BỎ MỌI LỚP CHẶN SSL TRÊN DOCKER HUGGING FACE
|
| 6 |
-
const agent = new https.Agent({
|
| 7 |
-
rejectUnauthorized: false,
|
| 8 |
-
secureOptions: require('crypto').constants.SSL_OP_LEGACY_SERVER_CONNECT // Cho phép bắt tay SSL linh hoạt hơn
|
| 9 |
-
});
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Hàm làm sạch văn bản: Xóa HTML rác và các tag lệnh ẩn
|
|
@@ -109,30 +102,38 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 109 |
if (imageUrls.length >= 2) {
|
| 110 |
console.log(`📚 [FB Service] Đang cấu trúc lại Album Carousel cho ${imageUrls.length} sản phẩm...`);
|
| 111 |
|
| 112 |
-
const blocks = responseText.split(/
|
| 113 |
const elements = [];
|
| 114 |
|
| 115 |
for (let i = 0; i < imageUrls.length; i++) {
|
| 116 |
const currentUrl = imageUrls[i];
|
| 117 |
-
const currentBlock = blocks[i] || "";
|
| 118 |
-
|
| 119 |
-
const codeMatch =
|
| 120 |
-
currentBlock.match(/\*\*Mã:\s*([^*\[\n]+)\*\*/i) ||
|
| 121 |
-
currentBlock.match(/Mã\s*(?:sản phẩm)?:\s*([^\n\[|-]+)/i) ||
|
| 122 |
-
currentBlock.match(/(?:🔹|👉|#|^)\s*([A-Z0-9]{2,5})\b/i) ||
|
| 123 |
-
currentBlock.match(/\b([A-Z]+[0-9]+)\b/);
|
| 124 |
|
| 125 |
-
const
|
|
|
|
| 126 |
|
| 127 |
-
const nameMatch = currentBlock.match(/(?:👕|👚|🌱|👉|^)\s*([^\n$*|]+)/m);
|
| 128 |
-
let itemName = nameMatch ? nameMatch[1].trim() : "
|
| 129 |
-
if (itemName.toLowerCase().includes("giá")
|
| 130 |
|
| 131 |
-
const priceMatch = currentBlock.match(/Giá:\s*\*?([^*|\n]+)\*?/i)
|
| 132 |
const itemPrice = priceMatch ? priceMatch[1].trim() : "Liên hệ shop";
|
| 133 |
|
|
|
|
|
|
|
|
|
|
| 134 |
const buttons = [];
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
buttons.push({
|
| 138 |
type: "postback",
|
|
@@ -166,8 +167,7 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 166 |
}
|
| 167 |
}
|
| 168 |
}
|
| 169 |
-
})
|
| 170 |
-
agent: agent // <--- 🎯 ÉP CHẠY QUA HTTPS AGENT SIÊU LÌ
|
| 171 |
});
|
| 172 |
}
|
| 173 |
|
|
@@ -191,11 +191,10 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 191 |
body: JSON.stringify({
|
| 192 |
recipient: { id: senderPsid },
|
| 193 |
message: formattedBottom
|
| 194 |
-
})
|
| 195 |
-
agent: agent // <--- 🎯 ÉP CHẠY QUA HTTPS AGENT SIÊU LÌ
|
| 196 |
});
|
| 197 |
}
|
| 198 |
-
console.log(`✅ [FB Service] Đã gửi xong Album Carousel
|
| 199 |
return;
|
| 200 |
}
|
| 201 |
|
|
@@ -217,8 +216,7 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 217 |
payload: { url: imageUrls[0], is_reusable: true }
|
| 218 |
}
|
| 219 |
}
|
| 220 |
-
})
|
| 221 |
-
agent: agent // <--- 🎯 ÉP CHẠY QUA HTTPS AGENT SIÊU LÌ
|
| 222 |
});
|
| 223 |
} catch (imgErr) {
|
| 224 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
|
@@ -233,8 +231,7 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 233 |
body: JSON.stringify({
|
| 234 |
recipient: { id: senderPsid },
|
| 235 |
message: formatted
|
| 236 |
-
})
|
| 237 |
-
agent: agent // <--- 🎯 ÉP CHẠY QUA HTTPS AGENT SIÊU LÌ
|
| 238 |
});
|
| 239 |
}
|
| 240 |
|
|
|
|
| 1 |
// C:\Users\Hi\OneDrive\Desktop\TCCS\Chatbot\chatbot_server\services\facebookService.js
|
| 2 |
+
const fetch = require('node-fetch'); // 🎯 ÉP DÙNG NODE-FETCH V2 ĐỂ BẮT BIẾN REJECT_UNAUTHORIZED
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Hàm làm sạch văn bản: Xóa HTML rác và các tag lệnh ẩn
|
|
|
|
| 102 |
if (imageUrls.length >= 2) {
|
| 103 |
console.log(`📚 [FB Service] Đang cấu trúc lại Album Carousel cho ${imageUrls.length} sản phẩm...`);
|
| 104 |
|
| 105 |
+
const blocks = responseText.split(/(?=🔹|\*\*Mã:)/i).filter(b => b.includes('http'));
|
| 106 |
const elements = [];
|
| 107 |
|
| 108 |
for (let i = 0; i < imageUrls.length; i++) {
|
| 109 |
const currentUrl = imageUrls[i];
|
| 110 |
+
const currentBlock = blocks[i] || "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
const codeMatch = currentBlock.match(/\*\*Mã:\s*([^*]+)\*\*/i) || currentBlock.match(/Mã:\s*([^\n[-]+)/i);
|
| 113 |
+
const itemCode = codeMatch ? codeMatch[1].trim() : `Mẫu ${i + 1}`;
|
| 114 |
|
| 115 |
+
const nameMatch = currentBlock.match(/(?:👕|👚|👕|🌱|👉|^)\s*([^\n$*|]+)/m);
|
| 116 |
+
let itemName = nameMatch ? nameMatch[1].trim() : "Quần áo trẻ em";
|
| 117 |
+
if (itemName.toLowerCase().includes("giá")) itemName = "Thời trang bé trai";
|
| 118 |
|
| 119 |
+
const priceMatch = currentBlock.match(/Giá:\s*\*?([^*|\n]+)\*?/i);
|
| 120 |
const itemPrice = priceMatch ? priceMatch[1].trim() : "Liên hệ shop";
|
| 121 |
|
| 122 |
+
const rawButtonRegex = /(\[[^\]]+\])/g;
|
| 123 |
+
const rawButtons = currentBlock.match(rawButtonRegex) || [];
|
| 124 |
+
|
| 125 |
const buttons = [];
|
| 126 |
+
let finalPayload = `[ACTION_ADD:${itemCode}]`;
|
| 127 |
+
|
| 128 |
+
if (rawButtons.length > 0) {
|
| 129 |
+
const rawText = rawButtons[0].trim();
|
| 130 |
+
const innerContent = rawText.slice(1, -1);
|
| 131 |
+
if (innerContent.includes('|')) {
|
| 132 |
+
finalPayload = innerContent.split('|')[1].trim();
|
| 133 |
+
} else {
|
| 134 |
+
finalPayload = rawText;
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
|
| 138 |
buttons.push({
|
| 139 |
type: "postback",
|
|
|
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
+
})
|
|
|
|
| 171 |
});
|
| 172 |
}
|
| 173 |
|
|
|
|
| 191 |
body: JSON.stringify({
|
| 192 |
recipient: { id: senderPsid },
|
| 193 |
message: formattedBottom
|
| 194 |
+
})
|
|
|
|
| 195 |
});
|
| 196 |
}
|
| 197 |
+
console.log(`✅ [FB Service] Đã gửi xong Album Carousel cho khách: ${senderPsid}`);
|
| 198 |
return;
|
| 199 |
}
|
| 200 |
|
|
|
|
| 216 |
payload: { url: imageUrls[0], is_reusable: true }
|
| 217 |
}
|
| 218 |
}
|
| 219 |
+
})
|
|
|
|
| 220 |
});
|
| 221 |
} catch (imgErr) {
|
| 222 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
|
|
|
| 231 |
body: JSON.stringify({
|
| 232 |
recipient: { id: senderPsid },
|
| 233 |
message: formatted
|
| 234 |
+
})
|
|
|
|
| 235 |
});
|
| 236 |
}
|
| 237 |
|