-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.js
230 lines (214 loc) · 8.63 KB
/
sessions.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
// person.js
'use strict';
const os = require('os');
const fs = require('fs');
const path = require('path');
const venom = require('venom-bot');
const { json } = require('express');
const { Session } = require('inspector');
module.exports = class Sessions {
static async start(sessionName) {
Sessions.sessions = Sessions.sessions || []; //start array
var session = Sessions.getSession(sessionName);
if (session == false) { //create new session
console.log("session == false");
session = await Sessions.addSesssion(sessionName);
} else if (["CLOSED"].includes(session.state)) { //restart session
console.log("session.state == CLOSED");
session.state = "STARTING";
session.status = 'notLogged';
session.client = Sessions.initSession(sessionName);
Sessions.setup(sessionName);
} else if (["CONFLICT", "UNPAIRED", "UNLAUNCHED"].includes(session.state)) {
console.log("client.useHere()");
session.client.then(client => {
client.useHere();
});
} else {
console.log("session.state: " + session.state);
}
return session;
}//start
static async addSesssion(sessionName) {
var newSession = {
name: sessionName,
qrcode: false,
client: false,
status: 'notLogged',
state: 'STARTING'
}
Sessions.sessions.push(newSession);
console.log("newSession.state: " + newSession.state);
//setup session
newSession.client = Sessions.initSession(sessionName);
Sessions.setup(sessionName);
return newSession;
}//addSession
static async initSession(sessionName) {
var session = Sessions.getSession(sessionName);
const client = await venom.create(
sessionName,
(base64Qr) => {
session.state = "QRCODE";
session.qrcode = base64Qr;
console.log("new qrcode updated - session.state: " + session.state);
},
(statusFind) => {
session.status = statusFind;
console.log("session.status: " + session.status);
},
{
headless: true,
devtools: false,
useChrome: false,
debug: false,
logQR: 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',
],
refreshQR: 15000,
autoClose: 60 * 60 * 24 * 365, //never
disableSpins: true
}
);
return client;
}//initSession
static async setup(sessionName) {
var session = Sessions.getSession(sessionName);
await session.client.then(client => {
client.onStateChange(state => {
session.state = state;
console.log("session.state: " + state);
});//.then((client) => Sessions.startProcess(client));
client.onMessage((message) => {
if (message.body === 'hi') {
client.sendText(message.from, 'Hello\nfriend!');
}
});
});
}//setup
static async closeSession(sessionName) {
var session = Sessions.getSession(sessionName);
if (session) { //só adiciona se não existir
if (session.state != "CLOSED") {
if (session.client)
await session.client.then(async client => {
try {
await client.close();
} catch (error) {
console.log("client.close(): " + error.message);
}
session.state = "CLOSED";
session.client = false;
console.log("client.close - session.state: " + session.state);
});
return { result: "success", message: "CLOSED" };
} else {//close
return { result: "success", message: session.state };
}
} else {
return { result: "error", message: "NOTFOUND" };
}
}//close
static getSession(sessionName) {
var foundSession = false;
if (Sessions.sessions)
Sessions.sessions.forEach(session => {
if (sessionName == session.name) {
foundSession = session;
}
});
return foundSession;
}//getSession
static getSessions() {
if (Sessions.sessions) {
return Sessions.sessions;
} else {
return [];
}
}//getSessions
static async getQrcode(sessionName) {
var session = Sessions.getSession(sessionName);
if (session) {
//if (["UNPAIRED", "UNPAIRED_IDLE"].includes(session.state)) {
if (["UNPAIRED_IDLE"].includes(session.state)) {
//restart session
await Sessions.closeSession(sessionName);
Sessions.start(sessionName);
return { result: "error", message: session.state };
} else if (["CLOSED"].includes(session.state)) {
Sessions.start(sessionName);
return { result: "error", message: session.state };
} else { //CONNECTED
if (session.status != 'isLogged') {
return { result: "success", message: session.state, qrcode: session.qrcode };
} else {
return { result: "success", message: session.state };
}
}
} else {
return { result: "error", message: "NOTFOUND" };
}
} //getQrcode
static async sendText(sessionName, number, text) {
var session = Sessions.getSession(sessionName);
if (session) {
if (session.state == "CONNECTED" || session.state == "QRCODE" || session.status == "isLogged") {
var resultSendText = await session.client.then(async client => {
return await client.sendText(number + '@c.us', text);
});
return { result: "success" }
} else {
return { result: "error", message: session.state };
}
} else {
return { result: "error", message: "NOTFOUNDSENDTXT" };
}
}//message
static async sendFile(sessionName, number, base64Data, fileName, caption) {
var session = Sessions.getSession(sessionName);
if (session) {
if (session.state == "CONNECTED") {
var resultSendFile = await session.client.then(async (client) => {
var folderName = fs.mkdtempSync(path.join(os.tmpdir(), session.name + '-'));
var filePath = path.join(folderName, fileName);
fs.writeFileSync(filePath, base64Data, 'base64');
console.log(filePath);
return await client.sendFile(number + '@c.us', filePath, fileName, caption);
});//client.then(
return { result: "success" };
} else {
return { result: "error", message: session.state };
}
} else {
return { result: "error", message: "NOTFOUND" };
}
}//message
}