-
Notifications
You must be signed in to change notification settings - Fork 7
/
engine.js
250 lines (232 loc) · 7.31 KB
/
engine.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
//
// Configuração dos módulos
const fs = require('fs-extra');
const rimraf = require("rimraf");
const sleep = require('sleep-promise');
const fnSocket = require('./controllers/fnSockets');
const {
forEach
} = require('p-iteration');
/*
const PQueue = require("p-queue");
const queue = new PQueue({
concurrency: 1
});
*/
if (fs.existsSync('./wppconnect/dist/index.js')) {
//
console.log("- Wppconnect is patch");
var wppconnect = require('./wppconnect/dist/index');
//
} else {
console.log("- Wppconnect is package");
var wppconnect = require('@wppconnect-team/wppconnect');
}
//
const events = require('./controllers/events');
const webhooks = require('./controllers/webhooks.js');
const Sessions = require('./controllers/sessions.js');
const config = require('./config.global');
//
// ------------------------------------------------------------------------------------------------------- //
//
module.exports = class Wppconnect {
static async Start(socket, SessionName, AuthorizationToken, MultiDevice, whatsappVersion) {
let session = SessionName;
var data = Sessions?.getSession(session);
const funcoesSocket = new fnSocket(socket);
funcoesSocket.events(session, {
message: 'Iniciando WhatsApp. Aguarde...',
state: 'STARTING',
session: session
});
if (data) {
this.initSession(socket, SessionName, AuthorizationToken, MultiDevice, whatsappVersion)
}
else
if (data == false) {
Sessions.checkAddUser(session);
Sessions.addInfoSession(session, {
state: 'STARTING',
status: 'notLogged',
funcoesSocket: funcoesSocket
});
this.initSession(socket, SessionName, AuthorizationToken, MultiDevice, whatsappVersion);
}
}
static async initSession(socket, SessionName, AuthorizationToken, MultiDevice, whatsappVersion) {
let session = SessionName
//let token = await this.getToken(session);
const funcoesSocket = new fnSocket(socket);
try {
wppconnect?.create({
session: session,
whatsappVersion: whatsappVersion ? `${whatsappVersion}` : '', // whatsappVersion: '2.2204.13',
deviceName: `${config.DEVICE_NAME}`,
tokenStore: 'memory',
catchQR: (base64Qrimg, urlCode) => {
webhooks?.wh_qrcode(session, base64Qrimg, urlCode)
this.exportQR(socket, base64Qrimg, urlCode, session);
Sessions?.addInfoSession(session, {
qrCode: base64Qrimg,
urlCode: urlCode,
state: "QRCODE"
})
},
statusFind: (statusSession, session) => {
Sessions?.addInfoSession(session, {
status: statusSession
})
webhooks?.wh_connect(session, statusSession)
if (statusSession == 'qrReadSuccess') {
funcoesSocket.events(session, {
message: 'Leitura do QRCode feita com sucesso...',
status: statusSession,
session: session
});
}
if (statusSession == 'qrReadError' || statusSession == 'qrReadFail') {
funcoesSocket.events(session, {
message: 'Falha na leitura do QRCode ...',
status: statusSession,
state: 'DISCONNECTED',
session: session
});
}
if (statusSession === 'browserClose' ||
statusSession === 'desconnectedMobile' ||
statusSession === 'phoneNotConnected' ||
statusSession === 'autocloseCalled' ||
statusSession === 'serverClose') {
funcoesSocket.events(session, {
message: 'Desconectado...',
state: 'DISCONNECTED',
status: statusSession,
session: session
});
}
if (statusSession === 'isLogged' ||
statusSession === 'chatsAvailable' ||
statusSession === 'inChat') {
funcoesSocket.events(session, {
message: 'WhatsApp autenticado com sucesso...',
state: 'CONNECTED',
status: statusSession,
session: session
});
}
},
headless: true,
logQR: parseInt(config.VIEW_QRCODE_TERMINAL), // Logs QR automatically in terminal
browserWS: config.BROWSER_WSENDPOINT ? `${config.BROWSER_WSENDPOINT}` : '', // If u want to use browserWSEndpoint
useChrome: true,
updatesLog: true,
autoClose: parseInt(config.AUTO_CLOSE), // Automatically closes the wppconnect only when scanning the QR code (default 60 seconds, if you want to turn it off, assign 0 or false)
browserArgs: [
'--log-level=3',
'--no-default-browser-check',
'--disable-site-isolation-trials',
'--no-experiments',
'--ignore-gpu-blacklist',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list',
'--disable-gpu',
'--disable-extensions',
'--disable-default-apps',
'--enable-features=NetworkService',
'--disable-setuid-sandbox',
'--no-sandbox',
// Extras
'--disable-webgl',
'--disable-threaded-animation',
'--disable-threaded-scrolling',
'--disable-in-process-stack-traces',
'--disable-histogram-customizer',
'--disable-gl-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',
],
disableSpins: false,
createPathFileToken: false,
puppeteerOptions: {
userDataDir: MultiDevice ? `${tokenPatch}/WPP-${SessionName}` : undefined, // or your custom directory
browserWSEndpoint: config.BROWSER_WSENDPOINT ? `${config.BROWSER_WSENDPOINT}` : undefined,
},
waitForLogin: true
}).then(async (client) => {
let tokens = await client.getSessionTokenBrowser();
let phone = await client?.getWid();
webhooks?.wh_connect(session, 'CONNECTED', phone);
events?.receiveMessage(session, client, socket);
events?.statusMessage(session, client, socket);
events?.statusConnection(session, client, socket);
events?.extraEvents(session, client, socket);
//
Sessions?.addInfoSession(session, {
state: 'CONNECTED',
client: client,
tokens: tokens,
phone: phone
});
client?.onIncomingCall(async (call) => {
if (req?.body?.AutoRejectCall == 'true') {
client?.rejectCall();
}
if (config?.incoming_call !== '') {
client?.sendText(call?.peerJid, config?.incoming_call);
}
});
console?.log({
"result": 200,
"status": "CONNECTED",
"response": `Sessão ${SessionName} gravada com sucesso no Firebase`
});
}).catch(async (error) => {
//exec(`rm -R tokens/${session}/`);
//
exec(`rm -rf ${config.tokenPatch}/${session}.data.json`, (error, stdout, stderr) => {
if (error) {
console.log(`Error: ${error.message}`);
return;
}
if (stderr) {
console.log(`Stderr: ${stderr}`);
return;
}
console.log(`Stdout: ${stdout}`);
});
//
exec(`rm -rf ${config.tokenPatch}/WPP-${session}/`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
//
})
wppconnect.defaultLogger.level = 'silly';
} catch (error) {
return error
}
}
static async exportQR(socket, qrCode, urlCode, session) {
socket.emit('events', {
message: 'QRCode Iniciando, Escanei por favor...',
state: 'QRCODE',
qrCode: qrCode,
urlCode: urlCode,
session: session
})
}
}