new(whatsapp.gblib): Native provider works in groups now.

This commit is contained in:
rodrigorodriguez 2022-10-12 20:38:35 -03:00
parent b23111dfad
commit 60b3536d8b
6 changed files with 55 additions and 39 deletions

View file

@ -119,6 +119,7 @@
"prism-media": "1.3.1",
"public-ip": "4.0.4",
"puppeteer": "13.7.0",
"puppeteer-extra": "^3.3.4",
"puppeteer-extra-plugin-stealth": "2.4.5",
"qrcode": "^1.5.0",
"qrcode-terminal": "0.12.0",

View file

@ -142,7 +142,7 @@ export class DialogKeywords {
if (!this.browser) {
this.browser = await createBrowser(null);
}
const page = await this.browser.newPage();
const page = (await this.browser.pages())[0];
if (username || password) {
await page.authenticate({ 'username': username, 'password': password });
}
@ -455,9 +455,6 @@ export class DialogKeywords {
* @example EXIT
*/
public async exit(step) {
if (this.browser) {
await this.browser.close();
}
await step.endDialog();
}

View file

@ -865,6 +865,10 @@ export class GBVMService extends GBService {
if (ret == -1) {
await step.endDialog();
}
if (sandbox.browser) {
await sandbox.browser.close();
}
} catch (error) {
throw new Error(`BASIC ERROR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
}

View file

@ -36,9 +36,14 @@
'use strict';
const puppeteer = require('puppeteer');
const pluginStealth = require('puppeteer-extra-plugin-stealth');
const puppeteer = require('puppeteer-extra')
const Fs = require('fs');
// const StealthPlugin = require('puppeteer-extra-plugin-stealth')
// puppeteer.use(StealthPlugin());
import { NextFunction, Request, Response } from "express";
import urljoin = require("url-join");
const Path = require('path');
// https://hackernoon.com/tips-and-tricks-for-web-scraping-with-puppeteer-ed391a63d952
@ -84,23 +89,32 @@ const RENDER_CACHE = new Map();
async function createBrowser(profilePath): Promise<any> {
let args = [
'--ignore-certificate-errors',
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=1920,1080',
"--disable-accelerated-2d-canvas",
"--disable-gpu",
"--disable-features=site-per-process"
'--check-for-update-interval=2592000',
'--disable-accelerated-2d-canvas',
'--disable-dev-shm-usage',
'--disable-features=site-per-process',
'--disable-gpu',
'--no-first-run',
'--no-default-browser-check'
];
if (profilePath){
args.push(`--user-data-dir=${profilePath}`);
if (profilePath) {
args.push(`--user-data-dir=${profilePath}`);
const preferences = urljoin(profilePath, "Default", "Preferences");
const file = Fs.readFileSync(preferences, "utf8")
const data = JSON.parse(file)
data["profile"]['exit_type'] = "none";
Fs.writeFileSync(preferences, JSON.stringify(data))
}
const browser = await puppeteer.launch({
args: args,
ignoreHTTPSErrors: true,
headless: false,
devTools: false,
defaultViewport: null,
ignoreDefaultArgs: ["--enable-automation", "--enable-blink-features=IdleDetection"]
});
return browser;
}

View file

@ -47,8 +47,7 @@ import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
import { GBMinService } from '../../core.gbapp/services/GBMinService';
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
const puppeteer = require('puppeteer');
import { createBrowser } from '../../core.gbapp/services/GBSSR';
const { MessageMedia, Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
@ -143,33 +142,24 @@ export class WhatsappDirectLine extends GBService {
// Initialize the browser using a local profile for each bot.
const gbaiName = `${this.min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'profile');
const profilePath = Path.join('work', gbaiName, 'profile');
const createClient = async (browserWSEndpoint) => {
let puppeteer: any = {
headless: false, args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process',
'--disable-gpu',
'--disable-infobars',
'--disable-features=site-per-process',
`--user-data-dir=${localName}`]
};
if (browserWSEndpoint) {
puppeteer = { browserWSEndpoint: browserWSEndpoint };
if (!browserWSEndpoint) {
const browser = await createBrowser(profilePath);
this.browserWSEndpoint = await browser.wsEndpoint();
}
else{
this.browserWSEndpoint = browserWSEndpoint;
}
const client = this.customClient = new Client({
authStrategy: new LocalAuth({
clientId: this.min.botId,
dataPath: localName
dataPath: profilePath
}),
puppeteer: puppeteer
puppeteer: { browserWSEndpoint: this.browserWSEndpoint }
});
@ -207,12 +197,13 @@ export class WhatsappDirectLine extends GBService {
}).bind(this));
client.on('authenticated', async () => {
this.browserWSEndpoint = client.pupBrowser.wsEndpoint();
GBLog.verbose(`GBWhatsApp: QR Code authenticated for ${this.botId}.`);
});
client.on('ready', async () => {
const page = (await client.pupBrowser.pages())[0];
await page.close();
client.pupBrowser.on('disconnected', (async () => {
GBLog.info(`Browser terminated. Restarting ${this.min.botId} WhatsApp native provider.`);
await (createClient.bind(this))(null);

View file

@ -13333,6 +13333,15 @@ puppeteer-extra-plugin@^3.1.2, puppeteer-extra-plugin@^3.2.2:
debug "^4.1.1"
merge-deep "^3.0.1"
puppeteer-extra@^3.3.4:
version "3.3.4"
resolved "https://registry.yarnpkg.com/puppeteer-extra/-/puppeteer-extra-3.3.4.tgz#e0ecf021783d1112b6b0db20546d5022e632ed55"
integrity sha512-fN5pHvSMJ8d1o7Z8wLLTQOUBpORD2BcFn+KDs7QnkGZs9SV69hcUcce67vX4L4bNSEG3A0P6Osrv+vWNhhdm8w==
dependencies:
"@types/debug" "^4.1.0"
debug "^4.1.1"
deepmerge "^4.2.2"
puppeteer@13.7.0, puppeteer@^13.0.0:
version "13.7.0"
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-13.7.0.tgz#18e16f83e397cf02f7a0804c67c1603d381cfb0b"