@lazyneoaz/nkxchat 1.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.
Files changed (123) hide show
  1. package/LICENSE +3 -0
  2. package/README.md +199 -0
  3. package/examples/login-with-cookies.js +102 -0
  4. package/examples/verify.js +70 -0
  5. package/index.js +2 -0
  6. package/package.json +84 -0
  7. package/src/apis/addExternalModule.js +24 -0
  8. package/src/apis/addUserToGroup.js +108 -0
  9. package/src/apis/changeAdminStatus.js +148 -0
  10. package/src/apis/changeArchivedStatus.js +61 -0
  11. package/src/apis/changeAvatar.js +103 -0
  12. package/src/apis/changeBio.js +69 -0
  13. package/src/apis/changeBlockedStatus.js +54 -0
  14. package/src/apis/changeGroupImage.js +136 -0
  15. package/src/apis/changeThreadColor.js +116 -0
  16. package/src/apis/changeThreadEmoji.js +53 -0
  17. package/src/apis/comment.js +207 -0
  18. package/src/apis/createAITheme.js +129 -0
  19. package/src/apis/createNewGroup.js +79 -0
  20. package/src/apis/createPoll.js +73 -0
  21. package/src/apis/deleteMessage.js +44 -0
  22. package/src/apis/deleteThread.js +52 -0
  23. package/src/apis/editMessage.js +70 -0
  24. package/src/apis/emoji.js +124 -0
  25. package/src/apis/enableAutoSaveAppState.js +69 -0
  26. package/src/apis/fetchThemeData.js +113 -0
  27. package/src/apis/follow.js +81 -0
  28. package/src/apis/forwardAttachment.js +178 -0
  29. package/src/apis/forwardMessage.js +52 -0
  30. package/src/apis/friend.js +243 -0
  31. package/src/apis/gcmember.js +122 -0
  32. package/src/apis/gcname.js +123 -0
  33. package/src/apis/gcrule.js +119 -0
  34. package/src/apis/getAccess.js +111 -0
  35. package/src/apis/getBotInfo.js +88 -0
  36. package/src/apis/getBotInitialData.js +43 -0
  37. package/src/apis/getEmojiUrl.js +40 -0
  38. package/src/apis/getFriendsList.js +79 -0
  39. package/src/apis/getMessage.js +423 -0
  40. package/src/apis/getTheme.js +123 -0
  41. package/src/apis/getThemeInfo.js +116 -0
  42. package/src/apis/getThemePictures.js +87 -0
  43. package/src/apis/getThreadColors.js +119 -0
  44. package/src/apis/getThreadHistory.js +239 -0
  45. package/src/apis/getThreadInfo.js +267 -0
  46. package/src/apis/getThreadList.js +232 -0
  47. package/src/apis/getThreadPictures.js +58 -0
  48. package/src/apis/getUserID.js +117 -0
  49. package/src/apis/getUserInfo.js +513 -0
  50. package/src/apis/getUserInfoV2.js +146 -0
  51. package/src/apis/handleFriendRequest.js +66 -0
  52. package/src/apis/handleMessageRequest.js +50 -0
  53. package/src/apis/httpGet.js +63 -0
  54. package/src/apis/httpPost.js +89 -0
  55. package/src/apis/httpPostFormData.js +69 -0
  56. package/src/apis/listenMqtt.js +924 -0
  57. package/src/apis/listenSpeed.js +178 -0
  58. package/src/apis/logout.js +63 -0
  59. package/src/apis/markAsDelivered.js +47 -0
  60. package/src/apis/markAsRead.js +95 -0
  61. package/src/apis/markAsReadAll.js +40 -0
  62. package/src/apis/markAsSeen.js +70 -0
  63. package/src/apis/mqttDeltaValue.js +252 -0
  64. package/src/apis/muteThread.js +45 -0
  65. package/src/apis/nickname.js +132 -0
  66. package/src/apis/notes.js +163 -0
  67. package/src/apis/pinMessage.js +150 -0
  68. package/src/apis/produceMetaTheme.js +160 -0
  69. package/src/apis/realtime.js +182 -0
  70. package/src/apis/refreshFb_dtsg.js +94 -0
  71. package/src/apis/removeUserFromGroup.js +117 -0
  72. package/src/apis/resolvePhotoUrl.js +58 -0
  73. package/src/apis/searchForThread.js +154 -0
  74. package/src/apis/sendEffect.js +306 -0
  75. package/src/apis/sendMessage.js +353 -0
  76. package/src/apis/sendMessageMqtt.js +255 -0
  77. package/src/apis/sendTypingIndicator.js +40 -0
  78. package/src/apis/setMessageReaction.js +27 -0
  79. package/src/apis/setMessageReactionMqtt.js +61 -0
  80. package/src/apis/setPostReaction.js +118 -0
  81. package/src/apis/setThreadTheme.js +210 -0
  82. package/src/apis/setThreadThemeMqtt.js +94 -0
  83. package/src/apis/setTitle.js +26 -0
  84. package/src/apis/share.js +106 -0
  85. package/src/apis/shareContact.js +66 -0
  86. package/src/apis/stickers.js +257 -0
  87. package/src/apis/story.js +181 -0
  88. package/src/apis/theme.js +233 -0
  89. package/src/apis/unfriend.js +47 -0
  90. package/src/apis/unsendMessage.js +17 -0
  91. package/src/apis/uploadAttachment.js +87 -0
  92. package/src/database/appStateBackup.js +189 -0
  93. package/src/database/models/index.js +56 -0
  94. package/src/database/models/thread.js +31 -0
  95. package/src/database/models/user.js +32 -0
  96. package/src/database/threadData.js +101 -0
  97. package/src/database/userData.js +90 -0
  98. package/src/engine/client.js +92 -0
  99. package/src/engine/models/buildAPI.js +118 -0
  100. package/src/engine/models/loginHelper.js +492 -0
  101. package/src/engine/models/setOptions.js +88 -0
  102. package/src/types/index.d.ts +498 -0
  103. package/src/utils/antiSuspension.js +516 -0
  104. package/src/utils/auth-helpers.js +149 -0
  105. package/src/utils/autoReLogin.js +237 -0
  106. package/src/utils/axios.js +368 -0
  107. package/src/utils/cache.js +54 -0
  108. package/src/utils/clients.js +279 -0
  109. package/src/utils/constants.js +525 -0
  110. package/src/utils/formatters/data/formatAttachment.js +370 -0
  111. package/src/utils/formatters/data/formatDelta.js +109 -0
  112. package/src/utils/formatters/index.js +159 -0
  113. package/src/utils/formatters/value/formatCookie.js +91 -0
  114. package/src/utils/formatters/value/formatDate.js +36 -0
  115. package/src/utils/formatters/value/formatID.js +16 -0
  116. package/src/utils/formatters.js +1369 -0
  117. package/src/utils/headers.js +235 -0
  118. package/src/utils/index.js +152 -0
  119. package/src/utils/monitoring.js +333 -0
  120. package/src/utils/rateLimiter.js +251 -0
  121. package/src/utils/tokenRefresh.js +285 -0
  122. package/src/utils/user-agents.js +238 -0
  123. package/src/utils/validation.js +157 -0
