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