surya-sahil-fca 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.
@@ -0,0 +1,272 @@
1
+ /**
2
+ * Create by surya-sahil (Surya Sahil)
3
+ * Don't change credit
4
+ * Send a message using MQTT.
5
+ * @param {string} text - The text of the message to send.
6
+ * @param {string} threadID - The ID of the thread to send the message to.
7
+ * @param {string} [msgReplace] - Optional. The message ID of the message to replace.
8
+ * @param {Array<Buffer|Stream>} [attachments] - Optional. The attachments to send with the message.
9
+ * @param {function} [callback] - Optional. The callback function to call when the message is sent.
10
+ * @returns {Promise<object>} A promise that resolves with the bodies of the sent message.
11
+ */
12
+
13
+ "use strict";
14
+ const log = require("npmlog");
15
+ const { parseAndCheckLogin } = require("../../utils/client");
16
+ const { getType } = require("../../utils/format");
17
+ const { isReadableStream } = require("../../utils/constants");
18
+ const { generateOfflineThreadingID } = require("../../utils/format");
19
+
20
+ module.exports = function (defaultFuncs, api, ctx) {
21
+ const hasLinks = s => typeof s === "string" && /(https?:\/\/|www\.|t\.me\/|fb\.me\/|youtu\.be\/|facebook\.com\/|youtube\.com\/)/i.test(s);
22
+ const emojiSizes = { small: 1, medium: 2, large: 3 };
23
+
24
+ async function uploadAttachment(streams) {
25
+ const uploads = streams.map(stream => {
26
+ if (!isReadableStream(stream)) throw { error: "Attachment should be a readable stream and not " + getType(stream) + "." };
27
+ const form = { farr: stream };
28
+ return defaultFuncs
29
+ .postFormData("https://www.facebook.com/ajax/mercury/upload.php", ctx.jar, form, {})
30
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
31
+ .then(resData => {
32
+ if (resData.error) throw resData;
33
+ return resData.payload.metadata[0];
34
+ });
35
+ });
36
+ return Promise.all(uploads);
37
+ }
38
+
39
+ function extractIdsFromPayload(payload) {
40
+ let messageID = null;
41
+ let threadID = null;
42
+ function walk(n) {
43
+ if (Array.isArray(n)) {
44
+ if (n[0] === 5 && (n[1] === "replaceOptimsiticMessage" || n[1] === "replaceOptimisticMessage")) {
45
+ messageID = String(n[3]);
46
+ }
47
+ if (n[0] === 5 && n[1] === "writeCTAIdToThreadsTable") {
48
+ const a = n[2];
49
+ if (Array.isArray(a) && a[0] === 19) threadID = String(a[1]);
50
+ }
51
+ for (const x of n) walk(x);
52
+ }
53
+ }
54
+ walk(payload?.step);
55
+ return { threadID, messageID };
56
+ }
57
+
58
+ function publishWithAck(content, text, reqID, callback) {
59
+ return new Promise((resolve, reject) => {
60
+ let done = false;
61
+ const cleanup = () => {
62
+ if (done) return;
63
+ done = true;
64
+ ctx.mqttClient.removeListener("message", handleRes);
65
+ };
66
+ const handleRes = (topic, message) => {
67
+ if (topic !== "/ls_resp") return;
68
+ let jsonMsg;
69
+ try {
70
+ jsonMsg = JSON.parse(message.toString());
71
+ jsonMsg.payload = JSON.parse(jsonMsg.payload);
72
+ } catch {
73
+ return;
74
+ }
75
+ if (jsonMsg.request_id !== reqID) return;
76
+ const { threadID, messageID } = extractIdsFromPayload(jsonMsg.payload);
77
+ const bodies = { body: text || null, messageID, threadID };
78
+ cleanup();
79
+ callback && callback(undefined, bodies);
80
+ resolve(bodies);
81
+ };
82
+ ctx.mqttClient.on("message", handleRes);
83
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(content), { qos: 1, retain: false }, err => {
84
+ if (err) {
85
+ cleanup();
86
+ callback && callback(err);
87
+ reject(err);
88
+ }
89
+ });
90
+ setTimeout(() => {
91
+ if (done) return;
92
+ cleanup();
93
+ const err = { error: "Timeout waiting for ACK" };
94
+ callback && callback(err);
95
+ reject(err);
96
+ }, 15000);
97
+ });
98
+ }
99
+
100
+ function buildMentionData(msg, baseBody) {
101
+ if (!msg.mentions || !Array.isArray(msg.mentions) || !msg.mentions.length) return null;
102
+ const base = typeof baseBody === "string" ? baseBody : "";
103
+ const ids = [];
104
+ const offsets = [];
105
+ const lengths = [];
106
+ const types = [];
107
+ let cursor = 0;
108
+ for (const m of msg.mentions) {
109
+ const raw = String(m.tag || "");
110
+ const name = raw.replace(/^@+/, "");
111
+ const start = Number.isInteger(m.fromIndex) ? m.fromIndex : cursor;
112
+ let idx = base.indexOf(raw, start);
113
+ let adj = 0;
114
+ if (idx === -1) {
115
+ idx = base.indexOf(name, start);
116
+ adj = 0;
117
+ } else {
118
+ adj = raw.length - name.length;
119
+ }
120
+ if (idx < 0) {
121
+ idx = 0;
122
+ adj = 0;
123
+ }
124
+ const off = idx + adj;
125
+ ids.push(String(m.id || 0));
126
+ offsets.push(off);
127
+ lengths.push(name.length);
128
+ types.push("p");
129
+ cursor = off + name.length;
130
+ }
131
+ return {
132
+ mention_ids: ids.join(","),
133
+ mention_offsets: offsets.join(","),
134
+ mention_lengths: lengths.join(","),
135
+ mention_types: types.join(",")
136
+ };
137
+ }
138
+
139
+ function coerceMsg(x) {
140
+ if (x == null) return { body: "" };
141
+ if (typeof x === "string") return { body: x };
142
+ if (typeof x === "object") return x;
143
+ return { body: String(x) };
144
+ }
145
+
146
+ return async function sendMessageMqtt(msg, threadID, callback, replyToMessage) {
147
+ if (typeof threadID === "function") return threadID({ error: "Pass a threadID as a second argument." });
148
+ if (typeof callback === "string" && !replyToMessage) {
149
+ replyToMessage = callback;
150
+ callback = () => { };
151
+ }
152
+ if (typeof callback !== "function") callback = () => { };
153
+ if (!threadID) {
154
+ const err = { error: "threadID is required" };
155
+ callback(err);
156
+ throw err;
157
+ }
158
+
159
+ const m = coerceMsg(msg);
160
+ const baseBody = m.body != null ? String(m.body) : "";
161
+ const reqID = Math.floor(100 + Math.random() * 900);
162
+ const epoch = (BigInt(Date.now()) << 22n).toString();
163
+
164
+ const payload0 = {
165
+ thread_id: String(threadID),
166
+ otid: generateOfflineThreadingID(),
167
+ source: 2097153,
168
+ send_type: 1,
169
+ sync_group: 1,
170
+ mark_thread_read: 1,
171
+ text: baseBody === "" ? null : baseBody,
172
+ initiating_source: 0,
173
+ skip_url_preview_gen: 0,
174
+ text_has_links: hasLinks(baseBody) ? 1 : 0,
175
+ multitab_env: 0,
176
+ metadata_dataclass: JSON.stringify({ media_accessibility_metadata: { alt_text: null } })
177
+ };
178
+
179
+ const mentionData = buildMentionData(m, baseBody);
180
+ if (mentionData) payload0.mention_data = mentionData;
181
+
182
+ if (m.sticker) {
183
+ payload0.send_type = 2;
184
+ payload0.sticker_id = m.sticker;
185
+ }
186
+
187
+ if (m.emoji) {
188
+ const size = !isNaN(m.emojiSize) ? Number(m.emojiSize) : emojiSizes[m.emojiSize || "small"] || 1;
189
+ payload0.send_type = 1;
190
+ payload0.text = m.emoji;
191
+ payload0.hot_emoji_size = Math.min(3, Math.max(1, size));
192
+ }
193
+
194
+ if (m.location && m.location.latitude != null && m.location.longitude != null) {
195
+ payload0.send_type = 1;
196
+ payload0.location_data = {
197
+ coordinates: { latitude: m.location.latitude, longitude: m.location.longitude },
198
+ is_current_location: !!m.location.current,
199
+ is_live_location: !!m.location.live
200
+ };
201
+ }
202
+
203
+ if (replyToMessage) {
204
+ payload0.reply_metadata = { reply_source_id: replyToMessage, reply_source_type: 1, reply_type: 0 };
205
+ }
206
+
207
+ if (m.attachment) {
208
+ payload0.send_type = 3;
209
+ if (payload0.text === "") payload0.text = null;
210
+ payload0.attachment_fbids = [];
211
+ let list = m.attachment;
212
+ if (getType(list) !== "Array") list = [list];
213
+ const idsFromPairs = [];
214
+ const streams = [];
215
+ for (const it of list) {
216
+ if (Array.isArray(it) && typeof it[0] === "string") {
217
+ idsFromPairs.push(String(it[1]));
218
+ } else if (isReadableStream(it)) {
219
+ streams.push(it);
220
+ }
221
+ }
222
+ if (idsFromPairs.length) payload0.attachment_fbids.push(...idsFromPairs);
223
+ if (streams.length) {
224
+ try {
225
+ const files = await uploadAttachment(streams);
226
+ for (const file of files) {
227
+ const key = Object.keys(file)[0];
228
+ payload0.attachment_fbids.push(file[key]);
229
+ }
230
+ } catch (err) {
231
+ log.error("uploadAttachment", err);
232
+ callback(err);
233
+ throw err;
234
+ }
235
+ }
236
+ }
237
+
238
+ const content = {
239
+ app_id: "2220391788200892",
240
+ payload: {
241
+ tasks: [
242
+ {
243
+ label: "46",
244
+ payload: payload0,
245
+ queue_name: String(threadID),
246
+ task_id: 400,
247
+ failure_count: null
248
+ },
249
+ {
250
+ label: "21",
251
+ payload: {
252
+ thread_id: String(threadID),
253
+ last_read_watermark_ts: Date.now(),
254
+ sync_group: 1
255
+ },
256
+ queue_name: String(threadID),
257
+ task_id: 401,
258
+ failure_count: null
259
+ }
260
+ ],
261
+ epoch_id: epoch,
262
+ version_id: "24804310205905615",
263
+ data_trace_id: "#" + Buffer.from(String(Math.random())).toString("base64").replace(/=+$/g, "")
264
+ },
265
+ request_id: reqID,
266
+ type: 3
267
+ };
268
+ content.payload.tasks.forEach(t => (t.payload = JSON.stringify(t.payload)));
269
+ content.payload = JSON.stringify(content.payload);
270
+ return publishWithAck(content, baseBody, reqID, callback);
271
+ };
272
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ const { getType } = require("../../utils/format.js");
3
+ module.exports = function (defaultFuncs, api, ctx) {
4
+ return function sendTyping(threadID, isTyping, options, callback) {
5
+ var resolveFunc = function () { };
6
+ var rejectFunc = function () { };
7
+ var returnPromise = new Promise(function (resolve, reject) {
8
+ resolveFunc = resolve;
9
+ rejectFunc = reject;
10
+ });
11
+ if (getType(options) == "Function" || getType(options) == "AsyncFunction") {
12
+ callback = options;
13
+ options = {};
14
+ }
15
+ options = options || {};
16
+ if (!callback || getType(callback) != "Function" && getType(callback) != "AsyncFunction") {
17
+ callback = function (err, data) {
18
+ if (err) return rejectFunc(err);
19
+ resolveFunc(data);
20
+ };
21
+ }
22
+ if (!threadID) {
23
+ return callback(new Error("threadID is required"));
24
+ }
25
+ const threadIDs = Array.isArray(threadID) ? threadID : [threadID];
26
+ threadIDs.forEach(tid => {
27
+ var isGroupThread = getType(tid) === "Array" ? 0 : 1;
28
+ var threadType = isGroupThread ? 2 : 1;
29
+ var duration = options.duration || 10000;
30
+ var autoStop = options.autoStop !== false;
31
+ var attribution = options.type || 0;
32
+ const publishTypingStatus = (isTypingStatus) => {
33
+ ctx.mqttClient.publish('/ls_req',
34
+ JSON.stringify({
35
+ app_id: "772021112871879",
36
+ payload: JSON.stringify({
37
+ label: "3",
38
+ payload: JSON.stringify({
39
+ "thread_key": parseInt(tid),
40
+ "is_group_thread": isGroupThread,
41
+ "is_typing": isTypingStatus ? 1 : 0,
42
+ "attribution": attribution,
43
+ "sync_group": 1,
44
+ "thread_type": threadType
45
+ }),
46
+ version: "8965252033599983"
47
+ }),
48
+ request_id: ++ctx.req_ID,
49
+ type: 4
50
+ }),
51
+ {
52
+ qos: 1,
53
+ retain: false,
54
+ }
55
+ );
56
+ };
57
+ publishTypingStatus(isTyping);
58
+ if (isTyping && autoStop) {
59
+ setTimeout(() => {
60
+ publishTypingStatus(false);
61
+ }, duration);
62
+ }
63
+ });
64
+ callback(null, true);
65
+ return returnPromise;
66
+ };
67
+ };
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ const logger = require("../../../func/logger");
4
+ const { generateOfflineThreadingID, getCurrentTimestamp } = require("../../utils/format");
5
+
6
+ module.exports = function (defaultFuncs, api, ctx) {
7
+ return function setMessageReaction(reaction, messageID, threadID, callback) {
8
+ return new Promise((resolve, reject) => {
9
+ if (!ctx.mqttClient) {
10
+ const err = new Error("MQTT client not connected");
11
+ if (typeof callback === 'function') callback(err);
12
+ return reject(err);
13
+ }
14
+ if (!reaction || !messageID || !threadID) {
15
+ const err = new Error("Missing required parameters");
16
+ if (typeof callback === 'function') callback(err);
17
+ return reject(err);
18
+ }
19
+ const reqID = ++ctx.wsReqNumber;
20
+ const taskID = ++ctx.wsTaskNumber;
21
+ const taskPayload = {
22
+ thread_key: threadID,
23
+ timestamp_ms: getCurrentTimestamp(),
24
+ message_id: messageID,
25
+ reaction: reaction,
26
+ actor_id: ctx.userID,
27
+ reaction_style: null,
28
+ sync_group: 1,
29
+ send_attribution: 65537,
30
+ dataclass_params: null,
31
+ attachment_fbid: null
32
+ };
33
+ const task = {
34
+ failure_count: null,
35
+ label: "29",
36
+ payload: JSON.stringify(taskPayload),
37
+ queue_name: JSON.stringify(["reaction", messageID]),
38
+ task_id: taskID,
39
+ };
40
+ const mqttForm = {
41
+ app_id: "772021112871879",
42
+ payload: JSON.stringify({
43
+ data_trace_id: null,
44
+ epoch_id: parseInt(generateOfflineThreadingID()),
45
+ tasks: [task],
46
+ version_id: "25376272951962053"
47
+ }),
48
+ request_id: reqID,
49
+ type: 3
50
+ };
51
+ const handleResponse = (topic, message) => {
52
+ if (topic !== "/ls_resp") return;
53
+ let json;
54
+ try {
55
+ json = JSON.parse(message.toString());
56
+ json.payload = JSON.parse(json.payload);
57
+ } catch {
58
+ return;
59
+ }
60
+ if (json.request_id !== reqID) return;
61
+ ctx.mqttClient.removeListener("message", handleResponse);
62
+ if (typeof callback === 'function') callback(null, { success: true });
63
+ return resolve({ success: true });
64
+ };
65
+ ctx.mqttClient.on("message", handleResponse);
66
+ ctx.mqttClient.publish("/ls_req", JSON.stringify(mqttForm), { qos: 1, retain: false }, (err) => {
67
+ if (err) {
68
+ ctx.mqttClient.removeListener("message", handleResponse);
69
+ logger("setMessageReaction" + err, "error");
70
+ if (typeof callback === 'function') callback(err);
71
+ return reject(err);
72
+ }
73
+ });
74
+ });
75
+ };
76
+ };
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+
3
+ const { getType, generateOfflineThreadingID, generateTimestampRelative, generateThreadingID, getCurrentTimestamp } = require("../../utils/format");
4
+ const { parseAndCheckLogin } = require("../../utils/client");
5
+ const log = require("npmlog");
6
+
7
+ module.exports = function (defaultFuncs, api, ctx) {
8
+ function setTitleNoMqtt(newTitle, threadID, callback) {
9
+ if (!callback && (getType(threadID) === "Function" || getType(threadID) === "AsyncFunction")) throw { error: "please pass a threadID as a second argument." };
10
+ var resolveFunc = function () { };
11
+ var rejectFunc = function () { };
12
+ var returnPromise = new Promise(function (resolve, reject) {
13
+ resolveFunc = resolve;
14
+ rejectFunc = reject;
15
+ });
16
+ if (!callback) {
17
+ callback = function (err, data) {
18
+ if (err) return rejectFunc(err);
19
+ resolveFunc(data);
20
+ };
21
+ }
22
+ var messageAndOTID = generateOfflineThreadingID();
23
+ var form = {
24
+ client: "mercury",
25
+ action_type: "ma-type:log-message",
26
+ author: "fbid:" + ctx.userID,
27
+ author_email: "",
28
+ coordinates: "",
29
+ timestamp: Date.now(),
30
+ timestamp_absolute: "Today",
31
+ timestamp_relative: generateTimestampRelative(),
32
+ timestamp_time_passed: "0",
33
+ is_unread: false,
34
+ is_cleared: false,
35
+ is_forward: false,
36
+ is_filtered_content: false,
37
+ is_spoof_warning: false,
38
+ source: "source:chat:web",
39
+ "source_tags[0]": "source:chat",
40
+ status: "0",
41
+ offline_threading_id: messageAndOTID,
42
+ message_id: messageAndOTID,
43
+ threading_id: generateThreadingID(ctx.clientID),
44
+ manual_retry_cnt: "0",
45
+ thread_fbid: threadID,
46
+ thread_name: newTitle,
47
+ thread_id: threadID,
48
+ log_message_type: "log:thread-name"
49
+ };
50
+ defaultFuncs
51
+ .post("https://www.facebook.com/messaging/set_thread_name/", ctx.jar, form)
52
+ .then(parseAndCheckLogin(ctx, defaultFuncs))
53
+ .then(function (resData) {
54
+ if (resData.error && resData.error === 1545012) throw { error: "Cannot change chat title: Not member of chat." };
55
+ if (resData.error && resData.error === 1545003) throw { error: "Cannot set title of single-user chat." };
56
+ if (resData.error) throw resData;
57
+ return callback();
58
+ })
59
+ .catch(function (err) {
60
+ log.error("setTitle", err);
61
+ return callback(err);
62
+ });
63
+ return returnPromise;
64
+ };
65
+ function setTitleMqtt(newTitle, threadID, callback) {
66
+ if (!ctx.mqttClient) {
67
+ throw new Error("Not connected to MQTT");
68
+ }
69
+ var resolveFunc = function () { };
70
+ var rejectFunc = function () { };
71
+ var returnPromise = new Promise(function (resolve, reject) {
72
+ resolveFunc = resolve;
73
+ rejectFunc = reject;
74
+ });
75
+ if (!callback) {
76
+ callback = function (err, data) {
77
+ if (err) return rejectFunc(err);
78
+ resolveFunc(data);
79
+ data
80
+ };
81
+ }
82
+ let count_req = 0
83
+ var form = JSON.stringify({
84
+ "app_id": "2220391788200892",
85
+ "payload": JSON.stringify({
86
+ epoch_id: generateOfflineThreadingID(),
87
+ tasks: [
88
+ {
89
+ failure_count: null,
90
+ label: '32',
91
+ payload: JSON.stringify({
92
+ "thread_key": threadID,
93
+ "thread_name": newTitle,
94
+ "sync_group": 1
95
+ }),
96
+ queue_name: threadID,
97
+ task_id: Math.random() * 1001 << 0
98
+ }
99
+ ],
100
+ version_id: '8798795233522156'
101
+ }),
102
+ "request_id": ++count_req,
103
+ "type": 3
104
+ });
105
+ mqttClient.publish('/ls_req', form);
106
+ return returnPromise;
107
+ };
108
+ return function setTitle(newTitle, threadID, callback) {
109
+ if (ctx.mqttClient) {
110
+ try {
111
+ setTitleMqtt(newTitle, threadID, callback);
112
+ } catch (e) {
113
+ setTitleNoMqtt(newTitle, threadID, callback);
114
+ }
115
+ } else {
116
+ setTitleNoMqtt(newTitle, threadID, callback);
117
+ }
118
+ };
119
+ };