Update services/facebookService.js
Browse files- services/facebookService.js +51 -40
services/facebookService.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
| 1 |
// C:\Users\Hi\OneDrive\Desktop\TCCS\Chatbot\chatbot_server\services\facebookService.js
|
| 2 |
-
const axios = require('axios');
|
| 3 |
-
const https = require('https'); // 🎯 THÊM THƯ VIỆN ĐỂ XỬ LÝ LỖI HANDSHAKE SSL
|
| 4 |
-
|
| 5 |
-
// 🎯 TẠO HTTPS AGENT ĐỂ BẺ GÃY CÁC BỘ LỌC CHỨNG CHỈ TRÊN DOCKER HUGGING FACE
|
| 6 |
-
const sslAgent = new https.Agent({
|
| 7 |
-
rejectUnauthorized: false
|
| 8 |
-
});
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Hàm làm sạch văn bản: Xóa HTML rác và các tag lệnh ẩn
|
|
@@ -96,12 +89,13 @@ function formatMessage(text, psid) {
|
|
| 96 |
}
|
| 97 |
|
| 98 |
/**
|
| 99 |
-
* Hàm chính thực hiện gửi tin nhắn thông minh
|
| 100 |
*/
|
| 101 |
async function sendResponse(senderPsid, responseText) {
|
| 102 |
try {
|
| 103 |
const accessToken = process.env.FB_PAGE_ACCESS_TOKEN;
|
| 104 |
const imageUrls = extractAllImageUrls(responseText);
|
|
|
|
| 105 |
|
| 106 |
// --- TRƯỜNG HỢP NHIỀU SẢN PHẨM (Dựng Album Carousel) ---
|
| 107 |
if (imageUrls.length >= 2) {
|
|
@@ -156,20 +150,24 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
-
// 🎯
|
| 160 |
if (elements.length > 0) {
|
| 161 |
-
await
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
-
}
|
| 172 |
-
}
|
| 173 |
}
|
| 174 |
|
| 175 |
const lines = responseText.split('\n');
|
|
@@ -183,49 +181,62 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 183 |
let bottomText = bottomTextLines.join('\n').trim();
|
| 184 |
bottomText = cleanTextContent(bottomText);
|
| 185 |
|
| 186 |
-
// 🎯
|
| 187 |
if (bottomText) {
|
| 188 |
const formattedBottom = formatMessage(bottomText, senderPsid);
|
| 189 |
-
await
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
}
|
|
|
|
| 194 |
return;
|
| 195 |
}
|
| 196 |
|
| 197 |
// --- TRƯỜNG HỢP THÔNG THƯỜNG (1 ẢNH HOẶC KHÔNG CÓ ẢNH) ---
|
| 198 |
const formatted = formatMessage(responseText, senderPsid);
|
| 199 |
|
| 200 |
-
// 🎯
|
| 201 |
if (imageUrls.length === 1) {
|
| 202 |
console.log(`📸 [FB Service] Gửi 1 ảnh sản phẩm lẻ: ${imageUrls[0]}`);
|
| 203 |
try {
|
| 204 |
-
await
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
} catch (imgErr) {
|
| 214 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
-
// 🎯
|
| 219 |
if (formatted && (formatted.text || formatted.attachment)) {
|
| 220 |
-
await
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
}
|
| 225 |
|
| 226 |
console.log(`✅ [FB Service] Đã xử lý tin nhắn sạch cho khách: ${senderPsid}`);
|
| 227 |
} catch (err) {
|
| 228 |
-
console.error("❌ [FB Service] Lỗi nghiêm trọng:", err.
|
| 229 |
}
|
| 230 |
}
|
| 231 |
|
|
|
|
| 1 |
// C:\Users\Hi\OneDrive\Desktop\TCCS\Chatbot\chatbot_server\services\facebookService.js
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Hàm làm sạch văn bản: Xóa HTML rác và các tag lệnh ẩn
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
+
* Hàm chính thực hiện gửi tin nhắn thông minh (Đã chuyển đổi hoàn toàn sang Native Fetch)
|
| 93 |
*/
|
| 94 |
async function sendResponse(senderPsid, responseText) {
|
| 95 |
try {
|
| 96 |
const accessToken = process.env.FB_PAGE_ACCESS_TOKEN;
|
| 97 |
const imageUrls = extractAllImageUrls(responseText);
|
| 98 |
+
const fbUrl = `https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`;
|
| 99 |
|
| 100 |
// --- TRƯỜNG HỢP NHIỀU SẢN PHẨM (Dựng Album Carousel) ---
|
| 101 |
if (imageUrls.length >= 2) {
|
|
|
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
+
// 🎯 Gửi Album Carousel bằng FETCH bản gốc
|
| 154 |
if (elements.length > 0) {
|
| 155 |
+
await fetch(fbUrl, {
|
| 156 |
+
method: 'POST',
|
| 157 |
+
headers: { 'Content-Type': 'application/json' },
|
| 158 |
+
body: JSON.stringify({
|
| 159 |
+
recipient: { id: senderPsid },
|
| 160 |
+
message: {
|
| 161 |
+
attachment: {
|
| 162 |
+
type: "template",
|
| 163 |
+
payload: {
|
| 164 |
+
template_type: "generic",
|
| 165 |
+
elements: elements
|
| 166 |
+
}
|
| 167 |
}
|
| 168 |
}
|
| 169 |
+
})
|
| 170 |
+
});
|
| 171 |
}
|
| 172 |
|
| 173 |
const lines = responseText.split('\n');
|
|
|
|
| 181 |
let bottomText = bottomTextLines.join('\n').trim();
|
| 182 |
bottomText = cleanTextContent(bottomText);
|
| 183 |
|
| 184 |
+
// 🎯 Gửi Text hướng dẫn ở cuối Album bằng FETCH bản gốc
|
| 185 |
if (bottomText) {
|
| 186 |
const formattedBottom = formatMessage(bottomText, senderPsid);
|
| 187 |
+
await fetch(fbUrl, {
|
| 188 |
+
method: 'POST',
|
| 189 |
+
headers: { 'Content-Type': 'application/json' },
|
| 190 |
+
body: JSON.stringify({
|
| 191 |
+
recipient: { id: senderPsid },
|
| 192 |
+
message: formattedBottom
|
| 193 |
+
})
|
| 194 |
+
});
|
| 195 |
}
|
| 196 |
+
console.log(`✅ [FB Service] Đã gửi xong Album Carousel cho khách: ${senderPsid}`);
|
| 197 |
return;
|
| 198 |
}
|
| 199 |
|
| 200 |
// --- TRƯỜNG HỢP THÔNG THƯỜNG (1 ẢNH HOẶC KHÔNG CÓ ẢNH) ---
|
| 201 |
const formatted = formatMessage(responseText, senderPsid);
|
| 202 |
|
| 203 |
+
// 🎯 Gửi 1 ảnh lẻ bằng FETCH bản gốc
|
| 204 |
if (imageUrls.length === 1) {
|
| 205 |
console.log(`📸 [FB Service] Gửi 1 ảnh sản phẩm lẻ: ${imageUrls[0]}`);
|
| 206 |
try {
|
| 207 |
+
await fetch(fbUrl, {
|
| 208 |
+
method: 'POST',
|
| 209 |
+
headers: { 'Content-Type': 'application/json' },
|
| 210 |
+
body: JSON.stringify({
|
| 211 |
+
recipient: { id: senderPsid },
|
| 212 |
+
message: {
|
| 213 |
+
attachment: {
|
| 214 |
+
type: "image",
|
| 215 |
+
payload: { url: imageUrls[0], is_reusable: true }
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
})
|
| 219 |
+
});
|
| 220 |
} catch (imgErr) {
|
| 221 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
+
// 🎯 Gửi tin nhắn Text thông thường bằng FETCH bản gốc
|
| 226 |
if (formatted && (formatted.text || formatted.attachment)) {
|
| 227 |
+
await fetch(fbUrl, {
|
| 228 |
+
method: 'POST',
|
| 229 |
+
headers: { 'Content-Type': 'application/json' },
|
| 230 |
+
body: JSON.stringify({
|
| 231 |
+
recipient: { id: senderPsid },
|
| 232 |
+
message: formatted
|
| 233 |
+
})
|
| 234 |
+
});
|
| 235 |
}
|
| 236 |
|
| 237 |
console.log(`✅ [FB Service] Đã xử lý tin nhắn sạch cho khách: ${senderPsid}`);
|
| 238 |
} catch (err) {
|
| 239 |
+
console.error("❌ [FB Service] Lỗi nghiêm trọng:", err.message);
|
| 240 |
}
|
| 241 |
}
|
| 242 |
|