alicezetion 1.6.5 → 1.6.7
Sign up to get free protection for your applications and to get access to all the features.
- package/.cache/replit/__replit_disk_meta.json +1 -1
- package/leiamnash/listen.js +553 -0
- package/leiamnash/listenMqtt.js +560 -789
- package/package.json +1 -1
package/leiamnash/listenMqtt.js
CHANGED
@@ -1,789 +1,560 @@
|
|
1
|
-
/* eslint-disable no-redeclare */
|
2
|
-
"use strict";
|
3
|
-
var utils = require("../utils");
|
4
|
-
var log = require("npmlog");
|
5
|
-
var mqtt = require('mqtt');
|
6
|
-
var websocket = require('websocket-stream');
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
var
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
"/
|
17
|
-
"/
|
18
|
-
"/
|
19
|
-
"/
|
20
|
-
|
21
|
-
"/
|
22
|
-
"/
|
23
|
-
"/
|
24
|
-
"/
|
25
|
-
"/notify_disconnect",
|
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
|
-
var fetchData = resData[0].o0.data.message;
|
562
|
-
|
563
|
-
if (utils.getType(fetchData) == "Object") {
|
564
|
-
log.info("forcedFetch", fetchData);
|
565
|
-
switch (fetchData.__typename) {
|
566
|
-
case "ThreadImageMessage":
|
567
|
-
(!ctx.globalOptions.selfListen &&
|
568
|
-
fetchData.message_sender.id.toString() === ctx.userID) ||
|
569
|
-
!ctx.loggedIn ?
|
570
|
-
undefined :
|
571
|
-
(function () {
|
572
|
-
globalCallback(null, {
|
573
|
-
type: "change_thread_image",
|
574
|
-
threadID: utils.formatID(tid.toString()),
|
575
|
-
snippet: fetchData.snippet,
|
576
|
-
timestamp: fetchData.timestamp_precise,
|
577
|
-
author: fetchData.message_sender.id,
|
578
|
-
image: {
|
579
|
-
attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
|
580
|
-
width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
|
581
|
-
height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
|
582
|
-
url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
|
583
|
-
}
|
584
|
-
});
|
585
|
-
})();
|
586
|
-
break;
|
587
|
-
case "UserMessage":
|
588
|
-
log.info("ff-Return", {
|
589
|
-
type: "message",
|
590
|
-
senderID: utils.formatID(fetchData.message_sender.id),
|
591
|
-
body: fetchData.message.text || "",
|
592
|
-
threadID: utils.formatID(tid.toString()),
|
593
|
-
messageID: fetchData.message_id,
|
594
|
-
attachments: [{
|
595
|
-
type: "share",
|
596
|
-
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
597
|
-
url: fetchData.extensible_attachment.story_attachment.url,
|
598
|
-
|
599
|
-
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
600
|
-
description: fetchData.extensible_attachment.story_attachment.description.text,
|
601
|
-
source: fetchData.extensible_attachment.story_attachment.source,
|
602
|
-
|
603
|
-
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
604
|
-
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
605
|
-
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
606
|
-
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
607
|
-
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
608
|
-
|
609
|
-
subattachments: fetchData.extensible_attachment.subattachments,
|
610
|
-
properties: fetchData.extensible_attachment.story_attachment.properties,
|
611
|
-
}],
|
612
|
-
mentions: {},
|
613
|
-
timestamp: parseInt(fetchData.timestamp_precise),
|
614
|
-
isGroup: (fetchData.message_sender.id != tid.toString())
|
615
|
-
});
|
616
|
-
globalCallback(null, {
|
617
|
-
type: "message",
|
618
|
-
senderID: utils.formatID(fetchData.message_sender.id),
|
619
|
-
body: fetchData.message.text || "",
|
620
|
-
threadID: utils.formatID(tid.toString()),
|
621
|
-
messageID: fetchData.message_id,
|
622
|
-
attachments: [{
|
623
|
-
type: "share",
|
624
|
-
ID: fetchData.extensible_attachment.legacy_attachment_id,
|
625
|
-
url: fetchData.extensible_attachment.story_attachment.url,
|
626
|
-
|
627
|
-
title: fetchData.extensible_attachment.story_attachment.title_with_entities.text,
|
628
|
-
description: fetchData.extensible_attachment.story_attachment.description.text,
|
629
|
-
source: fetchData.extensible_attachment.story_attachment.source,
|
630
|
-
|
631
|
-
image: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).uri,
|
632
|
-
width: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).width,
|
633
|
-
height: ((fetchData.extensible_attachment.story_attachment.media || {}).image || {}).height,
|
634
|
-
playable: (fetchData.extensible_attachment.story_attachment.media || {}).is_playable || false,
|
635
|
-
duration: (fetchData.extensible_attachment.story_attachment.media || {}).playable_duration_in_ms || 0,
|
636
|
-
|
637
|
-
subattachments: fetchData.extensible_attachment.subattachments,
|
638
|
-
properties: fetchData.extensible_attachment.story_attachment.properties,
|
639
|
-
}],
|
640
|
-
mentions: {},
|
641
|
-
timestamp: parseInt(fetchData.timestamp_precise),
|
642
|
-
isGroup: (fetchData.message_sender.id != tid.toString())
|
643
|
-
});
|
644
|
-
}
|
645
|
-
} else {
|
646
|
-
console.error("forcedFetch", fetchData);
|
647
|
-
}
|
648
|
-
})
|
649
|
-
.catch((err) => {
|
650
|
-
console.error("forcedFetch", err);
|
651
|
-
});
|
652
|
-
}
|
653
|
-
break;
|
654
|
-
case "ThreadName":
|
655
|
-
case "ParticipantsAddedToGroupThread":
|
656
|
-
case "ParticipantLeftGroupThread":
|
657
|
-
var formattedEvent;
|
658
|
-
try {
|
659
|
-
formattedEvent = utils.formatDeltaEvent(v.delta);
|
660
|
-
} catch (err) {
|
661
|
-
return globalCallback({
|
662
|
-
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
663
|
-
detail: err,
|
664
|
-
res: v.delta,
|
665
|
-
type: "parse_error"
|
666
|
-
});
|
667
|
-
}
|
668
|
-
return (!ctx.globalOptions.selfListen &&
|
669
|
-
formattedEvent.author.toString() === ctx.userID) ||
|
670
|
-
!ctx.loggedIn ?
|
671
|
-
undefined :
|
672
|
-
(function () { globalCallback(null, formattedEvent); })();
|
673
|
-
}
|
674
|
-
}
|
675
|
-
|
676
|
-
function markDelivery(ctx, api, threadID, messageID) {
|
677
|
-
if (threadID && messageID) {
|
678
|
-
api.markAsDelivered(threadID, messageID, (err) => {
|
679
|
-
if (err) {
|
680
|
-
console.error("markAsDelivered", err);
|
681
|
-
} else {
|
682
|
-
if (ctx.globalOptions.autoMarkRead) {
|
683
|
-
api.markAsRead(threadID, (err) => {
|
684
|
-
if (err) {
|
685
|
-
console.error("markAsDelivered", err);
|
686
|
-
}
|
687
|
-
});
|
688
|
-
}
|
689
|
-
}
|
690
|
-
});
|
691
|
-
}
|
692
|
-
}
|
693
|
-
|
694
|
-
module.exports = function (defaultFuncs, api, ctx) {
|
695
|
-
var globalCallback = identity;
|
696
|
-
getSeqID = function getSeqID() {
|
697
|
-
ctx.t_mqttCalled = false;
|
698
|
-
defaultFuncs
|
699
|
-
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
700
|
-
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
701
|
-
.then((resData) => {
|
702
|
-
if (utils.getType(resData) != "Array") {
|
703
|
-
throw {
|
704
|
-
error: "Not logged in",
|
705
|
-
res: resData
|
706
|
-
};
|
707
|
-
}
|
708
|
-
|
709
|
-
if (resData && resData[resData.length - 1].error_results > 0) {
|
710
|
-
throw resData[0].o0.errors;
|
711
|
-
}
|
712
|
-
|
713
|
-
if (resData[resData.length - 1].successful_results === 0) {
|
714
|
-
throw { error: "getSeqId: there was no successful_results", res: resData };
|
715
|
-
}
|
716
|
-
|
717
|
-
if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
|
718
|
-
ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
|
719
|
-
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
720
|
-
} else {
|
721
|
-
throw { error: "getSeqId: no sync_sequence_id found.", res: resData };
|
722
|
-
}
|
723
|
-
})
|
724
|
-
.catch((err) => {
|
725
|
-
console.error("getSeqId", err);
|
726
|
-
if (utils.getType(err) == "Object" && err.error === "Not logged in") {
|
727
|
-
ctx.loggedIn = false;
|
728
|
-
}
|
729
|
-
return globalCallback(err);
|
730
|
-
});
|
731
|
-
};
|
732
|
-
|
733
|
-
return function (callback) {
|
734
|
-
class MessageEmitter extends EventEmitter {
|
735
|
-
stopListening(callback) {
|
736
|
-
callback = callback || (() => { });
|
737
|
-
globalCallback = identity;
|
738
|
-
if (ctx.mqttClient) {
|
739
|
-
ctx.mqttClient.unsubscribe("/webrtc");
|
740
|
-
ctx.mqttClient.unsubscribe("/rtc_multi");
|
741
|
-
ctx.mqttClient.unsubscribe("/onevc");
|
742
|
-
ctx.mqttClient.publish("/browser_close", "{}");
|
743
|
-
ctx.mqttClient.end(false, function (...data) {
|
744
|
-
callback(data);
|
745
|
-
ctx.mqttClient = undefined;
|
746
|
-
});
|
747
|
-
}
|
748
|
-
}
|
749
|
-
}
|
750
|
-
|
751
|
-
var msgEmitter = new MessageEmitter();
|
752
|
-
globalCallback = (callback || function (error, message) {
|
753
|
-
if (error) {
|
754
|
-
return msgEmitter.emit("error", error);
|
755
|
-
}
|
756
|
-
msgEmitter.emit("message", message);
|
757
|
-
});
|
758
|
-
|
759
|
-
//Reset some stuff
|
760
|
-
if (!ctx.firstListen) ctx.lastSeqId = null;
|
761
|
-
ctx.syncToken = undefined;
|
762
|
-
ctx.t_mqttCalled = false;
|
763
|
-
|
764
|
-
//Same request as getThreadList
|
765
|
-
form = {
|
766
|
-
"av": ctx.globalOptions.pageID,
|
767
|
-
"queries": JSON.stringify({
|
768
|
-
"o0": {
|
769
|
-
"doc_id": "3336396659757871",
|
770
|
-
"query_params": {
|
771
|
-
"limit": 1,
|
772
|
-
"before": null,
|
773
|
-
"tags": ["INBOX"],
|
774
|
-
"includeDeliveryReceipts": false,
|
775
|
-
"includeSeqID": true
|
776
|
-
}
|
777
|
-
}
|
778
|
-
})
|
779
|
-
};
|
780
|
-
|
781
|
-
if (!ctx.firstListen || !ctx.lastSeqId) {
|
782
|
-
getSeqID();
|
783
|
-
} else {
|
784
|
-
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
785
|
-
}
|
786
|
-
ctx.firstListen = false;
|
787
|
-
return msgEmitter;
|
788
|
-
};
|
789
|
-
};
|
1
|
+
/* eslint-disable no-redeclare */
|
2
|
+
"use strict";
|
3
|
+
var utils = require("../utils");
|
4
|
+
var log = require("npmlog");
|
5
|
+
var mqtt = require('mqtt');
|
6
|
+
var websocket = require('websocket-stream');
|
7
|
+
|
8
|
+
var identity = function () {};
|
9
|
+
|
10
|
+
//Don't really know what this does but I think it's for the active state
|
11
|
+
//TODO: Move to ctx when implemented
|
12
|
+
var chatOn = true;
|
13
|
+
var foreground = false;
|
14
|
+
|
15
|
+
var topics = [
|
16
|
+
"/t_ms",
|
17
|
+
"/thread_typing",
|
18
|
+
"/orca_typing_notifications",
|
19
|
+
"/orca_presence",
|
20
|
+
"/legacy_web",
|
21
|
+
"/br_sr",
|
22
|
+
"/sr_res",
|
23
|
+
"/webrtc",
|
24
|
+
"/onevc",
|
25
|
+
"/notify_disconnect",
|
26
|
+
"/inbox",
|
27
|
+
"/mercury",
|
28
|
+
"/messaging_events",
|
29
|
+
"/orca_message_notifications",
|
30
|
+
"/pp",
|
31
|
+
"/webrtc_response",
|
32
|
+
];
|
33
|
+
|
34
|
+
function listenMqtt(defaultFuncs, api, ctx, globalCallback) {
|
35
|
+
var sessionID = Math.floor(Math.random() * 9007199254740991) + 1;
|
36
|
+
var username = {
|
37
|
+
u: ctx.userID,
|
38
|
+
s: sessionID,
|
39
|
+
chat_on: chatOn,
|
40
|
+
fg: foreground,
|
41
|
+
d: utils.getGUID(),
|
42
|
+
ct: "websocket",
|
43
|
+
//App id from facebook
|
44
|
+
aid: "219994525426954",
|
45
|
+
mqtt_sid: "",
|
46
|
+
cp: 3,
|
47
|
+
ecp: 10,
|
48
|
+
st: topics,
|
49
|
+
pm: [],
|
50
|
+
dc: "",
|
51
|
+
no_auto_fg: true,
|
52
|
+
gas: null
|
53
|
+
};
|
54
|
+
var cookies = ctx.jar.getCookies("https://www.facebook.com").join("; ");
|
55
|
+
|
56
|
+
//Region could be changed for better ping. (Region atn: Southeast Asia, region ash: West US, prob) (Don't really know if we need it).
|
57
|
+
//// var host = 'wss://edge-chat.facebook.com/chat?region=atn&sid=' + sessionID;
|
58
|
+
var host = 'wss://edge-chat.facebook.com/chat?sid=' + sessionID;
|
59
|
+
|
60
|
+
var options = {
|
61
|
+
clientId: "mqttwsclient",
|
62
|
+
protocolId: 'MQIsdp',
|
63
|
+
protocolVersion: 3,
|
64
|
+
username: JSON.stringify(username),
|
65
|
+
clean: true,
|
66
|
+
wsOptions: {
|
67
|
+
headers: {
|
68
|
+
'Cookie': cookies,
|
69
|
+
'Origin': 'https://www.facebook.com',
|
70
|
+
'User-Agent': ctx.globalOptions.userAgent,
|
71
|
+
'Referer': 'https://www.facebook.com',
|
72
|
+
'Host': 'edge-chat.facebook.com'
|
73
|
+
},
|
74
|
+
origin: 'https://www.facebook.com',
|
75
|
+
protocolVersion: 13
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
|
80
|
+
|
81
|
+
var mqttClient = ctx.mqttClient;
|
82
|
+
|
83
|
+
mqttClient.on('error', function(err) {
|
84
|
+
log.error(err);
|
85
|
+
mqttClient.end();
|
86
|
+
globalCallback("Connection refused: Server unavailable", null);
|
87
|
+
});
|
88
|
+
|
89
|
+
mqttClient.on('connect', function() {
|
90
|
+
var topic;
|
91
|
+
var queue = {
|
92
|
+
sync_api_version: 10,
|
93
|
+
max_deltas_able_to_process: 1000,
|
94
|
+
delta_batch_size: 500,
|
95
|
+
encoding: "JSON",
|
96
|
+
entity_fbid: ctx.userID,
|
97
|
+
};
|
98
|
+
|
99
|
+
if(ctx.globalOptions.pageID) {
|
100
|
+
queue.entity_fbid = ctx.globalOptions.pageID;
|
101
|
+
}
|
102
|
+
|
103
|
+
if(ctx.syncToken) {
|
104
|
+
topic = "/messenger_sync_get_diffs";
|
105
|
+
queue.last_seq_id = ctx.lastSeqId;
|
106
|
+
queue.sync_token = ctx.syncToken;
|
107
|
+
} else {
|
108
|
+
topic = "/messenger_sync_create_queue";
|
109
|
+
queue.initial_titan_sequence_id = ctx.lastSeqId;
|
110
|
+
queue.device_params = null;
|
111
|
+
}
|
112
|
+
|
113
|
+
mqttClient.publish(topic, JSON.stringify(queue), {qos: 1, retain: false});
|
114
|
+
});
|
115
|
+
|
116
|
+
mqttClient.on('message', function(topic, message, packet) {
|
117
|
+
var jsonMessage = JSON.parse(message);
|
118
|
+
if(topic === "/t_ms") {
|
119
|
+
if(jsonMessage.firstDeltaSeqId && jsonMessage.syncToken) {
|
120
|
+
ctx.lastSeqId = jsonMessage.firstDeltaSeqId;
|
121
|
+
ctx.syncToken = jsonMessage.syncToken;
|
122
|
+
}
|
123
|
+
|
124
|
+
if(jsonMessage.lastIssuedSeqId) {
|
125
|
+
ctx.lastSeqId = parseInt(jsonMessage.lastIssuedSeqId);
|
126
|
+
}
|
127
|
+
|
128
|
+
if(jsonMessage.queueEntityId && ctx.globalOptions.pageID &&
|
129
|
+
ctx.globalOptions.pageID != jsonMessage.queueEntityId) {
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
|
133
|
+
//If it contains more than 1 delta
|
134
|
+
for (var i in jsonMessage.deltas) {
|
135
|
+
var delta = jsonMessage.deltas[i];
|
136
|
+
parseDelta(defaultFuncs, api, ctx, globalCallback, { "delta": delta });
|
137
|
+
}
|
138
|
+
} else if (topic === "/thread_typing" || topic === "/orca_typing_notifications") {
|
139
|
+
var typ = {
|
140
|
+
type: "typ",
|
141
|
+
isTyping: !!jsonMessage.state,
|
142
|
+
from: jsonMessage.sender_fbid.toString(),
|
143
|
+
threadID: utils.formatID((jsonMessage.thread || jsonMessage.sender_fbid).toString())
|
144
|
+
};
|
145
|
+
(function () { globalCallback(null, typ); })();
|
146
|
+
} else if (topic === "/orca_presence") {
|
147
|
+
if (!ctx.globalOptions.updatePresence) {
|
148
|
+
for (var i in jsonMessage.list) {
|
149
|
+
var data = jsonMessage.list[i];
|
150
|
+
var userID = data["u"];
|
151
|
+
|
152
|
+
var presence = {
|
153
|
+
type: "presence",
|
154
|
+
userID: userID.toString(),
|
155
|
+
//Convert to ms
|
156
|
+
timestamp: data["l"] * 1000,
|
157
|
+
statuses: data["p"]
|
158
|
+
};
|
159
|
+
(function () { globalCallback(null, presence); })();
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
});
|
165
|
+
|
166
|
+
mqttClient.on('close', function() {
|
167
|
+
// client.end();
|
168
|
+
});
|
169
|
+
}
|
170
|
+
|
171
|
+
function parseDelta(defaultFuncs, api, ctx, globalCallback, v) {
|
172
|
+
if(v.delta.class == "NewMessage") {
|
173
|
+
(function resolveAttachmentUrl(i) {
|
174
|
+
// sometimes, with sticker message in group, delta does not contain 'attachments' property.
|
175
|
+
if (v.delta.attachments && (i == v.delta.attachments.length)) {
|
176
|
+
var fmtMsg;
|
177
|
+
try {
|
178
|
+
fmtMsg = utils.formatDeltaMessage(v);
|
179
|
+
} catch (err) {
|
180
|
+
return globalCallback({
|
181
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
182
|
+
detail: err,
|
183
|
+
res: v,
|
184
|
+
type: "parse_error"
|
185
|
+
});
|
186
|
+
}
|
187
|
+
if (fmtMsg) {
|
188
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
189
|
+
markDelivery(ctx, api, fmtMsg.threadID, fmtMsg.messageID);
|
190
|
+
}
|
191
|
+
}
|
192
|
+
return !ctx.globalOptions.selfListen &&
|
193
|
+
fmtMsg.senderID === ctx.userID ?
|
194
|
+
undefined :
|
195
|
+
(function () { globalCallback(null, fmtMsg); })();
|
196
|
+
} else {
|
197
|
+
if (
|
198
|
+
v.delta.attachments && (v.delta.attachments[i].mercury.attach_type == "photo")
|
199
|
+
) {
|
200
|
+
api.resolvePhotoUrl(
|
201
|
+
v.delta.attachments[i].fbid,
|
202
|
+
(err, url) => {
|
203
|
+
if (!err)
|
204
|
+
v.delta.attachments[
|
205
|
+
i
|
206
|
+
].mercury.metadata.url = url;
|
207
|
+
return resolveAttachmentUrl(i + 1);
|
208
|
+
}
|
209
|
+
);
|
210
|
+
} else {
|
211
|
+
return resolveAttachmentUrl(i + 1);
|
212
|
+
}
|
213
|
+
}
|
214
|
+
})(0);
|
215
|
+
}
|
216
|
+
|
217
|
+
if (v.delta.class == "ClientPayload") {
|
218
|
+
var clientPayload = utils.decodeClientPayload(
|
219
|
+
v.delta.payload
|
220
|
+
);
|
221
|
+
if (clientPayload && clientPayload.deltas) {
|
222
|
+
for (var i in clientPayload.deltas) {
|
223
|
+
var delta = clientPayload.deltas[i];
|
224
|
+
if (delta.deltaMessageReaction && !!ctx.globalOptions.listenEvents) {
|
225
|
+
(function () { globalCallback(null, {
|
226
|
+
type: "message_reaction",
|
227
|
+
threadID: (delta.deltaMessageReaction.threadKey
|
228
|
+
.threadFbId ?
|
229
|
+
delta.deltaMessageReaction.threadKey.threadFbId : delta.deltaMessageReaction.threadKey
|
230
|
+
.otherUserFbId).toString(),
|
231
|
+
messageID: delta.deltaMessageReaction.messageId,
|
232
|
+
reaction: delta.deltaMessageReaction.reaction,
|
233
|
+
senderID: delta.deltaMessageReaction.senderId.toString(),
|
234
|
+
userID: delta.deltaMessageReaction.userId.toString()
|
235
|
+
}); })();
|
236
|
+
} else if (delta.deltaRecallMessageData && !!ctx.globalOptions.listenEvents) {
|
237
|
+
(function () { globalCallback(null, {
|
238
|
+
type: "message_unsend",
|
239
|
+
threadID: (delta.deltaRecallMessageData.threadKey.threadFbId ?
|
240
|
+
delta.deltaRecallMessageData.threadKey.threadFbId : delta.deltaRecallMessageData.threadKey
|
241
|
+
.otherUserFbId).toString(),
|
242
|
+
messageID: delta.deltaRecallMessageData.messageID,
|
243
|
+
senderID: delta.deltaRecallMessageData.senderID.toString(),
|
244
|
+
deletionTimestamp: delta.deltaRecallMessageData.deletionTimestamp,
|
245
|
+
timestamp: delta.deltaRecallMessageData.timestamp
|
246
|
+
}); })();
|
247
|
+
} else if (delta.deltaMessageReply) {
|
248
|
+
//Mention block - #1
|
249
|
+
var mdata =
|
250
|
+
delta.deltaMessageReply.message === undefined ? [] :
|
251
|
+
delta.deltaMessageReply.message.data === undefined ? [] :
|
252
|
+
delta.deltaMessageReply.message.data.prng === undefined ? [] :
|
253
|
+
JSON.parse(delta.deltaMessageReply.message.data.prng);
|
254
|
+
var m_id = mdata.map(u => u.i);
|
255
|
+
var m_offset = mdata.map(u => u.o);
|
256
|
+
var m_length = mdata.map(u => u.l);
|
257
|
+
|
258
|
+
var mentions = {};
|
259
|
+
|
260
|
+
for (var i = 0; i < m_id.length; i++) {
|
261
|
+
mentions[m_id[i]] = (delta.deltaMessageReply.message.body || "").substring(
|
262
|
+
m_offset[i],
|
263
|
+
m_offset[i] + m_length[i]
|
264
|
+
);
|
265
|
+
}
|
266
|
+
//Mention block - 1#
|
267
|
+
var callbackToReturn = {
|
268
|
+
type: "message_reply",
|
269
|
+
threadID: (delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId ?
|
270
|
+
delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.message.messageMetadata.threadKey
|
271
|
+
.otherUserFbId).toString(),
|
272
|
+
messageID: delta.deltaMessageReply.message.messageMetadata.messageId,
|
273
|
+
senderID: delta.deltaMessageReply.message.messageMetadata.actorFbId.toString(),
|
274
|
+
attachments: delta.deltaMessageReply.message.attachments.map(function (att) {
|
275
|
+
var mercury = JSON.parse(att.mercuryJSON);
|
276
|
+
Object.assign(att, mercury);
|
277
|
+
return att;
|
278
|
+
}).map(att => {
|
279
|
+
var x;
|
280
|
+
try {
|
281
|
+
x = utils._formatAttachment(att);
|
282
|
+
} catch (ex) {
|
283
|
+
x = att;
|
284
|
+
x.error = ex;
|
285
|
+
x.type = "unknown";
|
286
|
+
}
|
287
|
+
return x;
|
288
|
+
}),
|
289
|
+
body: delta.deltaMessageReply.message.body || "",
|
290
|
+
isGroup: !!delta.deltaMessageReply.message.messageMetadata.threadKey.threadFbId,
|
291
|
+
mentions: mentions,
|
292
|
+
timestamp: delta.deltaMessageReply.message.messageMetadata.timestamp,
|
293
|
+
};
|
294
|
+
|
295
|
+
if (delta.deltaMessageReply.repliedToMessage) {
|
296
|
+
//Mention block - #2
|
297
|
+
mdata =
|
298
|
+
delta.deltaMessageReply.repliedToMessage === undefined ? [] :
|
299
|
+
delta.deltaMessageReply.repliedToMessage.data === undefined ? [] :
|
300
|
+
delta.deltaMessageReply.repliedToMessage.data.prng === undefined ? [] :
|
301
|
+
JSON.parse(delta.deltaMessageReply.repliedToMessage.data.prng);
|
302
|
+
m_id = mdata.map(u => u.i);
|
303
|
+
m_offset = mdata.map(u => u.o);
|
304
|
+
m_length = mdata.map(u => u.l);
|
305
|
+
|
306
|
+
var rmentions = {};
|
307
|
+
|
308
|
+
for (var i = 0; i < m_id.length; i++) {
|
309
|
+
rmentions[m_id[i]] = (delta.deltaMessageReply.repliedToMessage.body || "").substring(
|
310
|
+
m_offset[i],
|
311
|
+
m_offset[i] + m_length[i]
|
312
|
+
);
|
313
|
+
}
|
314
|
+
//Mention block - 2#
|
315
|
+
callbackToReturn.messageReply = {
|
316
|
+
threadID: (delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId ?
|
317
|
+
delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId : delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey
|
318
|
+
.otherUserFbId).toString(),
|
319
|
+
messageID: delta.deltaMessageReply.repliedToMessage.messageMetadata.messageId,
|
320
|
+
senderID: delta.deltaMessageReply.repliedToMessage.messageMetadata.actorFbId.toString(),
|
321
|
+
attachments: delta.deltaMessageReply.repliedToMessage.attachments.map(function (att) {
|
322
|
+
var mercury = JSON.parse(att.mercuryJSON);
|
323
|
+
Object.assign(att, mercury);
|
324
|
+
return att;
|
325
|
+
}).map(att => {
|
326
|
+
var x;
|
327
|
+
try {
|
328
|
+
x = utils._formatAttachment(att);
|
329
|
+
} catch (ex) {
|
330
|
+
x = att;
|
331
|
+
x.error = ex;
|
332
|
+
x.type = "unknown";
|
333
|
+
}
|
334
|
+
return x;
|
335
|
+
}),
|
336
|
+
body: delta.deltaMessageReply.repliedToMessage.body || "",
|
337
|
+
isGroup: !!delta.deltaMessageReply.repliedToMessage.messageMetadata.threadKey.threadFbId,
|
338
|
+
mentions: rmentions,
|
339
|
+
timestamp: delta.deltaMessageReply.repliedToMessage.messageMetadata.timestamp,
|
340
|
+
};
|
341
|
+
}
|
342
|
+
|
343
|
+
if (ctx.globalOptions.autoMarkDelivery) {
|
344
|
+
markDelivery(ctx, api, callbackToReturn.threadID, callbackToReturn.messageID);
|
345
|
+
}
|
346
|
+
|
347
|
+
return !ctx.globalOptions.selfListen &&
|
348
|
+
callbackToReturn.senderID === ctx.userID ?
|
349
|
+
undefined :
|
350
|
+
(function () { globalCallback(null, callbackToReturn); })();
|
351
|
+
}
|
352
|
+
}
|
353
|
+
return;
|
354
|
+
}
|
355
|
+
}
|
356
|
+
|
357
|
+
if (v.delta.class !== "NewMessage" &&
|
358
|
+
!ctx.globalOptions.listenEvents
|
359
|
+
)
|
360
|
+
return;
|
361
|
+
|
362
|
+
switch (v.delta.class) {
|
363
|
+
case "ReadReceipt":
|
364
|
+
var fmtMsg;
|
365
|
+
try {
|
366
|
+
fmtMsg = utils.formatDeltaReadReceipt(v.delta);
|
367
|
+
} catch (err) {
|
368
|
+
return globalCallback({
|
369
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
370
|
+
detail: err,
|
371
|
+
res: v.delta,
|
372
|
+
type: "parse_error"
|
373
|
+
});
|
374
|
+
}
|
375
|
+
return (function () { globalCallback(null, fmtMsg); })();
|
376
|
+
case "AdminTextMessage":
|
377
|
+
switch (v.delta.type) {
|
378
|
+
case "change_thread_theme":
|
379
|
+
case "change_thread_nickname":
|
380
|
+
case "change_thread_icon":
|
381
|
+
break;
|
382
|
+
case "group_poll":
|
383
|
+
var fmtMsg;
|
384
|
+
try {
|
385
|
+
fmtMsg = utils.formatDeltaEvent(v.delta);
|
386
|
+
} catch (err) {
|
387
|
+
return globalCallback({
|
388
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
389
|
+
detail: err,
|
390
|
+
res: v.delta,
|
391
|
+
type: "parse_error"
|
392
|
+
});
|
393
|
+
}
|
394
|
+
return (function () { globalCallback(null, fmtMsg); })();
|
395
|
+
default:
|
396
|
+
return;
|
397
|
+
}
|
398
|
+
break;
|
399
|
+
//For group images
|
400
|
+
case "ForcedFetch":
|
401
|
+
if (!v.delta.threadKey) return;
|
402
|
+
var mid = v.delta.messageId;
|
403
|
+
var tid = v.delta.threadKey.threadFbId;
|
404
|
+
if (mid && tid) {
|
405
|
+
const form = {
|
406
|
+
"av": ctx.globalOptions.pageID,
|
407
|
+
"queries": JSON.stringify({
|
408
|
+
"o0": {
|
409
|
+
//This doc_id is valid as of ? (prob January 18, 2020)
|
410
|
+
"doc_id": "1768656253222505",
|
411
|
+
"query_params": {
|
412
|
+
"thread_and_message_id": {
|
413
|
+
"thread_id": tid.toString(),
|
414
|
+
"message_id": mid.toString(),
|
415
|
+
}
|
416
|
+
}
|
417
|
+
}
|
418
|
+
})
|
419
|
+
};
|
420
|
+
|
421
|
+
defaultFuncs
|
422
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
423
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
424
|
+
.then((resData) => {
|
425
|
+
if (resData[resData.length - 1].error_results > 0) {
|
426
|
+
throw resData[0].o0.errors;
|
427
|
+
}
|
428
|
+
|
429
|
+
if (resData[resData.length - 1].successful_results === 0) {
|
430
|
+
throw { error: "forcedFetch: there was no successful_results", res: resData };
|
431
|
+
}
|
432
|
+
|
433
|
+
var fetchData = resData[0].o0.data.message;
|
434
|
+
if (fetchData && fetchData.__typename === "ThreadImageMessage") {
|
435
|
+
(!ctx.globalOptions.selfListen &&
|
436
|
+
fetchData.message_sender.id.toString() === ctx.userID) ||
|
437
|
+
!ctx.loggedIn ?
|
438
|
+
undefined :
|
439
|
+
(function () { globalCallback(null, {
|
440
|
+
type: "change_thread_image",
|
441
|
+
threadID: utils.formatID(tid.toString()),
|
442
|
+
snippet: fetchData.snippet,
|
443
|
+
timestamp: fetchData.timestamp_precise,
|
444
|
+
author: fetchData.message_sender.id,
|
445
|
+
image: {
|
446
|
+
attachmentID: fetchData.image_with_metadata && fetchData.image_with_metadata.legacy_attachment_id,
|
447
|
+
width: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.x,
|
448
|
+
height: fetchData.image_with_metadata && fetchData.image_with_metadata.original_dimensions.y,
|
449
|
+
url: fetchData.image_with_metadata && fetchData.image_with_metadata.preview.uri
|
450
|
+
}
|
451
|
+
}); })();
|
452
|
+
}
|
453
|
+
})
|
454
|
+
.catch((err) => {
|
455
|
+
log.error("forcedFetch", err);
|
456
|
+
});
|
457
|
+
}
|
458
|
+
break;
|
459
|
+
case "ThreadName":
|
460
|
+
case "ParticipantsAddedToGroupThread":
|
461
|
+
case "ParticipantLeftGroupThread":
|
462
|
+
var formattedEvent;
|
463
|
+
try {
|
464
|
+
formattedEvent = utils.formatDeltaEvent(v.delta);
|
465
|
+
} catch (err) {
|
466
|
+
return globalCallback({
|
467
|
+
error: "Problem parsing message object. Please open an issue at https://github.com/Schmavery/facebook-chat-api/issues.",
|
468
|
+
detail: err,
|
469
|
+
res: v.delta,
|
470
|
+
type: "parse_error"
|
471
|
+
});
|
472
|
+
}
|
473
|
+
return (!ctx.globalOptions.selfListen &&
|
474
|
+
formattedEvent.author.toString() === ctx.userID) ||
|
475
|
+
!ctx.loggedIn ?
|
476
|
+
undefined :
|
477
|
+
(function () { globalCallback(null, formattedEvent); })();
|
478
|
+
}
|
479
|
+
}
|
480
|
+
|
481
|
+
function markDelivery(ctx, api, threadID, messageID) {
|
482
|
+
if (threadID && messageID) {
|
483
|
+
api.markAsDelivered(threadID, messageID, (err) => {
|
484
|
+
if (err) {
|
485
|
+
log.error(err);
|
486
|
+
} else {
|
487
|
+
if (ctx.globalOptions.autoMarkRead) {
|
488
|
+
api.markAsRead(threadID, (err) => {
|
489
|
+
if (err) {
|
490
|
+
log.error(err);
|
491
|
+
}
|
492
|
+
});
|
493
|
+
}
|
494
|
+
}
|
495
|
+
});
|
496
|
+
}
|
497
|
+
}
|
498
|
+
|
499
|
+
module.exports = function (defaultFuncs, api, ctx) {
|
500
|
+
var globalCallback = identity;
|
501
|
+
return function (callback) {
|
502
|
+
globalCallback = callback;
|
503
|
+
|
504
|
+
//Reset some stuff
|
505
|
+
ctx.lastSeqId = 0;
|
506
|
+
ctx.syncToken = undefined;
|
507
|
+
|
508
|
+
//Same request as getThreadList
|
509
|
+
const form = {
|
510
|
+
"av": ctx.globalOptions.pageID,
|
511
|
+
"queries": JSON.stringify({
|
512
|
+
"o0": {
|
513
|
+
"doc_id": "1349387578499440",
|
514
|
+
"query_params": {
|
515
|
+
"limit": 1,
|
516
|
+
"before": null,
|
517
|
+
"tags": ["INBOX"],
|
518
|
+
"includeDeliveryReceipts": false,
|
519
|
+
"includeSeqID": true
|
520
|
+
}
|
521
|
+
}
|
522
|
+
})
|
523
|
+
};
|
524
|
+
|
525
|
+
defaultFuncs
|
526
|
+
.post("https://www.facebook.com/api/graphqlbatch/", ctx.jar, form)
|
527
|
+
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
|
528
|
+
.then((resData) => {
|
529
|
+
if (resData && resData.length > 0 && resData[resData.length - 1].error_results > 0) {
|
530
|
+
throw resData[0].o0.errors;
|
531
|
+
}
|
532
|
+
|
533
|
+
if (resData[resData.length - 1].successful_results === 0) {
|
534
|
+
throw { error: "getSeqId: there was no successful_results", res: resData };
|
535
|
+
}
|
536
|
+
|
537
|
+
if (resData[0].o0.data.viewer.message_threads.sync_sequence_id) {
|
538
|
+
ctx.lastSeqId = resData[0].o0.data.viewer.message_threads.sync_sequence_id;
|
539
|
+
listenMqtt(defaultFuncs, api, ctx, globalCallback);
|
540
|
+
}
|
541
|
+
|
542
|
+
})
|
543
|
+
.catch((err) => {
|
544
|
+
log.error("getSeqId", err);
|
545
|
+
return callback(err);
|
546
|
+
});
|
547
|
+
|
548
|
+
var stopListening = function () {
|
549
|
+
globalCallback = identity;
|
550
|
+
|
551
|
+
if(ctx.mqttClient)
|
552
|
+
{
|
553
|
+
ctx.mqttClient.end();
|
554
|
+
ctx.mqttClient = undefined;
|
555
|
+
}
|
556
|
+
};
|
557
|
+
|
558
|
+
return stopListening;
|
559
|
+
};
|
560
|
+
}
|