fca-riyad 37.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +325 -0
- package/DOCS.md +2712 -0
- package/README.md +410 -0
- package/func/checkUpdate.js +7 -0
- package/func/logAdapter.js +33 -0
- package/func/logger.js +48 -0
- package/index.d.ts +751 -0
- package/index.js +22 -0
- package/module/config.js +40 -0
- package/module/login.js +133 -0
- package/module/loginHelper.js +1359 -0
- package/module/options.js +44 -0
- package/package.json +95 -0
- package/src/api/action/addExternalModule.js +25 -0
- package/src/api/action/changeAvatar.js +137 -0
- package/src/api/action/changeBio.js +75 -0
- package/src/api/action/enableAutoSaveAppState.js +73 -0
- package/src/api/action/getCurrentUserID.js +7 -0
- package/src/api/action/handleFriendRequest.js +57 -0
- package/src/api/action/logout.js +76 -0
- package/src/api/action/refreshFb_dtsg.js +48 -0
- package/src/api/action/setPostReaction.js +106 -0
- package/src/api/action/unfriend.js +54 -0
- package/src/api/http/httpGet.js +46 -0
- package/src/api/http/httpPost.js +52 -0
- package/src/api/http/postFormData.js +47 -0
- package/src/api/messaging/addUserToGroup.js +68 -0
- package/src/api/messaging/changeAdminStatus.js +126 -0
- package/src/api/messaging/changeArchivedStatus.js +55 -0
- package/src/api/messaging/changeBlockedStatus.js +48 -0
- package/src/api/messaging/changeGroupImage.js +91 -0
- package/src/api/messaging/changeNickname.js +70 -0
- package/src/api/messaging/changeThreadColor.js +79 -0
- package/src/api/messaging/changeThreadEmoji.js +111 -0
- package/src/api/messaging/createNewGroup.js +88 -0
- package/src/api/messaging/createPoll.js +46 -0
- package/src/api/messaging/createThemeAI.js +98 -0
- package/src/api/messaging/deleteMessage.js +136 -0
- package/src/api/messaging/deleteThread.js +56 -0
- package/src/api/messaging/editMessage.js +90 -0
- package/src/api/messaging/forwardAttachment.js +57 -0
- package/src/api/messaging/forwardMessage.js +134 -0
- package/src/api/messaging/getEmojiUrl.js +29 -0
- package/src/api/messaging/getFriendsList.js +82 -0
- package/src/api/messaging/getMessage.js +829 -0
- package/src/api/messaging/getThemePictures.js +62 -0
- package/src/api/messaging/handleMessageRequest.js +65 -0
- package/src/api/messaging/markAsDelivered.js +57 -0
- package/src/api/messaging/markAsRead.js +88 -0
- package/src/api/messaging/markAsReadAll.js +49 -0
- package/src/api/messaging/markAsSeen.js +61 -0
- package/src/api/messaging/muteThread.js +50 -0
- package/src/api/messaging/pinMessage.js +115 -0
- package/src/api/messaging/removeUserFromGroup.js +62 -0
- package/src/api/messaging/resolvePhotoUrl.js +43 -0
- package/src/api/messaging/scheduler.js +264 -0
- package/src/api/messaging/searchForThread.js +53 -0
- package/src/api/messaging/sendBroadcast.js +93 -0
- package/src/api/messaging/sendMessage.js +269 -0
- package/src/api/messaging/sendTypingIndicator.js +74 -0
- package/src/api/messaging/sessionGuard.js +130 -0
- package/src/api/messaging/setMessageReaction.js +95 -0
- package/src/api/messaging/setTitle.js +124 -0
- package/src/api/messaging/shareContact.js +49 -0
- package/src/api/messaging/threadColors.js +128 -0
- package/src/api/messaging/unsendMessage.js +81 -0
- package/src/api/messaging/uploadAttachment.js +492 -0
- package/src/api/socket/OldMessage.js +186 -0
- package/src/api/socket/core/connectMqtt.js +269 -0
- package/src/api/socket/core/emitAuth.js +103 -0
- package/src/api/socket/core/getSeqID.js +321 -0
- package/src/api/socket/core/getTaskResponseData.js +25 -0
- package/src/api/socket/core/parseDelta.js +387 -0
- package/src/api/socket/detail/buildStream.js +215 -0
- package/src/api/socket/detail/constants.js +28 -0
- package/src/api/socket/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/crypto.js +173 -0
- package/src/api/socket/e2ee/e2ee/index.js +421 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/e2ee/proto/proto-writer.ts +76 -0
- package/src/api/socket/e2ee/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/e2ee/vendor/proto/proto-writer.ts +76 -0
- package/src/api/socket/e2ee/index.js +437 -0
- package/src/api/socket/e2ee/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/proto/proto-writer.ts +76 -0
- package/src/api/socket/e2ee/protocol.js +196 -0
- package/src/api/socket/e2ee/ratchet.js +219 -0
- package/src/api/socket/e2ee/store.js +182 -0
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +9162 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloApplication.proto +281 -0
- package/src/api/socket/e2ee/vendor/proto/ArmadilloICDC.proto +14 -0
- package/src/api/socket/e2ee/vendor/proto/ConsumerApplication.proto +232 -0
- package/src/api/socket/e2ee/vendor/proto/MessageApplication.proto +82 -0
- package/src/api/socket/e2ee/vendor/proto/MessageTransport.proto +77 -0
- package/src/api/socket/e2ee/vendor/proto/WACommon.proto +66 -0
- package/src/api/socket/e2ee/vendor/proto/WAMediaTransport.proto +176 -0
- package/src/api/socket/e2ee/vendor/proto/proto-writer.ts +76 -0
- package/src/api/socket/listenE2EE.js +75 -0
- package/src/api/socket/listenMqtt.js +388 -0
- package/src/api/socket/middleware/index.js +216 -0
- package/src/api/socket/sendMessage.js +309 -0
- package/src/api/socket/sendMessageMqtt.js +69 -0
- package/src/api/threads/getThreadHistory.js +664 -0
- package/src/api/threads/getThreadInfo.js +296 -0
- package/src/api/threads/getThreadList.js +293 -0
- package/src/api/threads/getThreadPictures.js +78 -0
- package/src/api/users/getUserID.js +65 -0
- package/src/api/users/getUserInfo.js +402 -0
- package/src/api/users/getUserInfoV2.js +134 -0
- package/src/app/MessengerBot.js +209 -0
- package/src/app/MessengerContext.js +32 -0
- package/src/app/createFcaClient.js +136 -0
- package/src/app/threadInfoRealtimeSync.js +284 -0
- package/src/core/sendReqMqtt.js +96 -0
- package/src/database/helpers.js +53 -0
- package/src/database/models/index.js +88 -0
- package/src/database/models/thread.js +50 -0
- package/src/database/models/user.js +46 -0
- package/src/database/threadData.js +94 -0
- package/src/database/userData.js +98 -0
- package/src/remote/remoteClient.js +123 -0
- package/src/utils/broadcast.js +51 -0
- package/src/utils/client.js +10 -0
- package/src/utils/constants.js +23 -0
- package/src/utils/cookies.js +68 -0
- package/src/utils/format/attachment.js +357 -0
- package/src/utils/format/cookie.js +9 -0
- package/src/utils/format/date.js +50 -0
- package/src/utils/format/decode.js +44 -0
- package/src/utils/format/delta.js +194 -0
- package/src/utils/format/ids.js +64 -0
- package/src/utils/format/index.js +64 -0
- package/src/utils/format/message.js +88 -0
- package/src/utils/format/presence.js +132 -0
- package/src/utils/format/readTyp.js +44 -0
- package/src/utils/format/thread.js +42 -0
- package/src/utils/format/utils.js +141 -0
- package/src/utils/headers.js +115 -0
- package/src/utils/loginParser/autoLogin.js +125 -0
- package/src/utils/loginParser/helpers.js +43 -0
- package/src/utils/loginParser/index.js +10 -0
- package/src/utils/loginParser/parseAndCheckLogin.js +220 -0
- package/src/utils/loginParser/textUtils.js +28 -0
- package/src/utils/nexca-logger.js +144 -0
- package/src/utils/nexca-utils.js +686 -0
- package/src/utils/request/client.js +26 -0
- package/src/utils/request/config.js +23 -0
- package/src/utils/request/defaults.js +46 -0
- package/src/utils/request/helpers.js +46 -0
- package/src/utils/request/index.js +17 -0
- package/src/utils/request/methods.js +163 -0
- package/src/utils/request/proxy.js +21 -0
- package/src/utils/request/retry.js +77 -0
- package/src/utils/request/sanitize.js +49 -0
- package/src/utils/versionCheck.js +47 -0
- package/src/vendor/fca-unofficial/.github/workflows/nodejs.yml +26 -0
- package/src/vendor/fca-unofficial/.github/workflows/npmpublish.yml +30 -0
- package/src/vendor/fca-unofficial/.travis.yml +6 -0
- package/src/vendor/fca-unofficial/DOCS.md +1731 -0
- package/src/vendor/fca-unofficial/LICENSE-MIT +21 -0
- package/src/vendor/fca-unofficial/README.md +225 -0
- package/src/vendor/fca-unofficial/index.js +619 -0
- package/src/vendor/fca-unofficial/package.json +76 -0
- package/src/vendor/fca-unofficial/src/addExternalModule.js +19 -0
- package/src/vendor/fca-unofficial/src/addUserToGroup.js +113 -0
- package/src/vendor/fca-unofficial/src/changeAdminStatus.js +79 -0
- package/src/vendor/fca-unofficial/src/changeArchivedStatus.js +55 -0
- package/src/vendor/fca-unofficial/src/changeBio.js +77 -0
- package/src/vendor/fca-unofficial/src/changeBlockedStatus.js +47 -0
- package/src/vendor/fca-unofficial/src/changeGroupImage.js +129 -0
- package/src/vendor/fca-unofficial/src/changeNickname.js +59 -0
- package/src/vendor/fca-unofficial/src/changeThreadColor.js +71 -0
- package/src/vendor/fca-unofficial/src/changeThreadEmoji.js +55 -0
- package/src/vendor/fca-unofficial/src/createNewGroup.js +86 -0
- package/src/vendor/fca-unofficial/src/createPoll.js +71 -0
- package/src/vendor/fca-unofficial/src/deleteMessage.js +56 -0
- package/src/vendor/fca-unofficial/src/deleteThread.js +56 -0
- package/src/vendor/fca-unofficial/src/forwardAttachment.js +60 -0
- package/src/vendor/fca-unofficial/src/getCurrentUserID.js +7 -0
- package/src/vendor/fca-unofficial/src/getEmojiUrl.js +29 -0
- package/src/vendor/fca-unofficial/src/getFriendsList.js +84 -0
- package/src/vendor/fca-unofficial/src/getThreadHistory.js +645 -0
- package/src/vendor/fca-unofficial/src/getThreadHistoryDeprecated.js +93 -0
- package/src/vendor/fca-unofficial/src/getThreadInfo.js +201 -0
- package/src/vendor/fca-unofficial/src/getThreadInfoDeprecated.js +80 -0
- package/src/vendor/fca-unofficial/src/getThreadList.js +238 -0
- package/src/vendor/fca-unofficial/src/getThreadListDeprecated.js +75 -0
- package/src/vendor/fca-unofficial/src/getThreadPictures.js +79 -0
- package/src/vendor/fca-unofficial/src/getUserID.js +66 -0
- package/src/vendor/fca-unofficial/src/getUserInfo.js +72 -0
- package/src/vendor/fca-unofficial/src/handleFriendRequest.js +61 -0
- package/src/vendor/fca-unofficial/src/handleMessageRequest.js +65 -0
- package/src/vendor/fca-unofficial/src/httpGet.js +52 -0
- package/src/vendor/fca-unofficial/src/httpPost.js +52 -0
- package/src/vendor/fca-unofficial/src/listenMqtt.js +788 -0
- package/src/vendor/fca-unofficial/src/logout.js +75 -0
- package/src/vendor/fca-unofficial/src/markAsDelivered.js +58 -0
- package/src/vendor/fca-unofficial/src/markAsRead.js +80 -0
- package/src/vendor/fca-unofficial/src/markAsReadAll.js +50 -0
- package/src/vendor/fca-unofficial/src/markAsSeen.js +59 -0
- package/src/vendor/fca-unofficial/src/muteThread.js +52 -0
- package/src/vendor/fca-unofficial/src/removeUserFromGroup.js +79 -0
- package/src/vendor/fca-unofficial/src/resolvePhotoUrl.js +45 -0
- package/src/vendor/fca-unofficial/src/searchForThread.js +53 -0
- package/src/vendor/fca-unofficial/src/sendMessage.js +459 -0
- package/src/vendor/fca-unofficial/src/sendTypingIndicator.js +103 -0
- package/src/vendor/fca-unofficial/src/setMessageReaction.js +117 -0
- package/src/vendor/fca-unofficial/src/setPostReaction.js +76 -0
- package/src/vendor/fca-unofficial/src/setTitle.js +86 -0
- package/src/vendor/fca-unofficial/src/threadColors.js +57 -0
- package/src/vendor/fca-unofficial/src/unfriend.js +52 -0
- package/src/vendor/fca-unofficial/src/unsendMessage.js +49 -0
- package/src/vendor/fca-unofficial/test/data/shareAttach.js +146 -0
- package/src/vendor/fca-unofficial/test/data/something.mov +0 -0
- package/src/vendor/fca-unofficial/test/data/test.png +0 -0
- package/src/vendor/fca-unofficial/test/data/test.txt +7 -0
- package/src/vendor/fca-unofficial/test/example-config.json +18 -0
- package/src/vendor/fca-unofficial/test/test-page.js +140 -0
- package/src/vendor/fca-unofficial/test/test.js +385 -0
- package/src/vendor/fca-unofficial/utils.js +1358 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const axios = require("axios");
|
|
4
|
+
const { wrapper } = require("axios-cookiejar-support");
|
|
5
|
+
const { CookieJar } = require("tough-cookie");
|
|
6
|
+
const FormData = require("form-data");
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
const stream = require("stream");
|
|
10
|
+
const { URL } = require("url");
|
|
11
|
+
const log = require("../../../func/logAdapter");
|
|
12
|
+
|
|
13
|
+
let http = null;
|
|
14
|
+
let cookieJar = new CookieJar();
|
|
15
|
+
let tokenCache = null;
|
|
16
|
+
let tokenCacheTime = 0;
|
|
17
|
+
const TOKEN_CACHE_TTL = 5 * 60 * 1000;
|
|
18
|
+
|
|
19
|
+
const DEFAULT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36";
|
|
20
|
+
|
|
21
|
+
function cleanJSON(x) {
|
|
22
|
+
if (typeof x !== "string") return x;
|
|
23
|
+
const s = x.replace(/^for\s*\(;;\);\s*/i, "");
|
|
24
|
+
try { return JSON.parse(s); } catch { return s; }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function pick(re, html, i = 1) {
|
|
28
|
+
const m = html && html.match(re);
|
|
29
|
+
return m ? m[i] : "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getFrom(html, a, b) {
|
|
33
|
+
const i = html.indexOf(a);
|
|
34
|
+
if (i < 0) return;
|
|
35
|
+
const start = i + a.length;
|
|
36
|
+
const j = html.indexOf(b, start);
|
|
37
|
+
return j < 0 ? undefined : html.slice(start, j);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function respFinalUrl(res) {
|
|
41
|
+
return (res && (res.url || res.requestUrl)) || "";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function detectCheckpoint(res) {
|
|
45
|
+
const url = String(respFinalUrl(res) || "");
|
|
46
|
+
const body = typeof res?.body === "string" ? res.body : "";
|
|
47
|
+
const hit =
|
|
48
|
+
/\/checkpoint\//i.test(url) ||
|
|
49
|
+
/(?:href|action)\s*=\s*["']https?:\/\/[^"']*\/checkpoint\//i.test(body) ||
|
|
50
|
+
/"checkpoint"|checkpoint_title|checkpointMain|id="checkpoint"/i.test(body) ||
|
|
51
|
+
(/login\.php/i.test(url) && /checkpoint/i.test(body));
|
|
52
|
+
return { hit, url: url || (body.match(/https?:\/\/[^"']*\/checkpoint\/[^"'<>]*/i)?.[0] || "") };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function checkpointError(res) {
|
|
56
|
+
const d = detectCheckpoint(res);
|
|
57
|
+
if (!d.hit) return null;
|
|
58
|
+
const e = new Error("Checkpoint required");
|
|
59
|
+
e.code = "CHECKPOINT";
|
|
60
|
+
e.checkpoint = true;
|
|
61
|
+
e.url = d.url || "https://www.facebook.com/checkpoint/";
|
|
62
|
+
e.status = res?.statusCode || res?.status;
|
|
63
|
+
return e;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function httpGet(pageUrl, ua, headers = {}) {
|
|
67
|
+
const host = new URL(pageUrl).hostname;
|
|
68
|
+
const referer = `https://${host}/`;
|
|
69
|
+
const baseHeaders = {
|
|
70
|
+
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
|
71
|
+
"Accept-Language": "vi-VN,vi;q=0.9,en-US;q=0.8,en;q=0.7",
|
|
72
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
73
|
+
"Cache-Control": "max-age=0",
|
|
74
|
+
Connection: "keep-alive",
|
|
75
|
+
Host: host,
|
|
76
|
+
Origin: `https://${host}`,
|
|
77
|
+
Referer: referer,
|
|
78
|
+
"Sec-Ch-Prefers-Color-Scheme": "dark",
|
|
79
|
+
"Sec-Ch-Ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
|
|
80
|
+
"Sec-Ch-Ua-Full-Version-List": '"Google Chrome";v="143.0.7499.182", "Chromium";v="143.0.7499.182", "Not A(Brand";v="24.0.0.0"',
|
|
81
|
+
"Sec-Ch-Ua-Mobile": "?0",
|
|
82
|
+
"Sec-Ch-Ua-Model": '""',
|
|
83
|
+
"Sec-Ch-Ua-Platform": '"Windows"',
|
|
84
|
+
"Sec-Ch-Ua-Platform-Version": '"19.0.0"',
|
|
85
|
+
"Sec-Fetch-Dest": "document",
|
|
86
|
+
"Sec-Fetch-Mode": "navigate",
|
|
87
|
+
"Sec-Fetch-Site": "same-origin",
|
|
88
|
+
"Sec-Fetch-User": "?1",
|
|
89
|
+
"Upgrade-Insecure-Requests": "1",
|
|
90
|
+
"User-Agent": ua || DEFAULT_UA,
|
|
91
|
+
"x-fb-rlafr": "0"
|
|
92
|
+
};
|
|
93
|
+
const res = await http.get(pageUrl, {
|
|
94
|
+
headers: { ...baseHeaders, ...headers },
|
|
95
|
+
timeout: 30000
|
|
96
|
+
});
|
|
97
|
+
const cp = checkpointError(res);
|
|
98
|
+
if (cp) throw cp;
|
|
99
|
+
return typeof res.data === "string" ? res.data : String(res.data || "");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function getTokens(ua, forceRefresh = false) {
|
|
103
|
+
const now = Date.now();
|
|
104
|
+
if (!forceRefresh && tokenCache && (now - tokenCacheTime) < TOKEN_CACHE_TTL) {
|
|
105
|
+
return tokenCache;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const html = await httpGet("https://www.facebook.com/", ua, { Referer: "https://www.facebook.com/" });
|
|
109
|
+
const fb_dtsg = getFrom(html, '"DTSGInitData",[],{"token":"', '",') || html.match(/name="fb_dtsg"\s+value="([^"]+)"/)?.[1] || "";
|
|
110
|
+
const jazoest = getFrom(html, 'name="jazoest" value="', '"') || getFrom(html, "jazoest=", '",') || html.match(/name="jazoest"\s+value="([^"]+)"/)?.[1] || "";
|
|
111
|
+
const lsd = getFrom(html, '["LSD",[],{"token":"', '"}') || html.match(/name="lsd"\s+value="([^"]+)"/)?.[1] || "";
|
|
112
|
+
const spin_r = pick(/"__spin_r":(\d+)/, html) || "";
|
|
113
|
+
const spin_t = pick(/"__spin_t":(\d+)/, html) || "";
|
|
114
|
+
const rev = pick(/"__rev":(\d+)/, html) || "";
|
|
115
|
+
|
|
116
|
+
if (!fb_dtsg || !lsd) {
|
|
117
|
+
// Cố gắng fallback nếu regex fail, nhưng thường là do cookie die
|
|
118
|
+
if (!tokenCache) throw new Error("Failed to fetch fb_dtsg or LSD from Facebook");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
tokenCache = { lsd, fb_dtsg, jazoest, spin_r, spin_t, rev };
|
|
122
|
+
tokenCacheTime = now;
|
|
123
|
+
return tokenCache;
|
|
124
|
+
} catch (e) {
|
|
125
|
+
if (tokenCache) {
|
|
126
|
+
log.warn("[uploadAttachment] Token fetch failed, using cached tokens: " + (e.message || e));
|
|
127
|
+
return tokenCache;
|
|
128
|
+
}
|
|
129
|
+
throw e;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function getType(obj) { return Object.prototype.toString.call(obj).slice(8, -1); }
|
|
134
|
+
function isReadableStream(obj) { return obj instanceof stream.Readable && (getType(obj._read) === "Function" || getType(obj._read) === "AsyncFunction") && getType(obj._readableState) === "Object"; }
|
|
135
|
+
function fromBuffer(buf) { return stream.Readable.from(buf); }
|
|
136
|
+
function parseDataUrl(s) { const m = /^data:([^;,]+)?(;base64)?,(.*)$/i.exec(s); if (!m) return null; const mime = m[1] || "application/octet-stream"; const isB64 = !!m[2]; const data = isB64 ? Buffer.from(m[3], "base64") : Buffer.from(decodeURIComponent(m[3]), "utf8"); return { mime, data }; }
|
|
137
|
+
|
|
138
|
+
function filenameFromUrl(u, headers) {
|
|
139
|
+
try {
|
|
140
|
+
const urlObj = new URL(u);
|
|
141
|
+
let filename = path.basename(urlObj.pathname) || `file-${Date.now()}`;
|
|
142
|
+
const cd = headers && (headers["content-disposition"] || headers["Content-Disposition"]);
|
|
143
|
+
if (cd) {
|
|
144
|
+
const m = /filename\*?=(?:UTF-8''|")?([^";\n]+)/i.exec(cd);
|
|
145
|
+
if (m) filename = decodeURIComponent(m[1].replace(/"/g, ""));
|
|
146
|
+
}
|
|
147
|
+
return filename;
|
|
148
|
+
} catch {
|
|
149
|
+
return `file-${Date.now()}`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function normalizeOne(input, ua) {
|
|
154
|
+
if (!input) throw new Error("Invalid input");
|
|
155
|
+
if (Buffer.isBuffer(input)) return { stream: fromBuffer(input), filename: `file-${Date.now()}.bin`, contentType: "application/octet-stream" };
|
|
156
|
+
if (typeof input === "string") {
|
|
157
|
+
if (/^https?:\/\//i.test(input)) {
|
|
158
|
+
const resp = await http.get(input, {
|
|
159
|
+
headers: {
|
|
160
|
+
"User-Agent": ua,
|
|
161
|
+
Accept: "*/*",
|
|
162
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
163
|
+
"Cache-Control": "no-cache"
|
|
164
|
+
},
|
|
165
|
+
timeout: 30000,
|
|
166
|
+
responseType: "stream"
|
|
167
|
+
});
|
|
168
|
+
const s = resp.data;
|
|
169
|
+
const filename = filenameFromUrl(input, resp.headers);
|
|
170
|
+
return { stream: s, filename };
|
|
171
|
+
}
|
|
172
|
+
if (input.startsWith("data:")) {
|
|
173
|
+
const p = parseDataUrl(input);
|
|
174
|
+
if (!p) throw new Error("Bad data URL");
|
|
175
|
+
return { stream: fromBuffer(p.data), filename: `file-${Date.now()}`, contentType: p.mime };
|
|
176
|
+
}
|
|
177
|
+
if (fs.existsSync(input) && fs.statSync(input).isFile()) {
|
|
178
|
+
return { stream: fs.createReadStream(input), filename: path.basename(input) };
|
|
179
|
+
}
|
|
180
|
+
throw new Error(`Unsupported string input: ${input}`);
|
|
181
|
+
}
|
|
182
|
+
if (isReadableStream(input)) {
|
|
183
|
+
return { stream: input, filename: `file-${Date.now()}` };
|
|
184
|
+
}
|
|
185
|
+
if (typeof input === "object") {
|
|
186
|
+
if (input.buffer && Buffer.isBuffer(input.buffer)) {
|
|
187
|
+
const filename = input.filename || `file-${Date.now()}.bin`;
|
|
188
|
+
const contentType = input.contentType || "application/octet-stream";
|
|
189
|
+
return { stream: fromBuffer(input.buffer), filename, contentType };
|
|
190
|
+
}
|
|
191
|
+
if (input.data && Buffer.isBuffer(input.data)) {
|
|
192
|
+
const filename = input.filename || `file-${Date.now()}.bin`;
|
|
193
|
+
const contentType = input.contentType || "application/octet-stream";
|
|
194
|
+
return { stream: fromBuffer(input.data), filename, contentType };
|
|
195
|
+
}
|
|
196
|
+
if (input.stream && isReadableStream(input.stream)) {
|
|
197
|
+
const filename = input.filename || `file-${Date.now()}`;
|
|
198
|
+
const contentType = input.contentType;
|
|
199
|
+
return { stream: input.stream, filename, contentType };
|
|
200
|
+
}
|
|
201
|
+
if (input.url) {
|
|
202
|
+
return normalizeOne(String(input.url), ua);
|
|
203
|
+
}
|
|
204
|
+
if (input.path && fs.existsSync(input.path) && fs.statSync(input.path).isFile()) {
|
|
205
|
+
return { stream: fs.createReadStream(input.path), filename: input.filename || path.basename(input.path), contentType: input.contentType };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
throw new Error("Unrecognized input");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function mapAttachmentDetails(data) {
|
|
212
|
+
const out = [];
|
|
213
|
+
if (!data || typeof data !== "object") return out;
|
|
214
|
+
|
|
215
|
+
const stack = [data];
|
|
216
|
+
while (stack.length) {
|
|
217
|
+
const cur = stack.pop();
|
|
218
|
+
if (!cur || typeof cur !== "object") continue;
|
|
219
|
+
const id = cur.video_id || cur.image_id || cur.audio_id || cur.file_id || cur.fbid || cur.id || cur.upload_id || cur.gif_id;
|
|
220
|
+
const idKey =
|
|
221
|
+
cur.video_id ? "video_id" :
|
|
222
|
+
cur.image_id ? "image_id" :
|
|
223
|
+
cur.audio_id ? "audio_id" :
|
|
224
|
+
cur.file_id ? "file_id" :
|
|
225
|
+
cur.gif_id ? "gif_id" :
|
|
226
|
+
cur.fbid ? "fbid" :
|
|
227
|
+
id ? "id" : null;
|
|
228
|
+
const filename = cur.filename || cur.file_name || cur.name || cur.original_filename;
|
|
229
|
+
const filetype = cur.filetype || cur.mime_type || cur.type || cur.content_type;
|
|
230
|
+
let thumbnail = cur.thumbnail_src || cur.thumbnail_url || cur.preview_url || cur.thumbSrc || cur.thumb_url || cur.image_preview_url || cur.large_preview_url;
|
|
231
|
+
if (!thumbnail) {
|
|
232
|
+
const m = cur.media || cur.thumbnail || cur.thumb || cur.image_data || cur.video_data || cur.preview;
|
|
233
|
+
thumbnail = m?.thumbnail_src || m?.thumbnail_url || m?.src || m?.uri || m?.url;
|
|
234
|
+
}
|
|
235
|
+
if (idKey) {
|
|
236
|
+
const o = {};
|
|
237
|
+
o[idKey] = id;
|
|
238
|
+
if (filename) o.filename = filename;
|
|
239
|
+
if (filetype) o.filetype = filetype;
|
|
240
|
+
if (thumbnail) o.thumbnail_src = thumbnail;
|
|
241
|
+
out.push(o);
|
|
242
|
+
}
|
|
243
|
+
if (Array.isArray(cur)) {
|
|
244
|
+
for (const v of cur) stack.push(v);
|
|
245
|
+
} else {
|
|
246
|
+
for (const k of Object.keys(cur)) stack.push(cur[k]);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!out.length && data.payload && Array.isArray(data.payload.metadata)) {
|
|
251
|
+
return data.payload.metadata.slice();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function pLimit(n) {
|
|
258
|
+
let active = 0;
|
|
259
|
+
const queue = [];
|
|
260
|
+
const next = () => {
|
|
261
|
+
active--;
|
|
262
|
+
if (queue.length) queue.shift()();
|
|
263
|
+
};
|
|
264
|
+
return fn => new Promise((resolve, reject) => {
|
|
265
|
+
const run = () => {
|
|
266
|
+
active++;
|
|
267
|
+
fn().then(v => { resolve(v); next(); }).catch(e => { reject(e); next(); });
|
|
268
|
+
};
|
|
269
|
+
if (active < n) run(); else queue.push(run);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Hàm upload core xử lý request
|
|
274
|
+
async function singleUpload(urlBase, file, ua, tokens, retries = 2) {
|
|
275
|
+
const form = new FormData();
|
|
276
|
+
// QUAN TRỌNG: Chỉ append file, KHÔNG append fb_dtsg vào body nữa
|
|
277
|
+
form.append("farr", file.stream, { filename: file.filename, contentType: file.contentType });
|
|
278
|
+
|
|
279
|
+
const headers = {
|
|
280
|
+
...form.getHeaders(),
|
|
281
|
+
Accept: "*/*",
|
|
282
|
+
"Accept-Language": "vi,en-US;q=0.9,en;q=0.8,fr-FR;q=0.7,fr;q=0.6",
|
|
283
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
284
|
+
"User-Agent": ua,
|
|
285
|
+
"x-asbd-id": "359341",
|
|
286
|
+
"x-fb-lsd": tokens.lsd || "",
|
|
287
|
+
"x-fb-friendly-name": "MercuryUpload",
|
|
288
|
+
"x-fb-request-analytics-tags": JSON.stringify({
|
|
289
|
+
network_tags: {
|
|
290
|
+
product: "256002347743983",
|
|
291
|
+
purpose: "none",
|
|
292
|
+
request_category: "graphql",
|
|
293
|
+
retry_attempt: "0"
|
|
294
|
+
},
|
|
295
|
+
application_tags: "graphservice"
|
|
296
|
+
}),
|
|
297
|
+
"sec-ch-prefers-color-scheme": "dark",
|
|
298
|
+
"sec-ch-ua": '"Google Chrome";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
|
|
299
|
+
"sec-ch-ua-mobile": "?0",
|
|
300
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
301
|
+
"sec-fetch-dest": "empty",
|
|
302
|
+
"sec-fetch-mode": "cors",
|
|
303
|
+
"sec-fetch-site": "same-origin",
|
|
304
|
+
Origin: "https://www.facebook.com",
|
|
305
|
+
Referer: "https://www.facebook.com/",
|
|
306
|
+
"x-fb-rlafr": "0",
|
|
307
|
+
Connection: "keep-alive"
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// Build URL với query string tokens
|
|
311
|
+
const finalUrl = new URL(urlBase);
|
|
312
|
+
finalUrl.searchParams.set("fb_dtsg", tokens.fb_dtsg);
|
|
313
|
+
finalUrl.searchParams.set("jazoest", tokens.jazoest);
|
|
314
|
+
finalUrl.searchParams.set("lsd", tokens.lsd);
|
|
315
|
+
finalUrl.searchParams.set("__aaid", "0");
|
|
316
|
+
finalUrl.searchParams.set("__ccg", "EXCELLENT");
|
|
317
|
+
|
|
318
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
319
|
+
try {
|
|
320
|
+
const res = await http.post(finalUrl.toString(), form, {
|
|
321
|
+
headers,
|
|
322
|
+
timeout: 120000,
|
|
323
|
+
maxContentLength: Infinity,
|
|
324
|
+
maxBodyLength: Infinity
|
|
325
|
+
});
|
|
326
|
+
return res;
|
|
327
|
+
} catch (e) {
|
|
328
|
+
if (attempt === retries) throw e;
|
|
329
|
+
if (e.code === "ETIMEDOUT" || e.code === "ECONNRESET" || (e.response && e.response.status >= 500)) {
|
|
330
|
+
await new Promise(r => setTimeout(r, (attempt + 1) * 1000));
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
throw e;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
339
|
+
const ua = ctx?.options?.userAgent || DEFAULT_UA;
|
|
340
|
+
cookieJar = ctx.jar instanceof CookieJar ? ctx.jar : new CookieJar();
|
|
341
|
+
|
|
342
|
+
// Axios instance
|
|
343
|
+
http = wrapper(axios.create({
|
|
344
|
+
timeout: 60000,
|
|
345
|
+
headers: {
|
|
346
|
+
"User-Agent": ua,
|
|
347
|
+
"Accept-Language": "vi-VN,vi;q=0.9,en-US;q=0.8,en;q=0.7",
|
|
348
|
+
"Accept-Encoding": "gzip, deflate, br",
|
|
349
|
+
Connection: "keep-alive"
|
|
350
|
+
},
|
|
351
|
+
maxRedirects: 5,
|
|
352
|
+
validateStatus: () => true
|
|
353
|
+
}));
|
|
354
|
+
http.defaults.withCredentials = true;
|
|
355
|
+
http.defaults.jar = cookieJar;
|
|
356
|
+
|
|
357
|
+
async function uploadCore(link, opts, callback) {
|
|
358
|
+
if (typeof opts === "function") { callback = opts; opts = undefined; }
|
|
359
|
+
const options = {
|
|
360
|
+
concurrency: Math.max(1, Math.min(5, Number(opts?.concurrency || 3))),
|
|
361
|
+
mode: opts?.mode === "single" ? "single" : "parallel"
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
let resolveFunc = function () { };
|
|
365
|
+
let rejectFunc = function () { };
|
|
366
|
+
const returnPromise = new Promise(function (resolve, reject) {
|
|
367
|
+
resolveFunc = resolve;
|
|
368
|
+
rejectFunc = reject;
|
|
369
|
+
});
|
|
370
|
+
if (!callback) {
|
|
371
|
+
callback = function (err, data) {
|
|
372
|
+
if (err) return rejectFunc(err);
|
|
373
|
+
resolveFunc(data);
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
(async () => {
|
|
378
|
+
try {
|
|
379
|
+
const inputsArr = Array.isArray(link) ? link : [link];
|
|
380
|
+
if (!inputsArr.length) {
|
|
381
|
+
const e = new Error("No files to upload");
|
|
382
|
+
callback(e);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
let tokens = await getTokens(ua);
|
|
387
|
+
const normAll = await Promise.all(inputsArr.map(x => normalizeOne(x, ua)));
|
|
388
|
+
|
|
389
|
+
// Base QS setup
|
|
390
|
+
const qs = [];
|
|
391
|
+
const userId = (ctx && (ctx.userID || ctx.userId)) ? String(ctx.userID || ctx.userId) : "";
|
|
392
|
+
if (userId) qs.push(`__user=${encodeURIComponent(userId)}`);
|
|
393
|
+
qs.push("__a=1");
|
|
394
|
+
qs.push("dpr=1");
|
|
395
|
+
const reqId = Math.floor(Math.random() * 36 ** 2).toString(36);
|
|
396
|
+
qs.push(`__req=${encodeURIComponent(reqId)}`);
|
|
397
|
+
if (tokens.spin_r) qs.push(`__spin_r=${encodeURIComponent(tokens.spin_r)}`);
|
|
398
|
+
if (tokens.spin_t) qs.push(`__spin_t=${encodeURIComponent(tokens.spin_t)}`);
|
|
399
|
+
if (tokens.rev) qs.push(`__rev=${encodeURIComponent(tokens.rev)}`);
|
|
400
|
+
qs.push("__spin_b=trunk");
|
|
401
|
+
qs.push("__comet_req=15");
|
|
402
|
+
|
|
403
|
+
const baseUrl = `https://www.facebook.com/ajax/mercury/upload.php?${qs.join("&")}`;
|
|
404
|
+
|
|
405
|
+
if (options.mode === "single") {
|
|
406
|
+
const f = normAll[0];
|
|
407
|
+
const res = await singleUpload(baseUrl, f, ua, tokens);
|
|
408
|
+
|
|
409
|
+
const cp = checkpointError(res);
|
|
410
|
+
if (cp) { tokenCache = null; throw cp; }
|
|
411
|
+
|
|
412
|
+
const data = cleanJSON(res.data);
|
|
413
|
+
const ids = mapAttachmentDetails(data);
|
|
414
|
+
|
|
415
|
+
if (!ids.length) {
|
|
416
|
+
const e = new Error("UploadFb returned no metadata/ids");
|
|
417
|
+
e.code = "NO_METADATA";
|
|
418
|
+
e.status = res.status;
|
|
419
|
+
e.body = typeof data === "string" ? data.slice(0, 500) : data;
|
|
420
|
+
throw e;
|
|
421
|
+
}
|
|
422
|
+
log.info(`[uploadAttachment] success ${ids.length} item(s) status ${res.status}`);
|
|
423
|
+
callback(null, { status: res.status, ids, raw: data });
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Parallel mode
|
|
428
|
+
const limit = pLimit(options.concurrency);
|
|
429
|
+
const tasks = normAll.map(f => () => singleUpload(baseUrl, f, ua, tokens));
|
|
430
|
+
const results = await Promise.all(tasks.map(t => limit(t)));
|
|
431
|
+
|
|
432
|
+
const ids = [];
|
|
433
|
+
const errors = [];
|
|
434
|
+
for (let i = 0; i < results.length; i++) {
|
|
435
|
+
const res = results[i];
|
|
436
|
+
try {
|
|
437
|
+
const cp = checkpointError(res);
|
|
438
|
+
if (cp) { tokenCache = null; throw cp; }
|
|
439
|
+
|
|
440
|
+
const data = cleanJSON(res.data);
|
|
441
|
+
const fileIds = mapAttachmentDetails(data);
|
|
442
|
+
if (!fileIds.length) {
|
|
443
|
+
log.warn(`[uploadAttachment] File ${i + 1} returned no metadata/ids`);
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
ids.push(...fileIds);
|
|
447
|
+
} catch (e) {
|
|
448
|
+
errors.push({ index: i, error: e });
|
|
449
|
+
log.error(`[uploadAttachment] Upload ${i + 1} failed: ${e.message || e}`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (ids.length === 0 && errors.length > 0) {
|
|
454
|
+
throw errors[0].error;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
log.info(`[uploadAttachment] success ${ids.length}/${normAll.length} item(s)`);
|
|
458
|
+
callback(null, { status: 200, ids, raw: null, errors: errors.length > 0 ? errors : undefined });
|
|
459
|
+
} catch (e) {
|
|
460
|
+
if (e.code === "CHECKPOINT" || (e.response && [401, 403].includes(e.response.status))) {
|
|
461
|
+
tokenCache = null;
|
|
462
|
+
try {
|
|
463
|
+
await getTokens(ua, true);
|
|
464
|
+
log.info("[uploadAttachment] Tokens refreshed after error");
|
|
465
|
+
} catch (refreshErr) {
|
|
466
|
+
log.error("[uploadAttachment] Token refresh failed: " + (refreshErr.message || refreshErr));
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
log.error(`[uploadAttachment] error ${e.code || e.status || ""} ${e.message || e}`);
|
|
470
|
+
callback(e);
|
|
471
|
+
}
|
|
472
|
+
})().catch(err => {
|
|
473
|
+
log.error("[uploadAttachment] Unhandled promise rejection: " + (err.message || err));
|
|
474
|
+
rejectFunc(err);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
return returnPromise;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return function uploadAttachment(attachments, callback) {
|
|
481
|
+
if (!attachments) throw { error: "Please pass an attachment or an array of attachments." };
|
|
482
|
+
|
|
483
|
+
if (typeof callback === "function") {
|
|
484
|
+
return uploadCore(attachments, { mode: "parallel" }, (err, result) => {
|
|
485
|
+
if (err) return callback(err);
|
|
486
|
+
callback(null, result && Array.isArray(result.ids) ? result.ids : []);
|
|
487
|
+
}).then(result => (result && Array.isArray(result.ids) ? result.ids : []));
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return uploadCore(attachments, { mode: "parallel" }).then(result => result && Array.isArray(result.ids) ? result.ids : []);
|
|
491
|
+
};
|
|
492
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("../../utils/nexca-utils");
|
|
4
|
+
var logger = require("../../utils/nexca-logger");
|
|
5
|
+
var bluebird = require("bluebird");
|
|
6
|
+
|
|
7
|
+
var allowedProperties = {
|
|
8
|
+
attachment: true, url: true, sticker: true, emoji: true,
|
|
9
|
+
emojiSize: true, body: true, mentions: true, location: true,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
|
13
|
+
|
|
14
|
+
function uploadAttachment(attachments, callback) {
|
|
15
|
+
var uploads = [];
|
|
16
|
+
for (var i = 0; i < attachments.length; i++) {
|
|
17
|
+
if (!utils.isReadableStream(attachments[i])) {
|
|
18
|
+
return callback({ error: "Attachment must be a readable stream." });
|
|
19
|
+
}
|
|
20
|
+
uploads.push(
|
|
21
|
+
defaultFuncs.postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, {
|
|
22
|
+
upload_1024: attachments[i],
|
|
23
|
+
voice_clip: "true"
|
|
24
|
+
})
|
|
25
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
26
|
+
.then(resData => {
|
|
27
|
+
if (resData.error) throw resData;
|
|
28
|
+
return resData.payload.metadata[0];
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
bluebird.all(uploads)
|
|
33
|
+
.then(resData => callback(null, resData))
|
|
34
|
+
.catch(err => { logger.error("OldMessage.upload", err); callback(err); });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getUrl(url, callback) {
|
|
38
|
+
defaultFuncs.post("https://www.facebook.com/message_share_attachment/fromURI/", ctx.jar, {
|
|
39
|
+
image_height: 960, image_width: 960, uri: url
|
|
40
|
+
})
|
|
41
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
42
|
+
.then(resData => {
|
|
43
|
+
if (resData.error) return callback(resData);
|
|
44
|
+
if (!resData.payload) return callback({ error: "Invalid URL" });
|
|
45
|
+
callback(null, resData.payload.share_data.share_params);
|
|
46
|
+
})
|
|
47
|
+
.catch(err => { logger.error("OldMessage.getUrl", err); callback(err); });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function sendContent(form, threadID, isSingleUser, messageAndOTID, callback) {
|
|
51
|
+
if (utils.getType(threadID) === "Array") {
|
|
52
|
+
for (var i = 0; i < threadID.length; i++) form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
|
|
53
|
+
form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
|
|
54
|
+
form["client_thread_id"] = "root:" + messageAndOTID;
|
|
55
|
+
} else {
|
|
56
|
+
if (isSingleUser) {
|
|
57
|
+
form["specific_to_list[0]"] = "fbid:" + threadID;
|
|
58
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.userID;
|
|
59
|
+
form["other_user_fbid"] = threadID;
|
|
60
|
+
} else {
|
|
61
|
+
form["thread_fbid"] = threadID;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (ctx.globalOptions.pageID) {
|
|
66
|
+
form["author"] = "fbid:" + ctx.globalOptions.pageID;
|
|
67
|
+
form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
|
|
68
|
+
form["creator_info[creatorID]"] = ctx.userID;
|
|
69
|
+
form["creator_info[creatorType]"] = "direct_admin";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
defaultFuncs.post("https://www.facebook.com/messaging/send/", ctx.jar, form)
|
|
73
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
|
74
|
+
.then(resData => {
|
|
75
|
+
if (!resData.payload) throw resData;
|
|
76
|
+
var messageID = (resData.payload.actions && resData.payload.actions[0] &&
|
|
77
|
+
resData.payload.actions[0].message_id) || null;
|
|
78
|
+
var threadID2 = resData.payload.thread_fbid ||
|
|
79
|
+
(resData.payload.actions && resData.payload.actions[0] && resData.payload.actions[0].thread_fbid);
|
|
80
|
+
callback(null, {
|
|
81
|
+
threadID: threadID2 ? String(threadID2) : String(threadID),
|
|
82
|
+
messageID: messageID ? String(messageID) : null,
|
|
83
|
+
timestamp: resData.payload.timestamp
|
|
84
|
+
});
|
|
85
|
+
})
|
|
86
|
+
.catch(err => { logger.error("OldMessage.send", err); callback(err); });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function send(form, threadID, isSingleUser, callback) {
|
|
90
|
+
var messageAndOTID = utils.generateOfflineThreadingID();
|
|
91
|
+
form.client = "mercury";
|
|
92
|
+
form.action_type = "ma-type:user-generated-message";
|
|
93
|
+
form.timestamp = Date.now();
|
|
94
|
+
form.timestamp_absolute = "Today";
|
|
95
|
+
form.timestamp_relative = "12:00";
|
|
96
|
+
form.timestamp_time_passed = "0";
|
|
97
|
+
form.is_unread = false;
|
|
98
|
+
form.is_cleared = false;
|
|
99
|
+
form.is_forward = false;
|
|
100
|
+
form.is_filtered_content = false;
|
|
101
|
+
form.is_filtered_content_bec = false;
|
|
102
|
+
form.is_filtered_content_account = false;
|
|
103
|
+
form.is_filtered_content_quasar = false;
|
|
104
|
+
form.is_filtered_content_invalid_app = false;
|
|
105
|
+
form.is_not_supported = false;
|
|
106
|
+
form.message_id = messageAndOTID;
|
|
107
|
+
form.offline_threading_id = messageAndOTID;
|
|
108
|
+
form.ephemeral_ttl_mode = 0;
|
|
109
|
+
form.manual_retry_cnt = 0;
|
|
110
|
+
form.has_attachment = false;
|
|
111
|
+
form.signatureID = utils.getGUID().replace(/-/g, "").slice(0, 8);
|
|
112
|
+
sendContent(form, threadID, isSingleUser, messageAndOTID, callback);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return function OldMessage(msg, threadID, callback, replyToMessage, isSingleUser) {
|
|
116
|
+
if (typeof msg === "string") msg = { body: msg };
|
|
117
|
+
|
|
118
|
+
if (msg.body && /(https?:\/\/|www\.|t\.me\/|fb\.me\/|youtu\.be\/|facebook\.com\/|youtube\.com\/)/i.test(msg.body)) {
|
|
119
|
+
return require('./sendMessage')(defaultFuncs, api, ctx)(msg, threadID, callback, replyToMessage, false);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
var resolveFunc = () => { };
|
|
123
|
+
var rejectFunc = () => { };
|
|
124
|
+
var promise = new Promise((res, rej) => { resolveFunc = res; rejectFunc = rej; });
|
|
125
|
+
if (!callback) {
|
|
126
|
+
callback = (err, data) => err ? rejectFunc(err) : resolveFunc(data);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (var key in msg) {
|
|
130
|
+
if (!allowedProperties[key]) {
|
|
131
|
+
return callback({ error: "OldMessage: Unknown property '" + key + "'" });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
var form = {};
|
|
136
|
+
if (msg.body) form.body = msg.body;
|
|
137
|
+
|
|
138
|
+
if (msg.mentions && Array.isArray(msg.mentions)) {
|
|
139
|
+
form.body = msg.body || "";
|
|
140
|
+
form['profile_to_mention[0]'] = msg.mentions.map(m => m.id).join(",");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (replyToMessage) form.replied_to_message_id = replyToMessage;
|
|
144
|
+
|
|
145
|
+
if (msg.sticker) {
|
|
146
|
+
form["has_attachment"] = true;
|
|
147
|
+
form["sticker_id"] = msg.sticker;
|
|
148
|
+
return send(form, threadID, isSingleUser, callback);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (msg.emoji) {
|
|
152
|
+
form.body = msg.emoji;
|
|
153
|
+
var size = msg.emojiSize || "small";
|
|
154
|
+
form["emoji_size"] = size;
|
|
155
|
+
return send(form, threadID, isSingleUser, callback);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (msg.url) {
|
|
159
|
+
return getUrl(msg.url, (err, shareData) => {
|
|
160
|
+
if (err) return callback(err);
|
|
161
|
+
form["has_attachment"] = true;
|
|
162
|
+
form["image_ids[0]"] = shareData.image_ids && shareData.image_ids[0];
|
|
163
|
+
form["share_params"] = JSON.stringify(shareData);
|
|
164
|
+
send(form, threadID, isSingleUser, callback);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (msg.attachment) {
|
|
169
|
+
var attachments = Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment];
|
|
170
|
+
return uploadAttachment(attachments, (err, uploaded) => {
|
|
171
|
+
if (err) return callback(err);
|
|
172
|
+
form["has_attachment"] = true;
|
|
173
|
+
uploaded.forEach((u, i) => {
|
|
174
|
+
if (u.image_id) form["image_ids[" + i + "]"] = u.image_id;
|
|
175
|
+
else if (u.video_id) form["video_ids[" + i + "]"] = u.video_id;
|
|
176
|
+
else if (u.file_id) form["file_ids[" + i + "]"] = u.file_id;
|
|
177
|
+
else if (u.audio_id) form["audio_ids[" + i + "]"] = u.audio_id;
|
|
178
|
+
});
|
|
179
|
+
send(form, threadID, isSingleUser, callback);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
send(form, threadID, isSingleUser, callback);
|
|
184
|
+
return promise;
|
|
185
|
+
};
|
|
186
|
+
};
|