Skip to content

Commit

Permalink
Create sessions.js
Browse files Browse the repository at this point in the history
  • Loading branch information
narlles authored Mar 29, 2021
1 parent ab26b66 commit 77331d4
Showing 1 changed file with 230 additions and 0 deletions.
230 changes: 230 additions & 0 deletions sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 77331d4

Please sign in to comment.