@@ -0,0 +1,353 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+ const { globalAntiSuspension } = require('../utils/antiSuspension');
5
+
6
+ const allowedProperties = {
7
+ attachment: true,
8
+ url: true,
9
+ sticker: true,
10
+ emoji: true,
11
+ emojiSize: true,
12
+ body: true,
13
+ mentions: true,
14
+ location: true,
15
+ effect: true,
16
+ };
17
+
18
+ module.exports = (defaultFuncs, api, ctx) => {
19
+ function getThreadCache() {
20
+ if (!ctx.threadTypeCache) ctx.threadTypeCache = Object.create(null);
21
+ return ctx.threadTypeCache;
22
+ }
23
+
24
+ async function isGroupThread(threadID, explicitIsGroup) {
25
+ if (utils.getType(explicitIsGroup) === "Boolean") return !!explicitIsGroup;
26
+ const tid = threadID.toString();
27
+ const cache = getThreadCache();
28
+ if (Object.prototype.hasOwnProperty.call(cache, tid)) return !!cache[tid];
29
+ try {
30
+ const info = await api.getThreadInfo(tid);
31
+ cache[tid] = !!info.isGroup;
32
+ return !!info.isGroup;
33
+ } catch (_) {
34
+ const fallback = tid.length >= 16;
35
+ cache[tid] = fallback;
36
+ return fallback;
37
+ }
38
+ }
39
+
40
+ function detectAttachmentType(attachment) {
41
+ const path = attachment.path || '';
42
+ const ext = path.toLowerCase().split('.').pop();
43
+
44
+ const audioTypes = ['mp3', 'wav', 'aac', 'm4a', 'ogg', 'opus', 'flac'];
45
+ const videoTypes = ['mp4', 'mov', 'avi', 'mkv', 'webm', 'wmv', 'flv'];
46
+ const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
47
+
48
+ if (audioTypes.includes(ext)) return { voice_clip: "true" };
49
+ if (videoTypes.includes(ext)) return { video: "true" };
50
+ if (imageTypes.includes(ext)) return { image: "true" };
51
+ return { file: "true" };
52
+ }
53
+
54
+ async function uploadSingleAttachment(attachment, threadIDHint) {
55
+ if (!utils.isReadableStream(attachment)) {
56
+ throw new Error("Attachment should be a readable stream and not " + utils.getType(attachment) + ".");
57
+ }
58
+ const uploadType = detectAttachmentType(attachment);
59
+ const oksir = await defaultFuncs.postFormData(
60
+ "https://upload.facebook.com/ajax/mercury/upload.php",
61
+ ctx.jar,
62
+ { upload_1024: attachment, ...uploadType },
63
+ {},
64
+ { ...ctx, requestThreadID: threadIDHint }
65
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
66
+
67
+ if (oksir.error) throw new Error(JSON.stringify(oksir));
68
+ return oksir.payload.metadata[0];
69
+ }
70
+
71
+ async function uploadAttachment(attachments, threadIDHint) {
72
+ const CONCURRENT_UPLOADS = 2;
73
+ const uploads = [];
74
+ for (let i = 0; i < attachments.length; i += CONCURRENT_UPLOADS) {
75
+ const batch = attachments.slice(i, i + CONCURRENT_UPLOADS);
76
+ const results = await Promise.all(batch.map(a => uploadSingleAttachment(a, threadIDHint)));
77
+ uploads.push(...results);
78
+ if (i + CONCURRENT_UPLOADS < attachments.length) {
79
+ await globalAntiSuspension.addSmartDelay();
80
+ }
81
+ }
82
+ return uploads;
83
+ }
84
+
85
+ async function getUrl(url) {
86
+ const resData = await defaultFuncs.post(
87
+ "https://www.facebook.com/message_share_attachment/fromURI/",
88
+ ctx.jar,
89
+ { image_height: 960, image_width: 960, uri: url }
90
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
91
+ if (!resData || resData.error || !resData.payload) throw new Error("Invalid url");
92
+ return resData.payload.share_data.share_params;
93
+ }
94
+
95
+ async function sendContent(form, threadID, isSingleUser, messageAndOTID) {
96
+ if (utils.getType(threadID) === "Array") {
97
+ for (let i = 0; i < threadID.length; i++) {
98
+ form["specific_to_list[" + i + "]"] = "fbid:" + threadID[i];
99
+ }
100
+ form["specific_to_list[" + threadID.length + "]"] = "fbid:" + ctx.userID;
101
+ form["client_thread_id"] = "root:" + messageAndOTID;
102
+ utils.log("sendMessage", "Sending message to multiple users: " + threadID);
103
+ } else {
104
+ if (isSingleUser) {
105
+ form["specific_to_list[0]"] = "fbid:" + threadID;
106
+ form["specific_to_list[1]"] = "fbid:" + ctx.userID;
107
+ form["other_user_fbid"] = threadID;
108
+ form["client_thread_id"] = "root:" + messageAndOTID;
109
+ } else {
110
+ form["thread_fbid"] = threadID;
111
+ }
112
+ }
113
+
114
+ if (ctx.globalOptions.pageID) {
115
+ form["author"] = "fbid:" + ctx.globalOptions.pageID;
116
+ form["specific_to_list[1]"] = "fbid:" + ctx.globalOptions.pageID;
117
+ form["creator_info[creatorID]"] = ctx.userID;
118
+ form["creator_info[creatorType]"] = "direct_admin";
119
+ form["creator_info[labelType]"] = "sent_message";
120
+ form["creator_info[pageID]"] = ctx.globalOptions.pageID;
121
+ form["request_user_id"] = ctx.globalOptions.pageID;
122
+ form["creator_info[profileURI]"] = "https://www.facebook.com/profile.php?id=" + ctx.userID;
123
+ }
124
+
125
+ const resData = await defaultFuncs.post(
126
+ "https://www.facebook.com/messaging/send/",
127
+ ctx.jar,
128
+ form,
129
+ { ...ctx, requestThreadID: threadID }
130
+ ).then(utils.parseAndCheckLogin(ctx, defaultFuncs));
131
+
132
+ if (!resData) throw new Error("Send message failed.");
133
+ if (resData.error) {
134
+ if (resData.error === 1545012) {
135
+ utils.warn("sendMessage", "Got error 1545012. This might mean that you're not part of the conversation " + threadID);
136
+ }
137
+ // Check for suspension signals in error
138
+ globalAntiSuspension.detectSuspensionSignal(String(resData.error) + ' ' + JSON.stringify(resData));
139
+ throw new Error(JSON.stringify(resData));
140
+ }
141
+
142
+ const messageInfo = resData.payload.actions.reduce((p, v) => {
143
+ return { threadID: v.thread_fbid, messageID: v.message_id, timestamp: v.timestamp } || p;
144
+ }, null);
145
+ return messageInfo;
146
+ }
147
+
148
+ return async (msg, threadID, callback, replyToMessage, isGroup) => {
149
+ if (!callback && (utils.getType(threadID) === "Function" || utils.getType(threadID) === "AsyncFunction")) {
150
+ throw new Error("Pass a threadID as a second argument.");
151
+ }
152
+ if (!replyToMessage && utils.getType(callback) === "String") {
153
+ replyToMessage = callback;
154
+ callback = undefined;
155
+ }
156
+
157
+ let resolveFunc = () => {};
158
+ let rejectFunc = () => {};
159
+ let returnPromise = new Promise((resolve, reject) => {
160
+ resolveFunc = resolve;
161
+ rejectFunc = reject;
162
+ });
163
+
164
+ if (!callback) {
165
+ callback = (err, data) => {
166
+ if (err) return rejectFunc(err);
167
+ resolveFunc(data);
168
+ };
169
+ }
170
+
171
+ let msgType = utils.getType(msg);
172
+ let threadIDType = utils.getType(threadID);
173
+ let messageIDType = utils.getType(replyToMessage);
174
+
175
+ if (msgType !== "String" && msgType !== "Object") {
176
+ return callback(new Error("Message should be of type string or object and not " + msgType + "."));
177
+ }
178
+ if (threadIDType !== "Array" && threadIDType !== "Number" && threadIDType !== "String") {
179
+ return callback(new Error("ThreadID should be of type number, string, or array and not " + threadIDType + "."));
180
+ }
181
+ if (replyToMessage && messageIDType !== 'String') {
182
+ return callback(new Error("MessageID should be of type string and not " + threadIDType + "."));
183
+ }
184
+
185
+ if (!ctx.validator.isValidMessage(msg)) {
186
+ return callback(new Error("Invalid message content"));
187
+ }
188
+ const threadIDs = Array.isArray(threadID) ? threadID : [threadID];
189
+ if (!ctx.validator.validateIDArray(threadIDs, ctx.validator.isValidThreadID)) {
190
+ return callback(new Error("Invalid thread ID(s)"));
191
+ }
192
+
193
+ if (msgType === "String") msg = { body: msg };
194
+
195
+ let disallowedProperties = Object.keys(msg).filter(prop => !allowedProperties[prop]);
196
+ if (disallowedProperties.length > 0) {
197
+ return callback(new Error("Dissallowed props: `" + disallowedProperties.join(", ") + "`"));
198
+ }
199
+
200
+ try {
201
+ let messageAndOTID = utils.generateOfflineThreadingID();
202
+ let form = {
203
+ client: "mercury",
204
+ action_type: "ma-type:user-generated-message",
205
+ author: "fbid:" + ctx.userID,
206
+ timestamp: Date.now(),
207
+ timestamp_absolute: "Today",
208
+ timestamp_relative: utils.generateTimestampRelative(),
209
+ timestamp_time_passed: "0",
210
+ is_unread: false,
211
+ is_cleared: false,
212
+ is_forward: false,
213
+ is_filtered_content: false,
214
+ is_filtered_content_bh: false,
215
+ is_filtered_content_account: false,
216
+ is_filtered_content_quasar: false,
217
+ is_filtered_content_invalid_app: false,
218
+ is_spoof_warning: false,
219
+ source: "source:chat:web",
220
+ "source_tags[0]": "source:chat",
221
+ ...(msg.body && { body: msg.body }),
222
+ html_body: false,
223
+ ui_push_phase: "V3",
224
+ status: "0",
225
+ offline_threading_id: messageAndOTID,
226
+ message_id: messageAndOTID,
227
+ threading_id: utils.generateThreadingID(ctx.clientID),
228
+ "ephemeral_ttl_mode:": "0",
229
+ manual_retry_cnt: "0",
230
+ has_attachment: !!(msg.attachment || msg.url || msg.sticker),
231
+ signatureID: utils.getSignatureID(),
232
+ ...(replyToMessage && { replied_to_message_id: replyToMessage })
233
+ };
234
+
235
+ if (msg.location) {
236
+ if (!msg.location.latitude || !msg.location.longitude) {
237
+ return callback(new Error("location property needs both latitude and longitude"));
238
+ }
239
+ form["location_attachment[coordinates][latitude]"] = msg.location.latitude;
240
+ form["location_attachment[coordinates][longitude]"] = msg.location.longitude;
241
+ form["location_attachment[is_current_location]"] = !!msg.location.current;
242
+ }
243
+ if (msg.sticker) form["sticker_id"] = msg.sticker;
244
+ if (msg.effect) {
245
+ const effectTag = String(msg.effect).toUpperCase().replace(/[\s\-]+/g, '_');
246
+ const messagingTag = 'fb.messaging.effects.' + effectTag;
247
+ form.has_lightweight_action = true;
248
+ form['lightweight_action_attached[message_id]'] = messageAndOTID;
249
+ form['lightweight_action_attached[messaging_tag]'] = messagingTag;
250
+ }
251
+ if (msg.attachment) {
252
+ form.image_ids = [];
253
+ form.gif_ids = [];
254
+ form.file_ids = [];
255
+ form.video_ids = [];
256
+ form.audio_ids = [];
257
+ if (utils.getType(msg.attachment) !== "Array") msg.attachment = [msg.attachment];
258
+ const files = await uploadAttachment(msg.attachment, threadID);
259
+ files.forEach(file => {
260
+ const type = Object.keys(file)[0];
261
+ form["" + type + "s"].push(file[type]);
262
+ });
263
+ }
264
+ if (msg.url) {
265
+ form["shareable_attachment[share_type]"] = "100";
266
+ const params = await getUrl(msg.url);
267
+ form["shareable_attachment[share_params]"] = params;
268
+ }
269
+ if (msg.emoji) {
270
+ if (!msg.emojiSize) msg.emojiSize = "medium";
271
+ if (msg.emojiSize !== "small" && msg.emojiSize !== "medium" && msg.emojiSize !== "large") {
272
+ return callback(new Error("emojiSize property is invalid"));
273
+ }
274
+ if (form.body && form.body !== "") return callback(new Error("body is not empty"));
275
+ form.body = msg.emoji;
276
+ form["tags[0]"] = "hot_emoji_size:" + msg.emojiSize;
277
+ }
278
+ if (msg.mentions) {
279
+ for (let i = 0; i < msg.mentions.length; i++) {
280
+ const mention = msg.mentions[i];
281
+ const tag = mention.tag;
282
+ if (typeof tag !== "string") return callback(new Error("Mention tags must be strings."));
283
+ const offset = msg.body.indexOf(tag, mention.fromIndex || 0);
284
+ if (offset < 0) utils.warn("handleMention", 'Mention for "' + tag + '" not found in message string.');
285
+ if (!mention.id) utils.warn("handleMention", "Mention id should be non-null.");
286
+ const id = mention.id || 0;
287
+ const emptyChar = '\u200E';
288
+ form["body"] = emptyChar + msg.body;
289
+ form["profile_xmd[" + i + "][offset]"] = offset + 1;
290
+ form["profile_xmd[" + i + "][length]"] = tag.length;
291
+ form["profile_xmd[" + i + "][id]"] = id;
292
+ form["profile_xmd[" + i + "][type]"] = "p";
293
+ }
294
+ }
295
+
296
+ const isSingleUser = !(await isGroupThread(threadID, isGroup));
297
+
298
+ // ── Optimised anti-suspension send flow ───────────────────────────────
299
+ // Step 1: enforce thread throttle (single delay — no stacking).
300
+ await globalAntiSuspension.prepareBeforeMessage(threadID, msg.body || '');
301
+
302
+ // Step 2: start typing indicator BEFORE the typing delay so the delay
303
+ // is "hidden" inside the visible typing indicator — zero extra latency.
304
+ let typingStarted = false;
305
+ let typingTimeout;
306
+ const shouldSimulateTyping = ctx.globalOptions && ctx.globalOptions.simulateTyping && api.sendTypingIndicator;
307
+ if (shouldSimulateTyping) {
308
+ try {
309
+ await api.sendTypingIndicator(true, threadID);
310
+ typingStarted = true;
311
+
312
+ // Typing delay runs while the indicator is already showing.
313
+ const msgLen = (msg.body || '').length;
314
+ const typingMs = await globalAntiSuspension.simulateTyping(threadID, msgLen);
315
+ await new Promise(resolve => setTimeout(resolve, typingMs));
316
+
317
+ typingTimeout = setTimeout(() => {
318
+ if (typingStarted) {
319
+ try { api.sendTypingIndicator(false, threadID); } catch (_) {}
320
+ typingStarted = false;
321
+ }
322
+ }, 10000);
323
+ } catch (_) {}
324
+ }
325
+
326
+ // Step 3: send.
327
+ try {
328
+ const result = await sendContent(form, threadID, isSingleUser, messageAndOTID);
329
+ callback(null, result);
330
+ } catch (primaryErr) {
331
+ // Fallback to MQTT for group threads or when HTTP send fails.
332
+ if (api.sendMessageMqtt) {
333
+ try {
334
+ const mqttRes = await api.sendMessageMqtt(msg, threadID, replyToMessage);
335
+ callback(null, mqttRes);
336
+ } catch (fallbackErr) {
337
+ callback(primaryErr);
338
+ }
339
+ } else {
340
+ callback(primaryErr);
341
+ }
342
+ } finally {
343
+ if (typingTimeout) clearTimeout(typingTimeout);
344
+ if (typingStarted) {
345
+ try { await api.sendTypingIndicator(false, threadID); } catch (_) {}
346
+ }
347
+ }
348
+ } catch (err) {
349
+ callback(err);
350
+ }
351
+ return returnPromise;
352
+ };
353
+ };
@@ -0,0 +1,255 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+ const { globalAntiSuspension } = require('../utils/antiSuspension');
5
+
6
+ module.exports = (defaultFuncs, api, ctx) => {
7
+ function detectAttachmentType(attachment) {
8
+ const p = attachment.path || "";
9
+ const ext = p.toLowerCase().split(".").pop();
10
+ const audio = ["mp3", "wav", "aac", "m4a", "ogg", "opus", "flac"];
11
+ const video = ["mp4", "mov", "avi", "mkv", "webm", "wmv", "flv"];
12
+ const image = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"];
13
+ if (audio.includes(ext)) return { voice_clip: "true" };
14
+ if (video.includes(ext)) return { video: "true" };
15
+ if (image.includes(ext)) return { image: "true" };
16
+ return { file: "true" };
17
+ }
18
+
19
+ async function uploadAttachment(attachments, callback) {
20
+ callback = callback || function () {};
21
+ var uploads = [];
22
+ try {
23
+ for (var i = 0; i < attachments.length; i++) {
24
+ if (!utils.isReadableStream(attachments[i])) {
25
+ throw { error: "Attachment should be a readable stream and not " + utils.getType(attachments[i]) + "." };
26
+ }
27
+
28
+ if (i > 0) {
29
+ await globalAntiSuspension.addSmartDelay();
30
+ }
31
+
32
+ var form = {
33
+ upload_1024: attachments[i],
34
+ ...detectAttachmentType(attachments[i]),
35
+ };
36
+
37
+ const upload = await defaultFuncs
38
+ .postFormData("https://upload.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {}, { ...ctx, requestThreadID: String(ctx._lastThreadHint || "") })
39
+ .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
40
+ .then(resData => {
41
+ if (resData.error) throw resData;
42
+ return resData.payload.metadata[0];
43
+ });
44
+
45
+ uploads.push(upload);
46
+ }
47
+ callback(null, uploads);
48
+ } catch (err) {
49
+ utils.error("uploadAttachment", err);
50
+ return callback(err);
51
+ }
52
+ }
53
+
54
+ function getSendPayload(threadID, msg, otid) {
55
+ const isString = typeof msg === 'string';
56
+ const body = isString ? msg : msg.body || "";
57
+ otid = otid.toString() || utils.generateOfflineThreadingID().toString();
58
+ let payload = {
59
+ thread_id: threadID.toString(),
60
+ otid,
61
+ source: 0,
62
+ send_type: 1,
63
+ sync_group: 1,
64
+ text: body,
65
+ initiating_source: 1,
66
+ skip_url_preview_gen: 0,
67
+ };
68
+ if (typeof msg === 'object') {
69
+ if (msg.sticker) {
70
+ payload.send_type = 2;
71
+ payload.sticker_id = msg.sticker;
72
+ payload.text = null;
73
+ }
74
+ if (msg.attachment) {
75
+ payload.send_type = 3;
76
+ payload.attachment_fbids = Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment];
77
+ }
78
+ if (msg.effect) {
79
+ const effectTag = String(msg.effect).toUpperCase().replace(/[\s\-]+/g, '_');
80
+ payload.lightweight_action_attached = {
81
+ messaging_tag: 'fb.messaging.effects.' + effectTag,
82
+ };
83
+ }
84
+ }
85
+ return payload;
86
+ }
87
+
88
+ function extractIdsFromPayload(payload) {
89
+ let messageID = null;
90
+ let threadID = null;
91
+ function walk(n) {
92
+ if (Array.isArray(n)) {
93
+ if (n[0] === 5 && (n[1] === "replaceOptimsiticMessage" || n[1] === "replaceOptimisticMessage")) {
94
+ messageID = String(n[3]);
95
+ }
96
+ if (n[0] === 5 && n[1] === "writeCTAIdToThreadsTable") {
97
+ const a = n[2];
98
+ if (Array.isArray(a) && a[0] === 19) threadID = String(a[1]);
99
+ }
100
+ for (const x of n) walk(x);
101
+ }
102
+ }
103
+ walk(payload?.step);
104
+ return { threadID, messageID };
105
+ }
106
+
107
+ function publishWithAck(content, reqID, callback) {
108
+ return new Promise((resolve, reject) => {
109
+ if (!ctx.mqttClient || typeof ctx.mqttClient.on !== "function" || typeof ctx.mqttClient.publish !== "function") {
110
+ const err = new Error("MQTT client is not initialized");
111
+ utils.error("sendMessageMqtt", err);
112
+ callback && callback(err);
113
+ return reject(err);
114
+ }
115
+
116
+ if (typeof ctx.mqttClient.setMaxListeners === "function") {
117
+ ctx.mqttClient.setMaxListeners(0);
118
+ }
119
+
120
+ let done = false;
121
+ const cleanup = () => {
122
+ if (done) return;
123
+ done = true;
124
+ ctx.mqttClient.removeListener("message", handleRes);
125
+ };
126
+ const handleRes = (topic, message) => {
127
+ if (topic !== "/ls_resp") return;
128
+ let jsonMsg;
129
+ try {
130
+ jsonMsg = JSON.parse(message.toString());
131
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
132
+ } catch {
133
+ return;
134
+ }
135
+ if (jsonMsg.request_id !== reqID) return;
136
+ const { threadID, messageID } = extractIdsFromPayload(jsonMsg.payload);
137
+ const result = { messageID, threadID };
138
+ cleanup();
139
+ callback && callback(undefined, result);
140
+ resolve(result);
141
+ };
142
+ ctx.mqttClient.on("message", handleRes);
143
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(content), { qos: 1, retain: false }, err => {
144
+ if (err) {
145
+ cleanup();
146
+ callback && callback(err);
147
+ reject(err);
148
+ }
149
+ });
150
+ setTimeout(() => {
151
+ if (done) return;
152
+ cleanup();
153
+ const err = { error: "Timeout waiting for ACK" };
154
+ callback && callback(err);
155
+ reject(err);
156
+ }, 15000);
157
+ });
158
+ }
159
+
160
+ return async (msg, threadID, replyToMessage, callback) => {
161
+ if (typeof msg !== 'string' && typeof msg !== 'object') {
162
+ throw new Error("Message should be of type string or object, not " + utils.getType(msg) + ".");
163
+ }
164
+
165
+ if (typeof threadID !== 'string' && typeof threadID !== 'number') {
166
+ throw new Error("threadID must be a string or number.");
167
+ }
168
+
169
+ if (!callback && typeof threadID === "function") {
170
+ throw new Error("Pass a threadID as a second argument.");
171
+ }
172
+
173
+ if (!callback && typeof replyToMessage === "function") {
174
+ callback = replyToMessage;
175
+ replyToMessage = null;
176
+ }
177
+
178
+ // Apply anti-suspension throttling and volume checks before every MQTT send
179
+ try {
180
+ await globalAntiSuspension.prepareBeforeMessage(String(threadID), typeof msg === 'string' ? msg : (msg.body || ''));
181
+ } catch (suspErr) {
182
+ utils.warn("sendMessageMqtt", "Anti-suspension check raised:", suspErr && suspErr.message ? suspErr.message : suspErr);
183
+ }
184
+
185
+ const timestamp = Date.now();
186
+ const otid = utils.generateOfflineThreadingID();
187
+ const epoch_id = utils.generateOfflineThreadingID();
188
+ const payload = getSendPayload(threadID, msg, otid);
189
+
190
+ const tasks = [{
191
+ label: "46",
192
+ payload,
193
+ queue_name: threadID.toString(),
194
+ task_id: 0,
195
+ failure_count: null,
196
+ }, {
197
+ label: "21",
198
+ payload: {
199
+ thread_id: threadID.toString(),
200
+ last_read_watermark_ts: timestamp,
201
+ sync_group: 1,
202
+ },
203
+ queue_name: threadID.toString(),
204
+ task_id: 1,
205
+ failure_count: null,
206
+ }];
207
+
208
+ if (replyToMessage) {
209
+ tasks[0].payload.reply_metadata = {
210
+ reply_source_id: replyToMessage,
211
+ reply_source_type: 1,
212
+ reply_type: 0,
213
+ };
214
+ }
215
+
216
+ const request_id = ++ctx.wsReqNumber;
217
+ const form = {
218
+ app_id: "2220391788200892",
219
+ payload: {
220
+ tasks,
221
+ epoch_id,
222
+ version_id: "6120284488008082",
223
+ data_trace_id: null,
224
+ },
225
+ request_id,
226
+ type: 3,
227
+ };
228
+
229
+ if (msg.attachment) {
230
+ try {
231
+ ctx._lastThreadHint = threadID;
232
+ const files = await new Promise((resolve, reject) => {
233
+ uploadAttachment(
234
+ Array.isArray(msg.attachment) ? msg.attachment : [msg.attachment],
235
+ (err, files) => {
236
+ if (err) return reject(err);
237
+ return resolve(files);
238
+ }
239
+ );
240
+ });
241
+ form.payload.tasks[0].payload.attachment_fbids = files.map(file => Object.values(file)[0]);
242
+ } catch (err) {
243
+ utils.error("Attachment upload failed:", err);
244
+ throw err;
245
+ }
246
+ }
247
+
248
+ form.payload.tasks.forEach(task => {
249
+ task.payload = JSON.stringify(task.payload);
250
+ });
251
+ form.payload = JSON.stringify(form.payload);
252
+
253
+ return publishWithAck(form, request_id, callback);
254
+ };
255
+ };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+ /**
6
+ * @param {Object} defaultFuncs
7
+ * @param {Object} api
8
+ * @param {Object} ctx
9
+ */
10
+ module.exports = function (defaultFuncs, api, ctx) {
11
+ /**
12
+ * Sends a typing indicator to a specific thread.
13
+ * @param {boolean} sendTyping - True to show typing indicator, false to hide.
14
+ * @param {string} threadID - The ID of the thread to send the typing indicator to.
15
+ * @param {Function} [callback] - An optional callback function.
16
+ * @returns {Promise<void>}
17
+ */
18
+ return async function sendTypingIndicatorV2(sendTyping, threadID, callback) {
19
+ let count_req = 0;
20
+ const wsContent = {
21
+ app_id: 2220391788200892,
22
+ payload: JSON.stringify({
23
+ label: 3,
24
+ payload: JSON.stringify({
25
+ thread_key: threadID.toString(),
26
+ is_group_thread: +(threadID.toString().length >= 16),
27
+ is_typing: +sendTyping,
28
+ attribution: 0
29
+ }),
30
+ version: 5849951561777440
31
+ }),
32
+ request_id: ++count_req,
33
+ type: 4
34
+ };
35
+ await new Promise((resolve, reject) => ctx.mqttClient.publish('/ls_req', JSON.stringify(wsContent), {}, (err, _packet) => err ? reject(err) : resolve()));
36
+ if (callback) {
37
+ callback();
38
+ }
39
+ };
40
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ const utils = require('../utils');
4
+
5
+
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ return async (reaction, messageID) => {
8
+ if (!reaction) throw new Error("Please enter a valid emoji.");
9
+ const defData = await defaultFuncs.postFormData("https://www.facebook.com/webgraphql/mutation/", ctx.jar, {}, {
10
+ doc_id: "1491398900900362",
11
+ variables: JSON.stringify({
12
+ data: {
13
+ client_mutation_id: ctx.clientMutationId++,
14
+ actor_id: ctx.userID,
15
+ action: reaction == "" ? "REMOVE_REACTION" : "ADD_REACTION",
16
+ message_id: messageID,
17
+ reaction
18
+ }
19
+ }),
20
+ dpr: 1
21
+ });
22
+ const resData = await utils.parseAndCheckLogin(ctx, defaultFuncs)(defData);
23
+ if (!resData) {
24
+ throw new Error("setMessageReactionLegacy returned empty object.");
25
+ }
26
+ };
27
+ };