Update services/facebookService.js
Browse files
services/facebookService.js
CHANGED
|
@@ -156,9 +156,8 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
-
//
|
| 160 |
if (elements.length > 0) {
|
| 161 |
-
// 🎯 TRUYỀN HTTPS AGENT VÀO ĐỂ TRÁNH LỖI EPROTO VỚI META
|
| 162 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 163 |
recipient: { id: senderPsid },
|
| 164 |
message: {
|
|
@@ -170,7 +169,7 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 170 |
}
|
| 171 |
}
|
| 172 |
}
|
| 173 |
-
}, { httpsAgent: sslAgent });
|
| 174 |
}
|
| 175 |
|
| 176 |
const lines = responseText.split('\n');
|
|
@@ -184,13 +183,13 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 184 |
let bottomText = bottomTextLines.join('\n').trim();
|
| 185 |
bottomText = cleanTextContent(bottomText);
|
| 186 |
|
|
|
|
| 187 |
if (bottomText) {
|
| 188 |
const formattedBottom = formatMessage(bottomText, senderPsid);
|
| 189 |
-
// 🎯 TRUYỀN HTTPS AGENT VÀO ĐỂ TRÁNH LỖI EPROTO VỚI META
|
| 190 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 191 |
recipient: { id: senderPsid },
|
| 192 |
message: formattedBottom
|
| 193 |
-
}, { httpsAgent: sslAgent });
|
| 194 |
}
|
| 195 |
return;
|
| 196 |
}
|
|
@@ -198,10 +197,10 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 198 |
// --- TRƯỜNG HỢP THÔNG THƯỜNG (1 ẢNH HOẶC KHÔNG CÓ ẢNH) ---
|
| 199 |
const formatted = formatMessage(responseText, senderPsid);
|
| 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 |
-
// 🎯 TRUYỀN HTTPS AGENT VÀO ĐỂ TRÁNH LỖI EPROTO VỚI META
|
| 205 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 206 |
recipient: { id: senderPsid },
|
| 207 |
message: {
|
|
@@ -210,18 +209,18 @@ async function sendResponse(senderPsid, responseText) {
|
|
| 210 |
payload: { url: imageUrls[0], is_reusable: true }
|
| 211 |
}
|
| 212 |
}
|
| 213 |
-
}, { httpsAgent: sslAgent });
|
| 214 |
} catch (imgErr) {
|
| 215 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
| 216 |
}
|
| 217 |
}
|
| 218 |
|
|
|
|
| 219 |
if (formatted && (formatted.text || formatted.attachment)) {
|
| 220 |
-
// 🎯 TRUYỀN HTTPS AGENT VÀO ĐỂ TRÁNH LỖI EPROTO VỚI META
|
| 221 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 222 |
recipient: { id: senderPsid },
|
| 223 |
message: formatted
|
| 224 |
-
}, { httpsAgent: sslAgent });
|
| 225 |
}
|
| 226 |
|
| 227 |
console.log(`✅ [FB Service] Đã xử lý tin nhắn sạch cho khách: ${senderPsid}`);
|
|
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
+
// 🎯 LỆNH 1: Gửi Album Carousel (Bắt buộc có sslAgent)
|
| 160 |
if (elements.length > 0) {
|
|
|
|
| 161 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 162 |
recipient: { id: senderPsid },
|
| 163 |
message: {
|
|
|
|
| 169 |
}
|
| 170 |
}
|
| 171 |
}
|
| 172 |
+
}, { httpsAgent: sslAgent }); // <--- Chỗ này đã có
|
| 173 |
}
|
| 174 |
|
| 175 |
const lines = responseText.split('\n');
|
|
|
|
| 183 |
let bottomText = bottomTextLines.join('\n').trim();
|
| 184 |
bottomText = cleanTextContent(bottomText);
|
| 185 |
|
| 186 |
+
// 🎯 LỆNH 2: Gửi Text Hướng Dẫn ở cuối Album (Bắt buộc có sslAgent)
|
| 187 |
if (bottomText) {
|
| 188 |
const formattedBottom = formatMessage(bottomText, senderPsid);
|
|
|
|
| 189 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 190 |
recipient: { id: senderPsid },
|
| 191 |
message: formattedBottom
|
| 192 |
+
}, { httpsAgent: sslAgent }); // <--- ĐẢM BẢO KHÔNG SÓT CHỖ NÀY
|
| 193 |
}
|
| 194 |
return;
|
| 195 |
}
|
|
|
|
| 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 |
+
// 🎯 LỆNH 3: Gửi 1 ảnh lẻ (Bắt buộc có sslAgent)
|
| 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 axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 205 |
recipient: { id: senderPsid },
|
| 206 |
message: {
|
|
|
|
| 209 |
payload: { url: imageUrls[0], is_reusable: true }
|
| 210 |
}
|
| 211 |
}
|
| 212 |
+
}, { httpsAgent: sslAgent }); // <--- ĐẢM BẢO KHÔNG SÓT CHỖ NÀY
|
| 213 |
} catch (imgErr) {
|
| 214 |
console.error("⚠️ Lỗi gửi ảnh đơn:", imgErr.message);
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
+
// 🎯 LỆNH 4: Gửi tin nhắn Text thông thường (Bắt buộc có sslAgent)
|
| 219 |
if (formatted && (formatted.text || formatted.attachment)) {
|
|
|
|
| 220 |
await axios.post(`https://graph.facebook.com/v19.0/me/messages?access_token=${accessToken}`, {
|
| 221 |
recipient: { id: senderPsid },
|
| 222 |
message: formatted
|
| 223 |
+
}, { httpsAgent: sslAgent }); // <--- ĐẢM BẢO KHÔNG SÓT CHỖ NÀY
|
| 224 |
}
|
| 225 |
|
| 226 |
console.log(`✅ [FB Service] Đã xử lý tin nhắn sạch cho khách: ${senderPsid}`);
|