Skip to content

Commit

Permalink
Atualizado o Cliente com LocalAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Mar 21, 2022
1 parent a594b33 commit 470b727
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions backend/src/libs/wbot.ts
@@ -1,11 +1,10 @@
import qrCode from "qrcode-terminal";
import { Client } from "whatsapp-web.js";
import { Client, LocalAuth } from "whatsapp-web.js";
import { getIO } from "./socket";
import Whatsapp from "../models/Whatsapp";
import AppError from "../errors/AppError";
import { logger } from "../utils/logger";
import { handleMessage } from "../services/WbotServices/wbotMessageListener";
import fs from 'fs'

interface Session extends Client {
id?: number;
Expand Down Expand Up @@ -37,14 +36,22 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
return new Promise((resolve, reject) => {
try {
const io = getIO();
const sessionName = whatsapp.name;
const SESSION_FILE_PATH = './session.json';
let sessionCfg;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionCfg = require(SESSION_FILE_PATH);
}

const wbot: Session = new Client({ puppeteer: { headless: true}/*, clientId: 'bd_'+whatsapp.id */});
const sessionName = whatsapp.name;
let sessionCfg;

if (whatsapp && whatsapp.session) {
sessionCfg = JSON.parse(whatsapp.session);
}

const wbot: Session = new Client({
session: sessionCfg,
authStrategy: new LocalAuth({clientId: 'bd_'+whatsapp.id}),
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: process.env.CHROME_BIN || undefined
},
});

wbot.initialize();

wbot.on("qr", async qr => {
Expand All @@ -66,9 +73,9 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {

wbot.on("authenticated", async session => {
logger.info(`Session: ${sessionName} AUTHENTICATED`);
await whatsapp.update({
session: JSON.stringify(session)
});
// await whatsapp.update({
// session: JSON.stringify(session)
// });
});

wbot.on("auth_failure", async msg => {
Expand Down Expand Up @@ -144,4 +151,4 @@ export const removeWbot = (whatsappId: number): void => {
} catch (err) {
logger.error(err);
}
};
};

0 comments on commit 470b727

Please sign in to comment.