stfca 1.0.22 → 1.0.24

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/index.js CHANGED
@@ -192,13 +192,83 @@ function buildAPI(globalOptions, html, jar) {
192
192
  };
193
193
  let config = { enableTypingIndicator: false, typingDuration: 4000 };
194
194
  try {
195
- const configPath = path.join(__dirname, 'config.json');
196
- if (fs.existsSync(configPath)) {
197
- config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
195
+ // Prefer global root config (project-level), but fallback to fca/config.json if present.
196
+ const rootConfigPath = path.join(process.cwd(), 'config.json');
197
+ if (fs.existsSync(rootConfigPath)) {
198
+ const rootConfig = JSON.parse(fs.readFileSync(rootConfigPath, 'utf8'));
199
+ if (rootConfig && typeof rootConfig === 'object') {
200
+ if (typeof rootConfig.enableTypingIndicator !== 'undefined') config.enableTypingIndicator = rootConfig.enableTypingIndicator;
201
+ if (typeof rootConfig.typingDuration !== 'undefined') config.typingDuration = rootConfig.typingDuration;
202
+ }
203
+ }
204
+
205
+ const fcaConfigPath = path.join(__dirname, 'config.json');
206
+ if (fs.existsSync(fcaConfigPath)) {
207
+ const fcaConfig = JSON.parse(fs.readFileSync(fcaConfigPath, 'utf8'));
208
+ if (fcaConfig && typeof fcaConfig === 'object') {
209
+ if (typeof fcaConfig.enableTypingIndicator !== 'undefined') config.enableTypingIndicator = fcaConfig.enableTypingIndicator;
210
+ if (typeof fcaConfig.typingDuration !== 'undefined') config.typingDuration = fcaConfig.typingDuration;
211
+ }
212
+ }
213
+
214
+ if (global.GoatBot && global.GoatBot.config) {
215
+ if (typeof global.GoatBot.config.enableTypingIndicator !== 'undefined') config.enableTypingIndicator = global.GoatBot.config.enableTypingIndicator;
216
+ if (typeof global.GoatBot.config.typingDuration !== 'undefined') config.typingDuration = global.GoatBot.config.typingDuration;
198
217
  }
199
218
  } catch (e) {
200
219
  console.log('Error loading config.json:', e);
201
220
  }
221
+
222
+ const refreshFcaConfig = () => {
223
+ try {
224
+ // Defaults first
225
+ const updatedConfig = { enableTypingIndicator: false, typingDuration: 4000 };
226
+
227
+ // Layered config sources
228
+ if (fs.existsSync(path.join(process.cwd(), 'config.json'))) {
229
+ const rootConfig = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'config.json'), 'utf8'));
230
+ if (rootConfig && typeof rootConfig === 'object') {
231
+ if (typeof rootConfig.enableTypingIndicator !== 'undefined') updatedConfig.enableTypingIndicator = rootConfig.enableTypingIndicator;
232
+ if (typeof rootConfig.typingDuration !== 'undefined') updatedConfig.typingDuration = rootConfig.typingDuration;
233
+ }
234
+ }
235
+
236
+ if (fs.existsSync(path.join(__dirname, 'config.json'))) {
237
+ const fcaConfig = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
238
+ if (fcaConfig && typeof fcaConfig === 'object') {
239
+ if (typeof fcaConfig.enableTypingIndicator !== 'undefined') updatedConfig.enableTypingIndicator = fcaConfig.enableTypingIndicator;
240
+ if (typeof fcaConfig.typingDuration !== 'undefined') updatedConfig.typingDuration = fcaConfig.typingDuration;
241
+ }
242
+ }
243
+
244
+ if (global.GoatBot && global.GoatBot.config) {
245
+ if (typeof global.GoatBot.config.enableTypingIndicator !== 'undefined') updatedConfig.enableTypingIndicator = global.GoatBot.config.enableTypingIndicator;
246
+ if (typeof global.GoatBot.config.typingDuration !== 'undefined') updatedConfig.typingDuration = global.GoatBot.config.typingDuration;
247
+ }
248
+
249
+ ctx.config = updatedConfig;
250
+ config = updatedConfig;
251
+ if (global.GoatBot) global.GoatBot.config = global.GoatBot.config || {};
252
+ if (global.GoatBot && typeof global.GoatBot.config.enableTypingIndicator !== 'undefined') {
253
+ global.GoatBot.config.enableTypingIndicator = updatedConfig.enableTypingIndicator;
254
+ }
255
+ if (global.GoatBot && typeof global.GoatBot.config.typingDuration !== 'undefined') {
256
+ global.GoatBot.config.typingDuration = updatedConfig.typingDuration;
257
+ }
258
+ } catch (e) {
259
+ console.log('Failed to refresh fca config:', e);
260
+ }
261
+ };
262
+
263
+ // Initial config load
264
+ refreshFcaConfig();
265
+
266
+ // Accessible runtime API for config reload
267
+ ctx.refreshFcaConfig = refreshFcaConfig;
268
+ if (global.GoatBot) {
269
+ global.GoatBot.refreshFcaConfig = refreshFcaConfig;
270
+ }
271
+
202
272
  ctx.config = config;
