@dongdev/fca-unofficial 0.0.3 → 0.0.5
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/.travis.yml +6 -6
- package/CHANGELOG.md +1 -1
- package/DOCS.md +1738 -1738
- package/LICENSE-MIT +21 -21
- package/README.md +219 -219
- package/index.js +267 -569
- package/lib/login.js +0 -0
- package/package.json +3 -3
- package/src/addExternalModule.js +19 -15
- package/src/addUserToGroup.js +113 -77
- package/src/changeAdminStatus.js +79 -47
- package/src/changeArchivedStatus.js +55 -41
- package/src/changeAvatar.js +126 -0
- package/src/changeBio.js +66 -54
- package/src/changeBlockedStatus.js +40 -29
- package/src/changeGroupImage.js +127 -101
- package/src/changeNickname.js +50 -36
- package/src/changeThreadColor.js +65 -61
- package/src/changeThreadEmoji.js +55 -41
- package/src/createNewGroup.js +86 -70
- package/src/createPoll.js +71 -59
- package/src/deleteMessage.js +56 -44
- package/src/deleteThread.js +56 -42
- package/src/forwardAttachment.js +60 -47
- package/src/getCurrentUserID.js +7 -7
- package/src/getEmojiUrl.js +29 -27
- package/src/getFriendsList.js +83 -73
- package/src/getMessage.js +796 -0
- package/src/getThreadHistory.js +666 -537
- package/src/getThreadInfo.js +232 -171
- package/src/getThreadList.js +241 -213
- package/src/getThreadPictures.js +79 -59
- package/src/getUserID.js +66 -61
- package/src/getUserInfo.js +74 -66
- package/src/handleFriendRequest.js +61 -46
- package/src/handleMessageRequest.js +65 -47
- package/src/httpGet.js +52 -44
- package/src/httpPost.js +52 -43
- package/src/httpPostFormData.js +63 -0
- package/src/listenMqtt.js +877 -709
- package/src/logout.js +62 -55
- package/src/markAsDelivered.js +58 -47
- package/src/markAsRead.js +80 -70
- package/src/markAsReadAll.js +49 -39
- package/src/markAsSeen.js +59 -48
- package/src/muteThread.js +52 -45
- package/src/postFormData.js +46 -0
- package/src/refreshFb_dtsg.js +81 -0
- package/src/removeUserFromGroup.js +79 -45
- package/src/resolvePhotoUrl.js +45 -36
- package/src/searchForThread.js +53 -42
- package/src/sendMessage.js +328 -328
- package/src/sendMessageMqtt.js +316 -0
- package/src/sendTypingIndicator.js +103 -70
- package/src/setMessageReaction.js +106 -98
- package/src/setPostReaction.js +102 -95
- package/src/setTitle.js +86 -70
- package/src/threadColors.js +131 -41
- package/src/unfriend.js +52 -42
- package/src/unsendMessage.js +49 -39
- package/src/uploadAttachment.js +95 -0
- package/utils.js +1470 -1196
- package/.gitattributes +0 -2
- package/src/Screenshot.js +0 -83
- package/src/changeAvt.js +0 -85
- package/src/getThreadHistoryDeprecated.js +0 -71
- package/src/getThreadInfoDeprecated.js +0 -56
- package/src/getThreadListDeprecated.js +0 -46
- package/src/shareContact.js +0 -46
- package/test/data/shareAttach.js +0 -146
- package/test/data/something.mov +0 -0
- package/test/data/test.png +0 -0
- package/test/data/test.txt +0 -7
- package/test/example-config.json +0 -18
- package/test/test-page.js +0 -140
- package/test/test.js +0 -385
package/src/listenMqtt.js
CHANGED
@@ -1,710 +1,878 @@
|
|
1
|
-
/* eslint-disable no-redeclare */
|
2
|
-
"use strict";
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
const EventEmitter = require('events');
|
9
|
-
|
10
|
-
var identity = function () { };
|
11
|
-
var form = {};
|
12
|
-
var
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
}
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
}
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
1
|
+
/* eslint-disable no-redeclare */
|
2
|
+
"use strict";
|
3
|
+
const utils = require("../utils");
|
4
|
+
const log = require("npmlog");
|
5
|
+
const mqtt = require('mqtt');
|
6
|
+
const websocket = require('websocket-stream');
|
7
|
+
const HttpsProxyAgent = require('https-proxy-agent');
|
8
|
+
const EventEmitter = require('events');
|
9
|
+
const debugSeq = false;
|
10
|
+
var identity = function () { };
|
11
|
+
var form = {};
|
12
|
+
var getSeqId = function () { };
|
13
|
+
|
14
|
+
const topics = [
|
15
|
+
"/legacy_web",
|
16
|
+
"/webrtc",
|
17
|
+
"/rtc_multi",
|
18
|
+
"/onevc",
|
19
|
+
"/br_sr", //Notification
|
20
|
+
//Need to publish /br_sr right after this
|
21
|
+
"/sr_res",
|
22
|
+
"/t_ms",
|
23
|
+
"/thread_typing",
|
24
|
+
"/orca_typing_notifications",
|
25
|
+
"/notify_disconnect",
|
26
|
+
//Need to publish /messenger_sync_create_queue right after this
|
27
|
+
"/orca_presence",
|
28
|
+
//Will receive /sr_res right here.
|
29
|
+
|
30
|
+
"/legacy_web_mtouch"
|
31
|
+
// "/inbox",
|
32
|
+
// "/mercury",
|
33
|
+
// "/messaging_events",
|
34
|
+
// "/orca_message_notifications",
|
35
|
+
// "/pp",
|
36
|
+
// "/webrtc_response",
|
37
|
+
];
|
38
|
+
|
39
|
+
function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
|
40
|
+
const chatOn = ctx.globalOptions.online;
|
41
|
+
const foreground = false;
|
42
|
+
const sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
|
43
|
+
const username = {
|
44
|
+
u: ctx.i_userID || ctx.userID,
|
45
|
+
s: sessionID,
|
46
|
+
chat_on: chatOn,
|
47
|
+
fg: foreground,
|
48
|
+
d: utils.getGUID(),
|
49
|
+
ct: "websocket",
|
50
|
+
aid: "219994525426954",
|
51
|
+
mqtt_sid: "",
|
52
|
+
cp: 3,
|
53
|
+
ecp: 10,
|
54
|
+
st: [],
|
55
|
+
pm: [],
|
56
|
+
dc: "",
|
57
|
+
no_auto_fg: true,
|
58
|
+
gas: null,
|
59
|
+
pack: [],
|
60
|
+
a: ctx.globalOptions.userAgent,
|
61
|
+
aids: null
|
62
|
+
};
|
63
|
+
const cookies = ctx.jar.getCookies("https://www.facebook.com").join("; ");
|
64
|
+
let host;
|
65
|
+
if (ctx.mqttEndpoint) {
|
66
|
+
host = `${ctx.mqttEndpoint}&sid=${sessionID}`;
|
67
|
+
} else if (ctx.region) {
|
68
|
+
host = `wss://edge-chat.facebook.com/chat?region=${ctx.region.toLocaleLowerCase()}&sid=${sessionID}`;
|
69
|
+
} else {
|
70
|
+
host = `wss://edge-chat.facebook.com/chat?sid=${sessionID}`;
|
71
|
+
}
|
72
|
+
const options = {
|
73
|
+
clientId: "mqttwsclient",
|
74
|
+
protocolId: 'MQIsdp',
|
75
|
+
protocolVersion: 3,
|
76
|
+
username: JSON.stringify(username),
|
77
|
+
clean: true,
|
78
|
+
wsOptions: {
|
79
|
+
headers: {
|
80
|
+
'Cookie': cookies,
|
81
|
+
'Origin': 'https://www.facebook.com',
|
82
|
+
'User-Agent': ctx.globalOptions.userAgent,
|
83
|
+
'Referer': 'https://www.facebook.com/',
|
84
|
+
'Host': new URL(host).hostname
|
85
|
+
},
|
86
|
+
origin: 'https://www.facebook.com',
|
87
|
+
protocolVersion: 13
|
88
|
+
},
|
89
|
+
keepalive: 10,
|
90
|
+
reschedulePings: false
|
91
|
+
};
|
92
|
+
if (typeof ctx.globalOptions.proxy != "undefined") {
|
93
|
+
const agent = new HttpsProxyAgent(ctx.globalOptions.proxy);
|
94
|
+
options.wsOptions.agent = agent;
|
95
|
+
}
|
96
|
+
ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
|
97
|
+
const mqttClient = ctx.mqttClient;
|
98
|
+
global.mqttClient = mqttClient;
|
99
|
+
mqttClient.on('error', function (err) {
|
100
|
+
log.error("listenMqtt", err);
|
101
|
+
mqttClient.end();
|
102
|
+
if (ctx.globalOptions.autoReconnect) {
|
103
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
104
|
+
} else {
|
105
|
+
utils.checkLiveCookie(ctx, defaultFuncs)
|
106
|
+
.then(res => {
|
107
|
+
globalCallback({
|
108
|
+
type: "stop_listen",
|
109
|
+
error: "Connection refused: Server unavailable"
|
110
|
+
}, null);
|
111
|
+
})
|
112
|
+
.catch(err => {
|
113
|
+
globalCallback({
|
114
|
+
type: "account_inactive",
|
115
|
+
error: "Maybe your account is blocked by facebook, please login and check at https://facebook.com"
|
116
|
+
}, null);
|
117
|
+
});
|
118
|
+
}
|
119
|
+
});
|
120
|
+
mqttClient.on('close', function () {
|
121
|
+
|
122
|
+
});
|
123
|
+
|
124
|
+
mqttClient.on('connect', function () {
|
125
|
+
topics.forEach(function (topicsub) {
|
126
|
+
mqttClient.subscribe(topicsub);
|
127
|
+
});
|
128
|
+
let topic;
|
129
|
+
const queue = {
|
130
|
+
sync_api_version: 10,
|
131
|
+
max_deltas_able_to_process: 1000,
|
132
|
+
delta_batch_size: 500,
|
133
|
+
encoding: "JSON",
|
134
|
+
entity_fbid: ctx.i_userID || ctx.userID
|
135
|
+
};
|
136
|
+
|
137
|
+
if (ctx.syncToken) {
|
138
|
+
topic = "/messenger_sync_get_diffs";
|
139
|
+
queue.last_seq_id = ctx.lastSeqId;
|
140
|
+
queue.sync_token = ctx.syncToken;
|
141
|
+
} else {
|
142
|
+
topic = "/messenger_sync_create_queue";
|
143
|
+
queue.initial_titan_sequence_id = ctx.lastSeqId;
|
144
|
+
queue.device_params = null;
|
145
|
+
}
|
146
|
+
|
147
|
+
mqttClient.publish(topic, JSON.stringify(queue), {
|
148
|
+
qos: 1,
|
149
|
+
retain: false
|
150
|
+
});
|
151
|
+
mqttClient.publish("/foreground_state", JSON.stringify({
|
152
|
+
foreground: chatOn
|
153
|
+
}), {
|
154
|
+
qos: 1
|
155
|
+
});
|
156
|
+
mqttClient.publish("/set_client_settings", JSON.stringify({
|
157
|
+
make_user_available_when_in_foreground: true
|
158
|
+
}), {
|
159
|
+
qos: 1
|
160
|
+
});
|
161
|
+
const rTimeout = setTimeout(function () {
|
162
|
+
mqttClient.end();
|
163
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
164
|
+
}, 5000);
|
165
|
+
ctx.tmsWait = function () {
|
166
|
+
clearTimeout(rTimeout);
|
167
|
+
ctx.globalOptions.emitReady ? globalCallback({
|
168
|
+
type: "ready",
|
169
|
+
error: null
|
170
|
+
}) : "";
|
171
|
+
delete ctx.tmsWait;
|
172
|
+
};
|
173
|
+
});
|
174
|
+
|
175
|
+
mqttClient.on('message', function (topic, message, _packet) {
|
176
|
+
let jsonMessage = Buffer.isBuffer(message) ? Buffer.from(message).toString() : message;
|
177
|
+
try {
|
178
|
+
jsonMessage = JSON.parse(jsonMessage);
|
179
|
+
} catch (e) {
|
180
|
+
jsonMessage = {};
|
181
|
+
}
|
182
|
+
if (jsonMessage.type === "jewel_requests_add") {
|
183
|
+
globalCallback(null, {
|
184
|
+
type: "friend_request_received",
|
185
|
+
actorFbId: jsonMessage.from.toString(),
|
186
|
+
timestamp: Date.now().toString()
|
187
|
+
});
|
188
|
+
} else if (jsonMessage.type === "jewel_requests_remove_old") {
|
189
|
+
globalCallback(null, {
|
190
|
+
type: "friend_request_cancel",
|
191
|
+
actorFbId: jsonMessage.from.toString(),
|
192
|
+
timestamp: Date.now().toString()
|
193
|
+
});
|
194
|
+
} else if (topic === "/t_ms") {
|
195
|
+
if (ctx.tmsWait && typeof ctx.tmsWait == "function") {
|
196
|
+
ctx.tmsWait();
|
197
|
+
}
|
198
|
+
|
199
|
+
if (jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
|
200
|
+
ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
|
201
|
+
ctx.syncToken = jsonMessage.syncToken;
|
202
|
+
}
|
203
|
+
|
204
|
+
if (jsonMessage.lastIssuedSeqId) {
|
205
|
+
ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
|
206
|
+
}
|
207
|
+
for (const i in jsonMessage.deltas) {
|
208
|
+
const delta = jsonMessage.deltas[i];
|
209
|
+
parseDelta(defaultFuncs, api, ctx, globalCallback, {
|
210
|
+
"delta": delta
|
211
|
+
});
|
212
|
+
}
|
213
|
+
} else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
|
214
|
+
const typ = {
|
215
|
+
type: "typ",
|
216
|
+
isTyping: !!jsonMessage.state,
|
217
|
+
from: jsonMessage.sender_fbid.toString(),
|
218
|
+
threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
|
219
|
+
};
|
220
|
+
(function () {
|
221
|
+
globalCallback(null, typ);
|
222
|
+
})();
|
223
|
+
} else if (topic === "/orca_presence") {
|
224
|
+
if (!ctx.globalOptions.updatePresence) {
|
225
|
+
for (const i in jsonMessage.list) {
|
226
|
+
const data = jsonMessage.list[i];
|
227
|
+
const userID = data["u"];
|
228
|
+
const presence = {
|
229
|
+
type: "presence",
|
230
|
+
userID: userID.toString(),
|
231
|
+
timestamp: data["l"] * 1000,
|
232
|
+
statuses: data["p"]
|
233
|
+
};
|
234
|
+
(function () {
|
235
|
+
globalCallback(null, presence);
|
236
|
+
})();
|
237
|
+
}
|
238
|
+
}
|
239
|
+
}
|
240
|
+
});
|
241
|
+
}
|
242
|
+
|
243
|
+
function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
|
244
|
+
if (v.delta.class == "NewMessage") {
|
245
|
+
if (ctx.globalOptions.pageID && ctx.globalOptions.pageID != v.queue) return;
|
246
|
+
(function resolveAttachmentUrl(i) {
|
247
|
+
if (i == (v.delta.attachments || []).length) {
|
248
|
+
let fmtMsg;
|
249
|
+
try {
|
250
|
+
fmtMsg = utils.formatDeltaMessage(v);
|
251
|
+
} catch (err) {
|
252
|
+
return globalCallback({
|
253
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/ntkhang03/fb-chat-api/issues.",
|
254
|
+
detail: err,
|
255
|
+
res: v,
|
256
|
+
type: "parse_error"
|
257
|
+
});
|
258
|
+
}
|
259
|
+
if (fmtMsg) {
|
260
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
261
|
+
markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
|
262
|
+
}
|
263
|
+
}
|
264
|
+
return !ctx.globalOptions.selfListen &&
|
265
|
+
(fmtMsg.senderID === ctx.i_userID || fmtMsg.senderID === ctx.userID) ?
|
266
|
+
undefined :
|
267
|
+
(function () {
|
268
|
+
globalCallback(null, fmtMsg);
|
269
|
+
})();
|
270
|
+
} else {
|
271
|
+
if (v.delta.attachments[i].mercury.attach_type == "photo") {
|
272
|
+
api.resolvePhotoUrl(
|
273
|
+
v.delta.attachments[i].fbid,
|
274
|
+
(err, url) => {
|
275
|
+
if (!err)
|
276
|
+
v.delta.attachments[
|
277
|
+
i
|
278
|
+
].mercury.metadata.url = url;
|
279
|
+
return resolveAttachmentUrl(i + 1);
|
280
|
+
}
|
281
|
+
);
|
282
|
+
} else {
|
283
|
+
return resolveAttachmentUrl(i + 1);
|
284
|
+
}
|
285
|
+
}
|
286
|
+
})(0);
|
287
|
+
}
|
288
|
+
|
289
|
+
if (v.delta.class == "ClientPayload") {
|
290
|
+
const clientPayload = utils.decodeClientPayload(
|
291
|
+
v.delta.payload
|
292
|
+
);
|
293
|
+
|
294
|
+
if (clientPayload && clientPayload.deltas) {
|
295
|
+
for (const i in clientPayload.deltas) {
|
296
|
+
const delta = clientPayload.deltas[i];
|
297
|
+
if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
|
298
|
+
(function () {
|
299
|
+
globalCallback(null, {
|
300
|
+
type: "message_reaction",
|
301
|
+
threadID: (delta.deltaMessageReaction.threadKey
|
302
|
+
.threadFbId ?
|
303
|
+
delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey
|
304
|
+
.otherUserFbId).toString(),
|
305
|
+
messageID: delta.deltaMessageReaction.messageId,
|
306
|
+
reaction: delta.deltaMessageReaction.reaction,
|
307
|
+
senderID: delta.deltaMessageReaction.senderId == 0 ? delta.deltaMessageReaction.userId.toString() : delta.deltaMessageReaction.senderId.toString(),
|
308
|
+
userID: (delta.deltaMessageReaction.userId || delta.deltaMessageReaction.senderId).toString()
|
309
|
+
});
|
310
|
+
})();
|
311
|
+
} else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
|
312
|
+
(function () {
|
313
|
+
globalCallback(null, {
|
314
|
+
type: "message_unsend",
|
315
|
+
threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ?
|
316
|
+
delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey
|
317
|
+
.otherUserFbId).toString(),
|
318
|
+
messageID: delta.deltaRecallMessageData.messageID,
|
319
|
+
senderID: delta.deltaRecallMessageData.senderID.toString(),
|
320
|
+
deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
|
321
|
+
timestamp: delta.deltaRecallMessageData.timestamp
|
322
|
+
});
|
323
|
+
})();
|
324
|
+
} else if (delta.deltaRemoveMessage && !!ctx.globalOptions.listenEvents) {
|
325
|
+
(function () {
|
326
|
+
globalCallback(null, {
|
327
|
+
type: "message_self_delete",
|
328
|
+
threadID: (delta.deltaRemoveMessage.threadKey.threadFbId ?
|
329
|
+
delta.deltaRemoveMessage.threadKey.threadFbId : delta.deltaRemoveMessage.threadKey
|
330
|
+
.otherUserFbId).toString(),
|
331
|
+
messageID: delta.deltaRemoveMessage.messageIds.length == 1 ? delta.deltaRemoveMessage.messageIds[0] : delta.deltaRemoveMessage.messageIds,
|
332
|
+
senderID: api.getCurrentUserID(),
|
333
|
+
deletionTimestamp: delta.deltaRemoveMessage.deletionTimestamp,
|
334
|
+
timestamp: delta.deltaRemoveMessage.timestamp
|
335
|
+
});
|
336
|
+
})();
|
337
|
+
} else if (delta.deltaMessageReply) {
|
338
|
+
let mdata =
|
339
|
+
delta.deltaMessageReply.message === undefined ? [] :
|
340
|
+
delta.deltaMessageReply.message.data === undefined ? [] :
|
341
|
+
delta.deltaMessageReply.message.data.prng === undefined ? [] :
|
342
|
+
JSON.parse(delta.deltaMessageReply.message.data.prng);
|
343
|
+
let m_id = mdata.map(u => u.i);
|
344
|
+
let m_offset = mdata.map(u => u.o);
|
345
|
+
let m_length = mdata.map(u => u.l);
|
346
|
+
|
347
|
+
const mentions = {};
|
348
|
+
|
349
|
+
for (let i = 0; i < m_id.length; i++) {
|
350
|
+
mentions[m_id[i]] = (delta.deltaMessageReply.message.body || "").substring(
|
351
|
+
m_offset[i],
|
352
|
+
m_offset[i] + m_length[i]
|
353
|
+
);
|
354
|
+
}
|
355
|
+
//Mention block - 1#
|
356
|
+
const callbackToReturn = {
|
357
|
+
type: "message_reply",
|
358
|
+
threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ?
|
359
|
+
delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey
|
360
|
+
.otherUserFbId).toString(),
|
361
|
+
messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
|
362
|
+
senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
|
363
|
+
attachments: (delta.deltaMessageReply.message.attachments || []).map(function (att) {
|
364
|
+
const mercury = JSON.parse(att.mercuryJSON);
|
365
|
+
Object.assign(att, mercury);
|
366
|
+
return att;
|
367
|
+
}).map(att => {
|
368
|
+
let x;
|
369
|
+
try {
|
370
|
+
x = utils._formatAttachment(att);
|
371
|
+
} catch (ex) {
|
372
|
+
x = att;
|
373
|
+
x.error = ex;
|
374
|
+
x.type = "unknown";
|
375
|
+
}
|
376
|
+
return x;
|
377
|
+
}),
|
378
|
+
body: delta.deltaMessageReply.message.body || "",
|
379
|
+
isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
|
380
|
+
mentions: mentions,
|
381
|
+
timestamp: delta.deltaMessageReply.message.messageMetadata.timestamp,
|
382
|
+
participantIDs: (delta.deltaMessageReply.message.messageMetadata.cid.canonicalParticipantFbids || delta.deltaMessageReply.message.participants || []).map(e => e.toString())
|
383
|
+
};
|
384
|
+
|
385
|
+
if (delta.deltaMessageReply.repliedToMessage) {
|
386
|
+
//Mention block - #2
|
387
|
+
mdata =
|
388
|
+
delta.deltaMessageReply.repliedToMessage === undefined ? [] :
|
389
|
+
delta.deltaMessageReply.repliedToMessage.data === undefined ? [] :
|
390
|
+
delta.deltaMessageReply.repliedToMessage.data.prng === undefined ? [] :
|
391
|
+
JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
|
392
|
+
m_id = mdata.map(u => u.i);
|
393
|
+
m_offset = mdata.map(u => u.o);
|
394
|
+
m_length = mdata.map(u => u.l);
|
395
|
+
|
396
|
+
const rmentions = {};
|
397
|
+
|
398
|
+
for (let i = 0; i < m_id.length; i++) {
|
399
|
+
rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || "").substring(
|
400
|
+
m_offset[i],
|
401
|
+
m_offset[i] + m_length[i]
|
402
|
+
);
|
403
|
+
}
|
404
|
+
//Mention block - 2#
|
405
|
+
callbackToReturn.messageReply = {
|
406
|
+
threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ?
|
407
|
+
delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey
|
408
|
+
.otherUserFbId).toString(),
|
409
|
+
messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
|
410
|
+
senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
|
411
|
+
attachments: delta.deltaMessageReply.repliedToMessage.attachments.map(function (att) {
|
412
|
+
const mercury = JSON.parse(att.mercuryJSON);
|
413
|
+
Object.assign(att, mercury);
|
414
|
+
return att;
|
415
|
+
}).map(att => {
|
416
|
+
let x;
|
417
|
+
try {
|
418
|
+
x = utils._formatAttachment(att);
|
419
|
+
} catch (ex) {
|
420
|
+
x = att;
|
421
|
+
x.error = ex;
|
422
|
+
x.type = "unknown";
|
423
|
+
}
|
424
|
+
return x;
|
425
|
+
}),
|
426
|
+
body: delta.deltaMessageReply.repliedToMessage.body || "",
|
427
|
+
isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
|
428
|
+
mentions: rmentions,
|
429
|
+
timestamp: delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp
|
430
|
+
};
|
431
|
+
} else if (delta.deltaMessageReply.replyToMessageId) {
|
432
|
+
return defaultFuncs
|
433
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, {
|
434
|
+
"av": ctx.globalOptions.pageID,
|
435
|
+
"queries": JSON.stringify({
|
436
|
+
"o0": {
|
437
|
+
//Using the same doc_id as forcedFetch
|
438
|
+
"doc_id": "2848441488556444",
|
439
|
+
"query_params": {
|
440
|
+
"thread_and_message_id": {
|
441
|
+
"thread_id": callbackToReturn.threadID,
|
442
|
+
"message_id": delta.deltaMessageReply.replyToMessageId.id
|
443
|
+
}
|
444
|
+
}
|
445
|
+
}
|
446
|
+
})
|
447
|
+
})
|
448
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
449
|
+
.then((resData) => {
|
450
|
+
if (resData[resData.length - 1].error_results > 0) {
|
451
|
+
throw resData[0].o0.errors;
|
452
|
+
}
|
453
|
+
|
454
|
+
if (resData[resData.length - 1].successful_results === 0) {
|
455
|
+
throw {
|
456
|
+
error: "forcedFetch: there was no successful_results",
|
457
|
+
res: resData
|
458
|
+
};
|
459
|
+
}
|
460
|
+
|
461
|
+
const fetchData = resData[0].o0.data.message;
|
462
|
+
|
463
|
+
const mobj = {};
|
464
|
+
for (const n in fetchData.message.ranges) {
|
465
|
+
mobj[fetchData.message.ranges[n].entity.id] = (fetchData.message.text || "").substr(fetchData.message.ranges[n].offset, fetchData.message.ranges[n].length);
|
466
|
+
}
|
467
|
+
|
468
|
+
callbackToReturn.messageReply = {
|
469
|
+
threadID: callbackToReturn.threadID,
|
470
|
+
messageID: fetchData.message_id,
|
471
|
+
senderID: fetchData.message_sender.id.toString(),
|
472
|
+
attachments: fetchData.message.blob_attachment.map(att => {
|
473
|
+
let x;
|
474
|
+
try {
|
475
|
+
x = utils._formatAttachment({
|
476
|
+
blob_attachment: att
|
477
|
+
});
|
478
|
+
} catch (ex) {
|
479
|
+
x = att;
|
480
|
+
x.error = ex;
|
481
|
+
x.type = "unknown";
|
482
|
+
}
|
483
|
+
return x;
|
484
|
+
}),
|
485
|
+
body: fetchData.message.text || "",
|
486
|
+
isGroup: callbackToReturn.isGroup,
|
487
|
+
mentions: mobj,
|
488
|
+
timestamp: parseInt(fetchData.timestamp_precise)
|
489
|
+
};
|
490
|
+
})
|
491
|
+
.catch((err) => {
|
492
|
+
log.error("forcedFetch", err);
|
493
|
+
})
|
494
|
+
.finally(function () {
|
495
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
496
|
+
markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
|
497
|
+
} !ctx.globalOptions.selfListen &&
|
498
|
+
(callbackToReturn.senderID === ctx.i_userID || callbackToReturn.senderID === ctx.userID) ?
|
499
|
+
undefined :
|
500
|
+
(function () {
|
501
|
+
globalCallback(null, callbackToReturn);
|
502
|
+
})();
|
503
|
+
});
|
504
|
+
} else {
|
505
|
+
callbackToReturn.delta = delta;
|
506
|
+
}
|
507
|
+
|
508
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
509
|
+
markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
|
510
|
+
}
|
511
|
+
|
512
|
+
return !ctx.globalOptions.selfListen &&
|
513
|
+
(callbackToReturn.senderID === ctx.i_userID || callbackToReturn.senderID === ctx.userID) ?
|
514
|
+
undefined :
|
515
|
+
(function () {
|
516
|
+
globalCallback(null, callbackToReturn);
|
517
|
+
})();
|
518
|
+
}
|
519
|
+
}
|
520
|
+
return;
|
521
|
+
}
|
522
|
+
}
|
523
|
+
|
524
|
+
if (v.delta.class !== "NewMessage" &&
|
525
|
+
!ctx.globalOptions.listenEvents
|
526
|
+
)
|
527
|
+
return;
|
528
|
+
|
529
|
+
switch (v.delta.class) {
|
530
|
+
case "ReadReceipt":
|
531
|
+
var fmtMsg;
|
532
|
+
try {
|
533
|
+
fmtMsg = utils.formatDeltaReadReceipt(v.delta);
|
534
|
+
} catch (err) {
|
535
|
+
return globalCallback({
|
536
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/ntkhang03/fb-chat-api/issues.",
|
537
|
+
detail: err,
|
538
|
+
res: v.delta,
|
539
|
+
type: "parse_error"
|
540
|
+
});
|
541
|
+
}
|
542
|
+
return (function () {
|
543
|
+
globalCallback(null, fmtMsg);
|
544
|
+
})();
|
545
|
+
case "AdminTextMessage":
|
546
|
+
switch (v.delta.type) {
|
547
|
+
case "change_thread_theme":
|
548
|
+
case "change_thread_nickname":
|
549
|
+
case "change_thread_icon":
|
550
|
+
case "change_thread_admins":
|
551
|
+
case "group_poll":
|
552
|
+
case "joinable_group_link_mode_change":
|
553
|
+
case "magic_words":
|
554
|
+
case "change_thread_approval_mode":
|
555
|
+
case "messenger_call_log":
|
556
|
+
case "participant_joined_group_call":
|
557
|
+
var fmtMsg;
|
558
|
+
try {
|
559
|
+
fmtMsg = utils.formatDeltaEvent(v.delta);
|
560
|
+
} catch (err) {
|
561
|
+
return globalCallback({
|
562
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/ntkhang03/fb-chat-api/issues.",
|
563
|
+
detail: err,
|
564
|
+
res: v.delta,
|
565
|
+
type: "parse_error"
|
566
|
+
});
|
567
|
+
}
|
568
|
+
return (function () {
|
569
|
+
globalCallback(null, fmtMsg);
|
570
|
+
})();
|
571
|
+
default:
|
572
|
+
return;
|
573
|
+
}
|
574
|
+
//For group images
|
575
|
+
case "ForcedFetch":
|
576
|
+
if (!v.delta.threadKey) return;
|
577
|
+
var mid = v.delta.messageId;
|
578
|
+
var tid = v.delta.threadKey.threadFbId;
|
579
|
+
if (mid && tid) {
|
580
|
+
const form = {
|
581
|
+
"av": ctx.globalOptions.pageID,
|
582
|
+
"queries": JSON.stringify({
|
583
|
+
"o0": {
|
584
|
+
//This doc_id is valid as of March 25, 2020
|
585
|
+
"doc_id": "2848441488556444",
|
586
|
+
"query_params": {
|
587
|
+
"thread_and_message_id": {
|
588
|
+
"thread_id": tid.toString(),
|
589
|
+
"message_id": mid
|
590
|
+
}
|
591
|
+
}
|
592
|
+
}
|
593
|
+
})
|
594
|
+
};
|
595
|
+
|
596
|
+
defaultFuncs
|
597
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
598
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
599
|
+
.then((resData) => {
|
600
|
+
if (resData[resData.length - 1].error_results > 0) {
|
601
|
+
throw resData[0].o0.errors;
|
602
|
+
}
|
603
|
+
|
604
|
+
if (resData[resData.length - 1].successful_results === 0) {
|
605
|
+
throw {
|
606
|
+
error: "forcedFetch: there was no successful_results",
|
607
|
+
res: resData
|
608
|
+
};
|
609
|
+
}
|
610
|
+
|
611
|
+
const fetchData = resData[0].o0.data.message;
|
612
|
+
|
613
|
+
if (utils.getType(fetchData) == "Object") {
|
614
|
+
log.info("forcedFetch", fetchData);
|
615
|
+
switch (fetchData.__typename) {
|
616
|
+
case "ThreadImageMessage":
|
617
|
+
(!ctx.globalOptions.selfListenEvent && (fetchData.message_sender.id.toString() === ctx.i_userID || fetchData.message_sender.id.toString() === ctx.userID)) || !ctx.loggedIn ?
|
618
|
+
undefined :
|
619
|
+
(function () {
|
620
|
+
globalCallback(null, {
|
621
|
+
type: "event",
|
622
|
+
threadID: utils.formatID(tid.toString()),
|
623
|
+
messageID: fetchData.message_id,
|
624
|
+
logMessageType: "log:thread-image",
|
625
|
+
logMessageData: {
|
626
|
+
attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
|
627
|
+
width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
|
628
|
+
height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
|
629
|
+
url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
|
630
|
+
},
|
631
|
+
logMessageBody: fetchData.snippet,
|
632
|
+
timestamp: fetchData.timestamp_precise,
|
633
|
+
author: fetchData.message_sender.id
|
634
|
+
});
|
635
|
+
})();
|
636
|
+
break;
|
637
|
+
case "UserMessage":
|
638
|
+
log.info("ff-Return", {
|
639
|
+
type: "message",
|
640
|
+
senderID: utils.formatID(fetchData.message_sender.id),
|
641
|
+
body: fetchData.message.text || "",
|
642
|
+
threadID: utils.formatID(tid.toString()),
|
643
|
+
messageID: fetchData.message_id,
|
644
|
+
attachments: [{
|
645
|
+
type: "share",
|
646
|
+
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
647
|
+
url: fetchData.extensible_attachment.story_attachment.url,
|
648
|
+
|
649
|
+
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
650
|
+
description: fetchData.extensible_attachment.story_attachment.description.text,
|
651
|
+
source: fetchData.extensible_attachment.story_attachment.source,
|
652
|
+
|
653
|
+
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
654
|
+
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
655
|
+
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
656
|
+
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
657
|
+
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
658
|
+
|
659
|
+
subattachments: fetchData.extensible_attachment.subattachments,
|
660
|
+
properties: fetchData.extensible_attachment.story_attachment.properties
|
661
|
+
}],
|
662
|
+
mentions: {},
|
663
|
+
timestamp: parseInt(fetchData.timestamp_precise),
|
664
|
+
participantIDs: (fetchData.participants || (fetchData.messageMetadata ? fetchData.messageMetadata.cid ? fetchData.messageMetadata.cid.canonicalParticipantFbids : fetchData.messageMetadata.participantIds : []) || []),
|
665
|
+
isGroup: (fetchData.message_sender.id != tid.toString())
|
666
|
+
});
|
667
|
+
globalCallback(null, {
|
668
|
+
type: "message",
|
669
|
+
senderID: utils.formatID(fetchData.message_sender.id),
|
670
|
+
body: fetchData.message.text || "",
|
671
|
+
threadID: utils.formatID(tid.toString()),
|
672
|
+
messageID: fetchData.message_id,
|
673
|
+
attachments: [{
|
674
|
+
type: "share",
|
675
|
+
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
676
|
+
url: fetchData.extensible_attachment.story_attachment.url,
|
677
|
+
|
678
|
+
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
679
|
+
description: fetchData.extensible_attachment.story_attachment.description.text,
|
680
|
+
source: fetchData.extensible_attachment.story_attachment.source,
|
681
|
+
|
682
|
+
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
683
|
+
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
684
|
+
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
685
|
+
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
686
|
+
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
687
|
+
|
688
|
+
subattachments: fetchData.extensible_attachment.subattachments,
|
689
|
+
properties: fetchData.extensible_attachment.story_attachment.properties
|
690
|
+
}],
|
691
|
+
mentions: {},
|
692
|
+
timestamp: parseInt(fetchData.timestamp_precise),
|
693
|
+
participantIDs: (fetchData.participants || (fetchData.messageMetadata ? fetchData.messageMetadata.cid ? fetchData.messageMetadata.cid.canonicalParticipantFbids : fetchData.messageMetadata.participantIds : []) || []),
|
694
|
+
isGroup: (fetchData.message_sender.id != tid.toString())
|
695
|
+
});
|
696
|
+
}
|
697
|
+
} else {
|
698
|
+
log.error("forcedFetch", fetchData);
|
699
|
+
}
|
700
|
+
})
|
701
|
+
.catch((err) => {
|
702
|
+
log.error("forcedFetch", err);
|
703
|
+
});
|
704
|
+
}
|
705
|
+
break;
|
706
|
+
case "ThreadName":
|
707
|
+
case "ParticipantsAddedToGroupThread":
|
708
|
+
case "ParticipantLeftGroupThread":
|
709
|
+
case "ApprovalQueue":
|
710
|
+
var formattedEvent;
|
711
|
+
try {
|
712
|
+
formattedEvent = utils.formatDeltaEvent(v.delta);
|
713
|
+
} catch (err) {
|
714
|
+
return globalCallback({
|
715
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/ntkhang03/fb-chat-api/issues.",
|
716
|
+
detail: err,
|
717
|
+
res: v.delta,
|
718
|
+
type: "parse_error"
|
719
|
+
});
|
720
|
+
}
|
721
|
+
return (!ctx.globalOptions.selfListenEvent && (formattedEvent.author.toString() === ctx.i_userID || formattedEvent.author.toString() === ctx.userID)) || !ctx.loggedIn ?
|
722
|
+
undefined :
|
723
|
+
(function () {
|
724
|
+
globalCallback(null, formattedEvent);
|
725
|
+
})();
|
726
|
+
}
|
727
|
+
}
|
728
|
+
|
729
|
+
function markDelivery(ctx, api, threadID, messageID) {
|
730
|
+
if (threadID && messageID) {
|
731
|
+
api.markAsDelivered(threadID, messageID, (err) => {
|
732
|
+
if (err) {
|
733
|
+
log.error("markAsDelivered", err);
|
734
|
+
} else {
|
735
|
+
if (ctx.globalOptions.autoMarkRead) {
|
736
|
+
api.markAsRead(threadID, (err) => {
|
737
|
+
if (err) {
|
738
|
+
log.error("markAsDelivered", err);
|
739
|
+
}
|
740
|
+
});
|
741
|
+
}
|
742
|
+
}
|
743
|
+
});
|
744
|
+
}
|
745
|
+
}
|
746
|
+
|
747
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
748
|
+
let globalCallback = identity;
|
749
|
+
getSeqId = function getSeqId() {
|
750
|
+
ctx.t_mqttCalled = false;
|
751
|
+
const forms = {
|
752
|
+
av: ctx.userID || i_userID,
|
753
|
+
fb_dtsg: ctx.fb_dtsg,
|
754
|
+
queries: JSON.stringify({
|
755
|
+
o0: {
|
756
|
+
doc_id: '3336396659757871',
|
757
|
+
query_params: {
|
758
|
+
limit: 1,
|
759
|
+
before: null,
|
760
|
+
tags: ['INBOX'],
|
761
|
+
includeDeliveryReceipts: false,
|
762
|
+
includeSeqID: true,
|
763
|
+
},
|
764
|
+
},
|
765
|
+
}),
|
766
|
+
__user: ctx.userID,
|
767
|
+
__a: '1',
|
768
|
+
__req: '8',
|
769
|
+
__hs: '19577.HYP:comet_pkg.2.1..2.1',
|
770
|
+
dpr: '1',
|
771
|
+
fb_api_caller_class: 'RelayModern',
|
772
|
+
fb_api_req_friendly_name: 'MessengerGraphQLThreadlistFetcher',
|
773
|
+
};
|
774
|
+
const headers = {
|
775
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
776
|
+
Referer: 'https://www.facebook.com/',
|
777
|
+
Origin: 'https://www.facebook.com',
|
778
|
+
'sec-fetch-site': 'same-origin',
|
779
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
|
780
|
+
Cookie: ctx.jar.getCookieString('https://www.facebook.com'),
|
781
|
+
accept: '*/*',
|
782
|
+
'accept-encoding': 'gzip, deflate, br',
|
783
|
+
};
|
784
|
+
defaultFuncs
|
785
|
+
.post('https://www.facebook.com/api/graphqlbatch/', ctx.jar, forms, { headers })
|
786
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
787
|
+
.then((resData) => {
|
788
|
+
if (utils.getType(resData) !== "Array") {
|
789
|
+
throw {
|
790
|
+
error: "Not logged in",
|
791
|
+
res: resData,
|
792
|
+
};
|
793
|
+
}
|
794
|
+
if (resData && resData[resData.length - 1].error_results > 0) {
|
795
|
+
throw resData[0].o0.errors;
|
796
|
+
}
|
797
|
+
if (resData[resData.length - 1].successful_results === 0) {
|
798
|
+
throw {
|
799
|
+
error: "getSeqId: there was no successful_results",
|
800
|
+
res: resData,
|
801
|
+
};
|
802
|
+
}
|
803
|
+
if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
|
804
|
+
ctx.lastSeqId =
|
805
|
+
resData[0].o0.data.viewer.message_threads.sync_sequence_id;
|
806
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
807
|
+
} else {
|
808
|
+
throw {
|
809
|
+
error: "getSeqId: no sync_sequence_id found.",
|
810
|
+
res: resData,
|
811
|
+
};
|
812
|
+
}
|
813
|
+
})
|
814
|
+
.catch((err) => {
|
815
|
+
log.error("getSeqId", err);
|
816
|
+
if (utils.getType(err) === "Object" && err.error === "Not logged in") {
|
817
|
+
ctx.loggedIn = false;
|
818
|
+
}
|
819
|
+
return globalCallback(err);
|
820
|
+
});
|
821
|
+
};
|
822
|
+
return function (callback) {
|
823
|
+
class MessageEmitter extends EventEmitter {
|
824
|
+
stopListening(callback) {
|
825
|
+
callback = callback || (() => { });
|
826
|
+
globalCallback = identity;
|
827
|
+
if (ctx.mqttClient) {
|
828
|
+
ctx.mqttClient.unsubscribe("/webrtc");
|
829
|
+
ctx.mqttClient.unsubscribe("/rtc_multi");
|
830
|
+
ctx.mqttClient.unsubscribe("/onevc");
|
831
|
+
ctx.mqttClient.publish("/browser_close", "{}");
|
832
|
+
ctx.mqttClient.end(false, () => {
|
833
|
+
ctx.mqttClient = undefined;
|
834
|
+
callback();
|
835
|
+
});
|
836
|
+
} else {
|
837
|
+
callback();
|
838
|
+
}
|
839
|
+
}
|
840
|
+
async stopListeningAsync() {
|
841
|
+
return new Promise((resolve) => {
|
842
|
+
this.stopListening(resolve);
|
843
|
+
});
|
844
|
+
}
|
845
|
+
}
|
846
|
+
const msgEmitter = new MessageEmitter();
|
847
|
+
globalCallback = callback || function (error, message) {
|
848
|
+
if (error) {
|
849
|
+
return msgEmitter.emit("error", error);
|
850
|
+
}
|
851
|
+
msgEmitter.emit("message", message);
|
852
|
+
};
|
853
|
+
if (!ctx.firstListen) ctx.lastSeqId = null;
|
854
|
+
ctx.syncToken = undefined;
|
855
|
+
ctx.t_mqttCalled = false;
|
856
|
+
form = {
|
857
|
+
av: ctx.globalOptions.pageID,
|
858
|
+
queries: JSON.stringify({
|
859
|
+
o0: {
|
860
|
+
doc_id: "3336396659757871",
|
861
|
+
query_params: {
|
862
|
+
limit: 1,
|
863
|
+
before: null,
|
864
|
+
tags: ["INBOX"],
|
865
|
+
includeDeliveryReceipts: false,
|
866
|
+
includeSeqID: true,
|
867
|
+
},
|
868
|
+
},
|
869
|
+
}),
|
870
|
+
};
|
871
|
+
if (!ctx.firstListen || !ctx.lastSeqId) getSeqId();
|
872
|
+
else listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
873
|
+
ctx.firstListen = false;
|
874
|
+
api.stopListening = msgEmitter.stopListening.bind(msgEmitter);
|
875
|
+
api.stopListeningAsync = msgEmitter.stopListeningAsync.bind(msgEmitter);
|
876
|
+
return msgEmitter;
|
877
|
+
};
|
710
878
|
};
|