-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpuppeteerWhatsApp.js
1362 lines (1235 loc) · 49.9 KB
/
puppeteerWhatsApp.js
1
2
3
4
5
6
7
8
9
10
11
12
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'
const EventEmitter = require('events')
const puppeteer = require('puppeteer')
const moduleRaid = require('./raidWhatsApp')
const { WindowStore, WindowUtils } = require('./storeWhatsApp')
const httpProxy = require('http-proxy')
const { getFreePorts, isFreePort } = require('node-port-check')
const yaqrcode = require('yaqrcode')
const fetch = require('node-fetch')
const qrcode_terminal = require('qrcode-terminal')
const moment = require('moment');
const path = require('path')
const fs = require('fs');
const jsonArgs = require('@toolia/json-args').default;
const argv = jsonArgs(process.argv.slice(2));
const userDataDir = path.dirname(__filename) + '/data';
var port = 8333
var headless = true
var debug = false
var addmessage = true
var addmessageme = false
var auth_user = 'user'
var auth_password = 'pass'
var domain = ''
if(typeof argv.port === 'number')port = argv.port
if(typeof argv.headless === 'boolean')headless = argv.headless
if(typeof argv.debug === 'boolean')debug = argv.debug
if(typeof argv.message === 'boolean')addmessage = argv.message
if(typeof argv.messageMe === 'boolean')addmessageme = argv.messageMe
if(typeof argv.user !== 'undefined')auth_user = argv.user
if(typeof argv.pass !== 'undefined')auth_password = argv.pass
if(typeof argv.domain !== 'undefined')domain = argv.domain
// DEFINE CONST WHATSAPP WEB
const APP_HEADLESS = headless
const APP_HOST = '0.0.0.0'
const APP_PORT = port
const APP_SERVER = 'http://localhost:' + APP_PORT
if(domain == '')domain = APP_SERVER
const APP_DOMAIN = domain
const APP_API_DIR = '/'
const APP_API_PATH = 'api'
const APP_URI = 'https://web.whatsapp.com'
const APP_TIMEOUT_SELECTOR = 150000
const APP_KEEP_PHONE_CONNECTED_SELECTOR = '[data-tab]'
const APP_QR_VALUE_SELECTOR = '[data-ref]'
const APP_LANGUAGE = 'en'
const APP_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'
const APP_LINUX = '/usr/bin/chromium-browser'
const APP_DEBUG = debug
const APP_MESSAGE = addmessage
const APP_MESSAGE_ME = addmessageme
const APP_AUTH_USER = auth_user
const APP_AUTH_PASSWORD = auth_password
console.log({port: port, headless: headless, debug: debug, message: addmessage, user: auth_user, password: auth_password, domain: domain})
// PUPPETEER EMITTER
class PuppeteerWhatsApp extends EventEmitter {
constructor () {
super()
this.browser = null
this.page = null
}
// CHECKED
async start(token, bot, webhook, socket){
try {
const time_start = Date.now()
if (typeof token === 'undefined' || token == '')token = 'new'
if (typeof bot === 'string' && (bot).trim() != '' && this.isUrl(bot)) var bot = (bot).trim()
else var bot = null
if (typeof webhook === 'string' && (webhook).trim() != '' && this.isUrl(webhook)) var webhook = (webhook).trim()
else var webhook = null
const db_token = this.getDatabaseToken()
const puppeteer_args = [
// '--full-memory-crash-report'
'--disable-dev-shm-usage',
'--enable-sync', '--enable-background-networking', '--no-sandbox', '--disable-setuid-sandbox',
'--disable-gpu', '--renderer', '--no-service-autorun', '--no-experiments',
'--no-default-browser-check', '--disable-webgl', '--disable-threaded-animation',
'--disable-threaded-scrolling', '--disable-in-process-stack-traces', '--disable-histogram-customizer',
'--disable-gl-extensions', '--disable-extensions', '--disable-composited-antialiasing',
'--disable-canvas-aa', '--disable-3d-apis', '--disable-accelerated-2d-canvas',
'--disable-accelerated-jpeg-decoding', '--disable-accelerated-mjpeg-decode', '--disable-app-list-dismiss-on-blur',
'--disable-accelerated-video-decode', '--mute-audio',
'--log-level=3',
'--disable-infobars',
'--disable-web-security',
'--disable-site-isolation-trials',
'--ignore-gpu-blacklist',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list',
'--disable-default-apps',
'--enable-features=NetworkService',
'--no-first-run',
'--no-zygote',
'--unlimited-storage'
];
if(APP_HEADLESS)puppeteer_args.push('--force-gpu-mem-available-mb');
else{
if(APP_DEBUG)puppeteer_args.push('--auto-open-devtools-for-tabs');
}
//console.log(puppeteer_args);
const browser_args = {
headless: APP_HEADLESS,
ignoreHTTPSErrors: true,
defaultViewport: null,
args: puppeteer_args,
userDataDir: userDataDir
}
//console.log(userDataDir);
//LINUX CHROMIUM PATH
if(fs.existsSync(APP_LINUX))browser_args.executablePath = APP_LINUX
// NEW PUPPETEER
const browser = await puppeteer.launch(browser_args)
this.browser = browser
// CREATE PUPPETEER INCOGNITO
const context = await browser.createIncognitoBrowserContext()
const page = await context.newPage()
// CREATE PUPPETEER NORMAL
// const page = await browser.newPage();
// CLOSE BLANK PAGE
const pages_created = await browser.pages()
pages_created[0].close()
this.page = pages_created[1]
console.log('INIT TOKEN ' + token)
// USER AGENT, EXTRA HEADER, REQUEST INTERCEPTION
await page.setUserAgent(APP_USER_AGENT)
await page.setExtraHTTPHeaders({ 'Accept-Language': APP_LANGUAGE })
await page.setRequestInterception(true)
// PUPPETEER CONSOLE
await page.on('console', msg => {
for (const arg of msg.args()) {
arg.jsonValue().then(v => console.log(v)).catch(error => console.log(msg))
}
})
// PUPPETEER ERROR
await page.on('error', error => {
console.log('function error')
var str_error = error.toString()
if (typeof str_error === 'string') {
const error_search = str_error.search('Page crashed');
const type_error = str_error.substring(0, 30).trim()
console.log('ERROR TYPE: ' + type_error)
if (error_search > 0) {
page.reload()
}
}
})
await page.on('load', () => {
console.log('ONLOAD')
page.evaluate(res => JSON.stringify(window.sessionStorage)).then(sessionStorage => {
const session = JSON.parse(sessionStorage)
if (typeof session.TK !== 'undefined' && session.TK != '' && session.TK != null) {
const token_name = session.TK
var data_token = db_token.get('token').find({ name: token_name }).value()
if (typeof data_token !== 'undefined' && typeof data_token.localstorage !== 'undefined' && data_token.localstorage != null) {
console.log('AUTOSTART TOKEN')
var send = {
method: 'post',
body: JSON.stringify({ bot: data_token.bot, webhook: data_token.webhook, socket: data_token.socket }),
headers: { 'Content-Type': 'application/json' }
}
const uri_fetch = APP_SERVER + '/' + APP_API_PATH + '/' + token_name + '/start'
fetch(uri_fetch, send)
page.close()
}
} else console.log('TOKEN INITAL')
})
})
// PUPPETEER PAGE ERROR
await page.on('pageerror', e => {
this.getLog('pageerror', e)
})
// BLOCK RESOURCES TO LOAD FAST
await page.on('request', request => {
if ([/* 'stylesheet', */'image', 'font', 'media'].indexOf(request.resourceType()) !== -1)request.abort()
else request.continue()
})
// TOKEN SESSION
var data_token_db = db_token.get('token').find({ name: token }).value()
if (typeof data_token_db !== 'undefined' && typeof data_token_db.localstorage !== 'undefined' && data_token_db.localstorage != null) {
console.log('READ SAVED TOKEN')
const data_token = data_token_db.localstorage
// INJECT TOKEN SESSION IN WHATSAPP WEB
if (data_token != '') {
console.log('VALIDATING TOKEN')
const session = JSON.parse(data_token)
await page.evaluateOnNewDocument(session => {
localStorage.clear()
localStorage.setItem('WABrowserId', session.WABrowserId)
localStorage.setItem('WASecretBundle', session.WASecretBundle)
localStorage.setItem('WAToken1', session.WAToken1)
localStorage.setItem('WAToken2', session.WAToken2)
}, session)
}
} else console.log('CREATE NEW TOKEN')
// GOTO URL
try {
console.log('EVALUATING')
await page.goto(APP_URI, { waitUntil: 'networkidle2', timeout: 10000 })
} catch (e) {
if (APP_DEBUG) {
console.log('NETWORKIDLE')
/* console.log(e) */
}
}
// VALID INJECT TOKEN SESSION
console.log('WAITING TOKEN')
try {
await page.waitForSelector(APP_QR_VALUE_SELECTOR, { timeout: 2300 })
const qr_code = await page.evaluate('document.querySelector("' + APP_QR_VALUE_SELECTOR + '").getAttribute("data-ref")')
if (typeof qr_code === 'undefined' || qr_code === null) {
console.log('NO TOKEN SELECTOR EXISTS')
this.emit('API', { action: 'error', value: 'No token whatsapp selector', data: null, status: false })
this.page.close()
} else {
var qr_base64 = yaqrcode(qr_code, { size: 300 })
this.emit('API', { action: 'token', value: 'Scan token', data: qr_base64, status: true })
qrcode_terminal.generate(qr_code, { small: true })
}
} catch (e) {
if (APP_DEBUG) {
console.log('NO SCAN SELECTOR')
// console.log(e);
}
}
// EVALUATE INJECTED TOKEN SESSION
const is_token = await page.waitForSelector(APP_KEEP_PHONE_CONNECTED_SELECTOR, { timeout: APP_TIMEOUT_SELECTOR }).then(res => {
console.log('VALID TOKEN')
return true
}).catch(e => {
console.log('INVALID TOKEN')
this.emit('API', { action: 'error', value: 'Invalid token', data: null, status: false })
this.browser.close()
return false
})
if (is_token === true) {
console.log('INJECTING')
// SAVE TOKEN LOCALSTORAGE TO JSON
await page.evaluate(res => JSON.stringify(window.localStorage)).then(localStorage => {
console.log('TOKEN SAVED')
db_token.get('token').remove({ name: token }).write()
db_token.get('token').push({ name: token, endpoint: null, localstorage: localStorage, bot: bot, webhook: webhook, socket: socket }).write()
})
// ADD MODULERAID - INJECT
await page.evaluate(WindowStore, moduleRaid.toString())
// CHECK INJECTION
const store_def = await page.waitForFunction('window.Store.Msg !== undefined')
.then(e => {
console.log('INJECTED')
return true
}).catch(e => {
console.log('NOT INJECTED')
this.emit('API', { action: 'error', value: 'Imposible start whatsapp', data: null, status: false })
return
})
// ENDPOINT
const bw_endpoint = await browser.wsEndpoint()
const ws_endpoint = await this.setWebSocket(bw_endpoint, token)
console.log('ENDPOINT ' + ws_endpoint)
db_token.get('token').find({ name: token }).assign({ endpoint: ws_endpoint }).write()
await page.evaluate(WindowUtils)
await page.evaluate((bw_endpoint, ws_endpoint, token) => {
window.App.BW = bw_endpoint
window.App.WS = ws_endpoint
window.App.TK = token
sessionStorage.setItem('BW', bw_endpoint)
sessionStorage.setItem('WS', ws_endpoint)
sessionStorage.setItem('TK', token)
return window.Store.Conn.wid
}, bw_endpoint, ws_endpoint, token).then(me => {
var time = (Date.now() - time_start) / 1000
console.log('WHATSAPP LOADED IN ' + time + ' SEC')
//OPEN SOCKET CLIENT
if(socket != ''){
console.log('SOCKET ON ' + token + ' AT ' + socket)
var socket_client = require('socket.io-client')(socket)
socket_client.on(token, (data) => {
var to_data = data.data;
var to_action = to_data.action
if(APP_DEBUG)console.log(to_data)
var to_url = 'http://localhost:' + APP_PORT + APP_API_DIR + APP_API_PATH + '/'+ token + '/' + to_action
//SEND
var to_send = {
method: 'post',
body: JSON.stringify(to_data),
headers: { 'Content-Type': 'application/json' }
}
fetch(to_url, to_send)
})
}
})
if(APP_MESSAGE){
// ON NEW MESSAGE TO ME
await page.exposeFunction('onAddMessageToMe', (message, typeMessage) => {
new Promise((resolve, reject) => {
this.sendToBot(message, this)
this.sendToWebhookTo(message, this)
this.sendToSocket(message, this, typeMessage)
return true;
}).then(res => res).catch(e => {
if(APP_DEBUG)return e
})
})
}
if(APP_MESSAGE_ME){
// ON NEW MESSAGE FROM ME
await page.exposeFunction('onAddMessageFromMe', (message, typeMessage) => {
new Promise((resolve, reject) => {
this.sendToWebhookTo(message, this)
this.sendToSocket(message, this, typeMessage)
return true;
}).then(res => res).catch(e => {
if(APP_DEBUG)return e
})
})
}
// ON STATE CHANGE
await page.exposeFunction('onAppStateChangedEvent', (state) => {
try {
if(APP_DEBUG)console.log('onAppStateChangedEvent ' + state)
if (!['OPENING', 'CONNECTED', 'PAIRING', 'TIMEOUT'].includes(state)) {
if(APP_DEBUG)console.log('ALERT RELOAD ->' + state)
page.reload()
}
} catch (e) {
this.getLog('exposeFunction(onAppStateChangedEvent)', e)
}
});
// ON STATE CHANGE
await page.exposeFunction('onChangeState', () => {
try {
this.getStatePage(page).then(json => {
if (typeof json.state !== 'undefined') {
const stream = json.stream
const state = json.state
if (typeof json.ws !== 'undefined' && json.ws != '') {
const ws = json.ws
if (stream == 'disconnected' && state == 'conflict') {
this.getWebSocketPage(ws).then(json_page => {
if (json_page != null && typeof json_page === 'object' && typeof json_page.page !== 'undefined' && json_page.page != null) {
var browser = json_page.browser
browser.close()
return false
}
})
}
}
}
})
} catch (e) {
this.getLog('exposeFunction(onChangeState)', e)
}
})
// ADD MESSAGES EVENT
console.log('READING MESSAGES')
await page.evaluate((token, APP_DEBUG, APP_MESSAGE, APP_MESSAGE_ME, APP_HEADLESS) => {
setTimeout(() => {
window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); });
// EMITER EVENT APP STATE
window.Store.AppState.on('change', () => onChangeState())
// EMITER EVENTS MESSAGES ADD
if(!APP_HEADLESS){
const removeElements = (elms) => elms.forEach(el => el.remove());
removeElements(document.querySelectorAll("._1DzHI"));
document.body.className = "web dark"
}
if(APP_MESSAGE || APP_MESSAGE_ME){
window.Store.Msg.on('add', (new_message) => {
new Promise((resolve, reject) => {
const message = new_message.serialize()
if(message.id.remote != 'status@broadcast'){
message.apiToken = token
if (typeof message.type === 'undefined' || typeof message.isNewMsg === 'undefined' || message.isNewMsg == false ||message.broadcast == true){
return
}else{
if(APP_MESSAGE_ME && message.id.fromMe == true){
var typeMessage = 'FromMe';
message.typeMessage = typeMessage
onAddMessageFromMe(message, typeMessage)
}
else{
if(APP_MESSAGE){
var typeMessage = 'ToMe';
message.typeMessage = typeMessage
onAddMessageToMe(message, typeMessage)
}
}
if(APP_DEBUG){
console.log(message)
}
}
}
}).then(res => true).catch(e => false)
})
}
}, 4500)
}, token, APP_DEBUG, APP_MESSAGE, APP_MESSAGE_ME, APP_HEADLESS)
var end_time = (Date.now() - time_start) / 1000
var value = 'READY ' + token + ' IN ' + end_time + ' SEC.'
console.log(value)
this.emit('API', { action: 'ready', value: value, status: true })
} else await browser.close()
} catch (e) {
this.getLog('start', e)
}
}
getLog(name, e){
if (APP_DEBUG) {
console.log('function ' + name)
console.log(moment().format('YYYY-MM-DD HH:mm:ss'));
console.log(e)
}
}
async sendToWebhookTo(message, WhatsApp){
try {
new Promise((resolve, reject) => {
if (typeof message === 'object' && typeof message.apiToken !== 'undefined' && typeof message.from !== 'undefined') {
//if(APP_DEBUG)console.log(message);
const token = message.apiToken
if(token != ''){
const WhatsAppDB = WhatsApp.getDatabaseToken()
const data_token = WhatsAppDB.get('token').find({ name: token }).value()
if(typeof data_token === 'undefined' || typeof data_token.endpoint === 'undefined') {
console.log({ response: 'Not defined token', status: false })
}else{
if(typeof message.id !== 'undefined' && typeof message.id.fromMe !== 'undefined'){
try{
new Promise((resolve, reject) => {
WhatsApp.getWebSocketPage(data_token.endpoint).then(json_page => {
if (typeof json_page === 'object' && typeof json_page.page !== 'undefined' && json_page.page != null) {
var page = json_page.page
if(message.id.fromMe)var number = message.to;
else var number = message.from;
WhatsApp.getContact(page, number).then(contact => {
WhatsApp.getProfilePicThumb(page, number).then(picture => {
message.picture = picture
message.contact = contact
WhatsApp.sendToWebhook(message.typeMessage, message, data_token)
})
})
}
})
})
}catch(e){
WhatsApp.sendToWebhook(typeMessage, message, data_token)
}
return
}
}
}
return
}
})
} catch (e) {
this.getLog('sendToWebhookTo', e)
return
}
}
//SEND SOCKET
async sendToSocket(message, WhatsApp, typeMessage){
try {
new Promise((resolve, reject) => {
if (typeof message === 'object' && typeof message.apiToken !== 'undefined' && typeof message.from !== 'undefined') {
//if(APP_DEBUG)console.log(message);
const token = message.apiToken
const WhatsAppDB = WhatsApp.getDatabaseToken()
const data_token = WhatsAppDB.get('token').find({ name: token }).value()
if(typeof data_token === 'undefined' || typeof data_token.endpoint === 'undefined') {
return
}else{
if (typeof data_token.socket !== 'undefined' && data_token.socket != null) {
WhatsApp.getWebSocketPage(data_token.endpoint).then(json_page => {
var socket = data_token.socket;
if(socket != ''){
if(APP_DEBUG)console.log('SEND SOCKET ' + typeMessage + ' -> ' + socket)
var socket_client = require('socket.io-client')(socket)
message.typeMessage = typeMessage
socket_client.emit(typeMessage, message)
}
})
}
}
}
})
} catch (e) {
this.getLog('sendToSocket', e)
return
}
}
async sendToBot(message, WhatsApp){
try {
new Promise((resolve, reject) => {
if (typeof message === 'object' && typeof message.apiToken !== 'undefined' && typeof message.from !== 'undefined') {
//if(APP_DEBUG)console.log(message);
const token = message.apiToken
const WhatsAppDB = WhatsApp.getDatabaseToken()
const data_token = WhatsAppDB.get('token').find({ name: token }).value()
if(typeof data_token === 'undefined' || typeof data_token.endpoint === 'undefined') {
console.log({ response: 'Not defined token', status: false })
return false
}else{
//BOT ON MESSAGE
/*
-- POST
{
message: '',
body: {}
token: '',
}
-- GET
{
status: true,
type: 'message' or 'media'
message: '' or [],
}
*/
//GET BOT AND SEND MESSAGE
new Promise((resolve, reject) => {
if (typeof data_token.bot !== 'undefined' && data_token.bot != null) {
WhatsApp.getWebSocketPage(data_token.endpoint).then(json_page => {
if (typeof json_page === 'object' && typeof json_page.page !== 'undefined' && json_page.page != null) {
var page = json_page.page
var bot = data_token.bot
var from = message.from
message.token = token;
if(message.id.fromMe)var number = message.to;
else var number = message.from;
//WhatsApp.getContact(page, number).then(contact => {
//WhatsApp.getProfilePicThumb(page, number).then(picture => {
//message.picture = picture
//message.contact = contact
//SEND TO BOT
var send = {
method: 'post',
body: JSON.stringify(message),
headers: { 'Content-Type': 'application/json' }
}
//console.log(to_post)
//BOT RESPONSE
try{
fetch(bot, send)
.then(res => res.json())
.then(response => {
if (typeof response === 'object' && typeof response.status !== 'undefined' && response.status == true){
if(APP_DEBUG)console.log(response)
if (typeof response.message !== 'undefined' && response.message != ''){
var responseMessage = response.message
WhatsApp.sendMessage(page, from, responseMessage)
WhatsApp.sendToWebhook('to', responseMessage, data_token)
}
}
}).catch(error => {
if(APP_DEBUG)console.log(error);
})
} catch (e) { if(APP_DEBUG)console.log(e) }
//BOT RESPONSE
//})
//})
}
})
}
})
}
}
})
} catch (e) {
this.getLog('sendToBot', e)
return
}
}
//function sendToWebhook
sendToWebhook(action, message, data){
new Promise((resolve, reject) => {
if (typeof data.webhook !== 'undefined' && data.webhook != null) {
var webhook = data.webhook
try {
var send = { method: 'post', body: JSON.stringify({action: action, token: data.name, via: 'whatsapp', data: message}), headers: { 'Content-Type': 'application/json' } }
fetch(webhook, send)
.then(res => res.json())
.then(info => {
if(APP_DEBUG)console.log(info)
}).catch(e => {
this.getLog('sendToWebhook', e);
})
return
} catch (e) {
this.getLog('sendToWebhook', e);
return
}
}
})
}
isUrl(url){
try {
const data_url = new URL(url)
if (typeof data_url !== 'undefined' && typeof data_url.host !== 'undefined' && data_url.host != '') return true
else return false
} catch (e) {
this.getLog('isUrl', e)
return false
}
}
getDatabaseToken(){
var db = null
try {
var low = require('lowdb')
var FileSync = require('lowdb/adapters/FileSync')
var path = require('path')
var adapter = new FileSync(path.join(__dirname, 'dbTokenWhatsApp.json'))
var db = low(adapter)
db.defaults({ token: [] }).write()
} catch (e) {
this.getLog('getDatabaseToken', e)
}
return db
}
getTimeToSend(no){
var to_time = 3582
try {
if (typeof no === 'number' && no > 0) {
var max = 3500
var number_op = Math.ceil(no / max)
switch (number_op) {
case 1: to_time = 3551; break
case 2: to_time = 3478; break
case 3: to_time = 2936; break
case 4: to_time = 2582; break
case 5: to_time = 2433; break
case 6: to_time = 2791; break
default: to_time = 3304; break
}
}
} catch (e) {
this.getLog('getTimeToSend', e)
}
return to_time
}
async setWebSocket(ws_endpoint, token){
var port = null
try {
var port = await getFreePorts(1, APP_HOST).then(res => res[0])
const is_open = await isFreePort(port, APP_HOST).then(open => open[2])
if (is_open) {
await httpProxy.createServer({ target: ws_endpoint, ws: true, localAddress: APP_HOST }).listen(port)
}
} catch (e) {
this.getLog('setWebSocket', e)
}
return 'ws://' + APP_HOST + ':' + port
}
async getWebSocketPage(ws_url){
try {
const browser = await puppeteer.connect({ browserWSEndpoint: ws_url, ignoreHTTPSErrors: true })
const pages_created = await browser.pages()
return { browser: browser, page: pages_created[0], endpoint: ws_url, type: 'success' }
} catch (e) {
this.getLog('getWebSocketPage', e)
return { browser: null, page: null, endpoint: e.target.url, type: e.type }
}
}
async sendMessage(page, id, message){
var time = 1097
try {
if(typeof id === 'string' && id != ''){
setTimeout(() => {
return this.setMessage(page, id, message)
}, time)
}else if (typeof id === 'object' && id.length > 0){
var no_ids = id.length
var to_time = this.getTimeToSend(no_ids)
id.forEach((from) => {
setTimeout(() => {
new Promise((resolve, reject) => {
this.setMessage(page, from, message)
})
}, time)
time += to_time
})
return no_ids
}
} catch (e) {
this.getLog('sendMessage', e)
return { number: null, message: 'Message Error', status_code: 504 }
}
}
async setMessage(page, id, message){
try {
return await page.evaluate((message, id) => {
try {
if (typeof id === 'string' && id != '') {
const get_id = window.Store.Chat.get(id)
if (typeof get_id.id !== 'undefined' && typeof get_id.id.server !== 'undefined' && get_id.id.server == 'c.us') {
const number = id.replace(/\D+/g, '')
const replaceNumber = (message, number) => message.replace(/{number}/g, number)
window.App.sendSeen(id)
if (typeof message === 'string' && message != '') {
var message = replaceNumber(message, number)
window.App.sendMessage(get_id, message)
} else if (Array.isArray(message) && message.length > 0) {
message.forEach(async (data_message) => {
try{var data_message = JSON.parse(data_message);}catch(e){}
if (typeof data_message === 'string'){
if(data_message != ''){
var data_message = replaceNumber(data_message, number)
await window.App.sendMessage(get_id, data_message)
}
}else{
await window.App.sendMessage(get_id, '', data_message)
}
})
}
return { number: number, message: message, status_code: 200 }
} else return { number: null, message: 'NaN', status_code: 404 }
} else return { number: null, message: 'NaN', status_code: 404 }
} catch (e) {
return { number: null, message: 'Error', status_code: 504 }
}
}, message, id)
} catch (e) {
this.getLog('setMessage', e)
return { number: null, message: 'Close', contact: {}, status_code: 501 }
}
}
async getMedia(page, message){
try {
return await page.evaluate((message, APP_DEBUG) => {
return (async() => {
if(typeof message.mediaKey !== 'undefined' && message.mediaKey != '' && typeof message.clientUrl !== 'undefined' && message.clientUrl != '' && typeof message.mimetype !== 'undefined' && message.mimetype != '' && typeof message.type !== 'undefined' && message.type != ''){
const mbuffer = await window.App.downloadBuffer(message.clientUrl);
const mdecrypted = await window.Store.CryptoLib.decryptE2EMedia(message.type, mbuffer, message.mediaKey, message.mimetype);
const mdata = await window.App.readBlobAsync(mdecrypted._blob);
return {
data: mdata,
mimetype: message.mimetype,
filename: message.filename
}
}
return {
data: null,
mimetype: null,
filename: null
}
})();
}, message, APP_DEBUG)
} catch (e) {
this.getLog('getMedia', e)
return false
}
}
async getMe(page){
try {
return await page.evaluate(() => {
var data = window.Store.Conn.serialize()
data.picture = window.Store.Contact.get(window.Store.Conn.wid._serialized).__x_profilePicThumb.__x_img
return data
})
} catch (e) {
this.getLog('getMe', e)
return {}
}
}
async getContact(page, id){
try {
return await page.evaluate((id) => {
if (typeof id === 'undefined' || id == '') return window.Store.Contact.serialize()
else return window.Store.Contact.get(id).serialize()
}, id)
} catch (e) {
this.getLog('getContact', e)
return {}
}
}
async getProfilePicThumb(page, id){
try {
return await page.evaluate((id) => {
try{
if (typeof id === 'undefined' || id == '') return window.Store.Contact.serialize()
else return window.Store.Contact.get(id).__x_profilePicThumb.__x_img
}catch (e) {
return {}
}
}, id)
} catch (e) {
this.getLog('getProfilePicThumb', e)
return {}
}
}
async getChat(page, id){
try {
return await page.evaluate((id) => {
if (typeof id === 'undefined' || id == '') return window.Store.Chat.serialize()
else return window.Store.Chat.get(id).serialize()
}, id)
} catch (e) {
this.getLog('getChat', e)
return {}
}
}
async getChatProfile(page){
try {
return await page.evaluate(() => {
var chats = window.Store.Chat.serialize();
var array = [];
chats.forEach(chat => {
var user = chat.id._serialized;
var unread = chat.unreadCount;
var msgs = chat.msgs;
var last_msgs = msgs.slice(-1)[0]
var thumb = window.Store.Contact.get(user).__x_profilePicThumb.__x_img
var profile = window.Store.Contact.get(user).serialize()
var name = '';
if(typeof profile.name === 'undefined')name = profile.pushname;
else name = profile.name;
var to_chat = {user: user, name: name, picture: thumb.eurl, chat: {message: last_msgs.body, type: last_msgs.type, time: last_msgs.t, unread: unread}};
array.push(to_chat);
});
return array;
})
} catch (e) {
this.getLog('getChatProfile', e)
return []
}
}
async getChatStats(page){
try {
return await page.evaluate(() => {
var chats = window.Store.Chat.serialize()
var no_chats = chats.length
var no_unread = 0
chats.forEach((chat) => {
if (typeof chat.unreadCount !== 'undefined' && chat.unreadCount > 0)++no_unread
})
return { chat: no_chats, unread: no_unread }
})
} catch (e) {
this.getLog('getChatStats', e)
return { chat: 0, unread: 0 }
}
}
async getChatUnread(page){
try {
return await page.evaluate(() => {
var chats = window.Store.Chat.serialize()
if (typeof chats === 'object' && chats.length > 0) {
var chats_unread = []
chats.forEach((chat) => {
if (typeof chat.unreadCount !== 'undefined' && typeof chat.msgs !== 'undefined') {
var number = chat.id._serialized
var unread = chat.unreadCount
var msg = chat.msgs
if (unread > 0) {
var msg_unread = msg.slice(-unread)
chats_unread.push({ number: number, unread: unread, message: msg_unread })
}
}
})
return chats_unread
}
})
} catch (e) {
this.getLog('getChatUnread', e)
return {}
}
}
// CHECKED
async loadEarlierMsgstById(page, id){
try {
return await page.evaluate((id, APP_DEBUG) => {
if (id != '') {
try {
window.Store.Chat.get(id).loadEarlierMsgs()
return true
} catch (e) { if (APP_DEBUG)console.log(e) }
}
return false
}, id, APP_DEBUG)
} catch (e) {
this.getLog('loadEarlierMsgstById', e)
return false
}
}
async setContactSeen(page, id){
try {
return await page.evaluate((id, APP_DEBUG) => {
if (id != '') {
try {
window.App.sendSeen(id)
return true
} catch (e) { if (APP_DEBUG)console.log(e) };
}
return false
}, id, APP_DEBUG)
} catch (e) {
this.getLog('setContactSeen', e)
return false
}
}
async setLogout(page){
try {