203
273
  var api = {
204
274
  setOptions: setOptions.bind(null, globalOptions),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stfca",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "Unofficial Facebook Chat API for Node.js with Auto-Update System - Enhanced by ST | Sheikh Tamim",
5
5
  "main": "index.js",
6
6
  "files": [
package/src/OldMessage.js CHANGED
@@ -311,11 +311,15 @@ module.exports = function (defaultFuncs, api, ctx) {
311
311
  };
312
312
  // console.log(form)
313
313
 
314
- if (isSingleUser && ctx.config.enableTypingIndicator) {
315
- api.sendTypingIndicatorV2(true, threadID, () => {});
314
+ const configSource = (global.GoatBot && global.GoatBot.config) ? global.GoatBot.config : ctx.config || {};
315
+ const enableTypingIndicator = typeof configSource.enableTypingIndicator !== 'undefined' ? configSource.enableTypingIndicator : ctx.config?.enableTypingIndicator;
316
+ const typingDuration = Number(configSource.typingDuration || ctx.config?.typingDuration || 4000);
317
+
318
+ if (enableTypingIndicator) {
319
+ api.sendTypingIndicator(true, threadID, () => {});
316
320
  const originalCallback = callback;
317
321
  callback = (err, data) => {
318
- api.sendTypingIndicatorV2(false, threadID, () => {});
322
+ api.sendTypingIndicator(false, threadID, () => {});
319
323
  originalCallback(err, data);
320
324
  };
321
325
  setTimeout(() => {
@@ -332,7 +336,7 @@ module.exports = function (defaultFuncs, api, ctx) {
332
336
  )
333
337
  )
334
338
  );
335
- }, ctx.config.typingDuration);
339
+ }, typingDuration);
336
340
  } else {
337
341
  handleLocation(msg, form, callback, () =>
338
342
  handleSticker(msg, form, callback, () =>
@@ -234,11 +234,15 @@ module.exports = (defaultFuncs, api, ctx) => {
234
234
  form["profile_xmd[" + i + "][type]"] = "p";
235
235
  }
236
236
  }
237
- if (isSingleUser && ctx.config.enableTypingIndicator) {
238
- await api.sendTypingIndicatorV2(true, threadID, () => {});
239
- await utils.delay(ctx.config.typingDuration);
237
+ const configSource = (global.GoatBot && global.GoatBot.config) ? global.GoatBot.config : ctx.config || {};
238
+ const enableTypingIndicator = typeof configSource.enableTypingIndicator !== 'undefined' ? configSource.enableTypingIndicator : ctx.config?.enableTypingIndicator;
239
+ const typingDuration = Number(configSource.typingDuration || ctx.config?.typingDuration || 4000);
240
+
241
+ if (enableTypingIndicator) {
242
+ await api.sendTypingIndicator(true, threadID, () => {});
243
+ await utils.delay(typingDuration);
240
244
  const result = await sendContent(form, threadID, isSingleUser, messageAndOTID);
241
- await api.sendTypingIndicatorV2(false, threadID, () => {});
245
+ await api.sendTypingIndicator(false, threadID, () => {});
242
246
  if (callback && typeof callback === "function") {
243
247
  callback(null, result);
244
248
  }
@@ -1,101 +1,45 @@
1
- "use strict";
2
-
3
- const utils = require("../utils");
4
- // @NethWs3Dev
5
-
6
- module.exports = function (defaultFuncs, api, ctx) {
7
- function makeTypingIndicator(typ, threadID, callback, isGroup) {
8
- const form = {
9
- typ: +typ,
10
- to: "",
11
- source: "mercury-chat",
12
- thread: threadID,
13
- };
14
-
15
- // Check if thread is a single person chat or a group chat
16
- // More info on this is in api.sendMessage
17
- if (utils.getType(isGroup) == "Boolean") {
18
- if (!isGroup) {
19
- form.to = threadID;
20
- }
21
- defaultFuncs
22
- .post("https://www.facebook.com/ajax/messaging/typ.php", ctx.jar, form)
23
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
24
- .then(function (resData) {
25
- if (resData.error) {
26
- throw resData;
27
- }
28
-
29
- return callback();
30
- })
31
- .catch(function (err) {
32
- console.error("sendTypingIndicator", err);
33
- return callback(err);
34
- });
35
- } else {
36
- api.getUserInfo(threadID, function (err, res) {
37
- if (err) {
38
- return callback(err);
39
- }
40
-
41
- // If id is single person chat
42
- if (Object.keys(res).length > 0) {
43
- form.to = threadID;
44
- }
45
-
46
- defaultFuncs
47
- .post(
48
- "https://www.facebook.com/ajax/messaging/typ.php",
49
- ctx.jar,
50
- form,
51
- )
52
- .then(utils.parseAndCheckLogin(ctx, defaultFuncs))
53
- .then(function (resData) {
54
- if (resData.error) {
55
- throw resData;
56
- }
57
-
58
- return callback();
59
- })
60
- .catch(function (err) {
61
- console.error("sendTypingIndicator", err);
62
- return callback(err);
63
- });
64
- });
65
- }
66
- }
67
-
68
- return function sendTypingIndicator(threadID, callback, isGroup) {
69
- if (
70
- utils.getType(callback) !== "Function" &&
71
- utils.getType(callback) !== "AsyncFunction"
72
- ) {
73
- if (callback) {
74
- console.warn(
75
- "sendTypingIndicator",
76
- "callback is not a function - ignoring.",
77
- );
78
- }
79
- callback = () => {};
80
- }
81
-
82
- makeTypingIndicator(true, threadID, callback, isGroup);
83
-
84
- return function end(cb) {
85
- if (
86
- utils.getType(cb) !== "Function" &&
87
- utils.getType(cb) !== "AsyncFunction"
88
- ) {
89
- if (cb) {
90
- console.warn(
91
- "sendTypingIndicator",
92
- "callback is not a function - ignoring.",
93
- );
94
- }
95
- cb = () => {};
96
- }
97
-
98
- makeTypingIndicator(false, threadID, cb, isGroup);
99
- };
100
- };
101
- };
1
+ "use strict";
2
+
3
+
4
+
5
+ var utils = require("../utils");
6
+ // @NethWs3Dev
7
+
8
+ module.exports = function (defaultFuncs, api, ctx) {
9
+ return async function sendTypingIndicatorV2(sendTyping, threadID, callback) {
10
+ const mqttClient = ctx.mqttClient || global.mqttClient;
11
+ if (!mqttClient) {
12
+ if (typeof callback === 'function') callback(new Error('No MQTT client available for typing indicator'));
13
+ return;
14
+ }
15
+
16
+ let count_req = 0;
17
+ var wsContent = {
18
+ app_id: 2220391788200892,
19
+ payload: JSON.stringify({
20
+ label: 3,
21
+ payload: JSON.stringify({
22
+ thread_key: threadID.toString(),
23
+ is_group_thread: +(threadID.toString().length >= 16),
24
+ is_typing: +sendTyping,
25
+ attribution: 0
26
+ }),
27
+ version: 5849951561777440
28
+ }),
29
+ request_id: ++count_req,
30
+ type: 4
31
+ };
32
+
33
+ return new Promise((resolve, reject) => {
34
+ mqttClient.publish('/ls_req', JSON.stringify(wsContent), {}, (err, _packet) => {
35
+ if (err) {
36
+ if (typeof callback === 'function') callback(err);
37
+ reject(err);
38
+ } else {
39
+ if (typeof callback === 'function') callback(null, _packet);
40
+ resolve(_packet);
41
+ }
42
+ });
43
+ });
44
+ };
45
+ };
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
-
4
-
5
- var utils = require("../utils");
6
- // @NethWs3Dev
7
-
8
- module.exports = function (defaultFuncs, api, ctx) {
9
- return async function sendTypingIndicatorV2(sendTyping,threadID, callback) {
10
- let count_req = 0
11
- var wsContent = {
12
- app_id: 2220391788200892,
13
- payload: JSON.stringify({
14
- label: 3,
15
- payload: JSON.stringify({
16
- thread_key: threadID.toString(),
17
- is_group_thread: +(threadID.toString().length >= 16),
18
- is_typing: +sendTyping,
19
- attribution: 0
20
- }),
21
- version: 5849951561777440
22
- }),
23
- request_id: ++count_req,
24
- type: 4
25
- };
26
- await new Promise((resolve, reject) => mqttClient.publish('/ls_req', JSON.stringify(wsContent), {}, (err, _packet) => err ? reject(err) : resolve()));
27
- };
28
- };