phanquocdat123 commited on
Commit
5c58a45
·
verified ·
1 Parent(s): e1d3a28

Update services/facebookService.js

Browse files
Files changed (1) hide show
  1. 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
- // 🎯 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: {
164
- attachment: {
165
- type: "template",
166
- payload: {
167
- template_type: "generic",
168
- elements: elements
 
 
 
 
169
  }
170
  }
171
- }
172
- }, { httpsAgent: sslAgent }); // <--- Chỗ này đã có
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
- // 🎯 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
  }
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
- // 🎯 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: {
207
- attachment: {
208
- type: "image",
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}`);
227
  } catch (err) {
228
- console.error("❌ [FB Service] Lỗi nghiêm trọng:", err.response?.data || err.message);
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