new(whatsapp.gblib): Native provider works in groups now.
This commit is contained in:
parent
b23111dfad
commit
60b3536d8b
6 changed files with 55 additions and 39 deletions
|
@ -119,6 +119,7 @@
|
||||||
"prism-media": "1.3.1",
|
"prism-media": "1.3.1",
|
||||||
"public-ip": "4.0.4",
|
"public-ip": "4.0.4",
|
||||||
"puppeteer": "13.7.0",
|
"puppeteer": "13.7.0",
|
||||||
|
"puppeteer-extra": "^3.3.4",
|
||||||
"puppeteer-extra-plugin-stealth": "2.4.5",
|
"puppeteer-extra-plugin-stealth": "2.4.5",
|
||||||
"qrcode": "^1.5.0",
|
"qrcode": "^1.5.0",
|
||||||
"qrcode-terminal": "0.12.0",
|
"qrcode-terminal": "0.12.0",
|
||||||
|
|
|
@ -142,7 +142,7 @@ export class DialogKeywords {
|
||||||
if (!this.browser) {
|
if (!this.browser) {
|
||||||
this.browser = await createBrowser(null);
|
this.browser = await createBrowser(null);
|
||||||
}
|
}
|
||||||
const page = await this.browser.newPage();
|
const page = (await this.browser.pages())[0];
|
||||||
if (username || password) {
|
if (username || password) {
|
||||||
await page.authenticate({ 'username': username, 'password': password });
|
await page.authenticate({ 'username': username, 'password': password });
|
||||||
}
|
}
|
||||||
|
@ -455,9 +455,6 @@ export class DialogKeywords {
|
||||||
* @example EXIT
|
* @example EXIT
|
||||||
*/
|
*/
|
||||||
public async exit(step) {
|
public async exit(step) {
|
||||||
if (this.browser) {
|
|
||||||
await this.browser.close();
|
|
||||||
}
|
|
||||||
await step.endDialog();
|
await step.endDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -865,6 +865,10 @@ export class GBVMService extends GBService {
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
await step.endDialog();
|
await step.endDialog();
|
||||||
}
|
}
|
||||||
|
if (sandbox.browser) {
|
||||||
|
await sandbox.browser.close();
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`BASIC ERROR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
|
throw new Error(`BASIC ERROR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,14 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer-extra')
|
||||||
const pluginStealth = require('puppeteer-extra-plugin-stealth');
|
const Fs = require('fs');
|
||||||
|
|
||||||
|
// const StealthPlugin = require('puppeteer-extra-plugin-stealth')
|
||||||
|
// puppeteer.use(StealthPlugin());
|
||||||
|
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
|
import urljoin = require("url-join");
|
||||||
const Path = require('path');
|
const Path = require('path');
|
||||||
|
|
||||||
// https://hackernoon.com/tips-and-tricks-for-web-scraping-with-puppeteer-ed391a63d952
|
// 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> {
|
async function createBrowser(profilePath): Promise<any> {
|
||||||
|
|
||||||
let args = [
|
let args = [
|
||||||
'--ignore-certificate-errors',
|
'--check-for-update-interval=2592000',
|
||||||
'--no-sandbox',
|
'--disable-accelerated-2d-canvas',
|
||||||
'--disable-setuid-sandbox',
|
'--disable-dev-shm-usage',
|
||||||
'--window-size=1920,1080',
|
'--disable-features=site-per-process',
|
||||||
"--disable-accelerated-2d-canvas",
|
'--disable-gpu',
|
||||||
"--disable-gpu",
|
'--no-first-run',
|
||||||
"--disable-features=site-per-process"
|
'--no-default-browser-check'
|
||||||
];
|
];
|
||||||
|
|
||||||
if (profilePath) {
|
if (profilePath) {
|
||||||
args.push(`--user-data-dir=${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({
|
const browser = await puppeteer.launch({
|
||||||
args: args,
|
args: args,
|
||||||
ignoreHTTPSErrors: true,
|
ignoreHTTPSErrors: true,
|
||||||
headless: false,
|
headless: false,
|
||||||
|
devTools: false,
|
||||||
|
defaultViewport: null,
|
||||||
|
ignoreDefaultArgs: ["--enable-automation", "--enable-blink-features=IdleDetection"]
|
||||||
});
|
});
|
||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,8 +47,7 @@ import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords';
|
||||||
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
||||||
import { GBMinService } from '../../core.gbapp/services/GBMinService';
|
import { GBMinService } from '../../core.gbapp/services/GBMinService';
|
||||||
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
||||||
|
import { createBrowser } from '../../core.gbapp/services/GBSSR';
|
||||||
const puppeteer = require('puppeteer');
|
|
||||||
|
|
||||||
const { MessageMedia, Client, LocalAuth } = require('whatsapp-web.js');
|
const { MessageMedia, Client, LocalAuth } = require('whatsapp-web.js');
|
||||||
const qrcode = require('qrcode-terminal');
|
const qrcode = require('qrcode-terminal');
|
||||||
|
@ -143,33 +142,24 @@ export class WhatsappDirectLine extends GBService {
|
||||||
// Initialize the browser using a local profile for each bot.
|
// Initialize the browser using a local profile for each bot.
|
||||||
|
|
||||||
const gbaiName = `${this.min.botId}.gbai`;
|
const gbaiName = `${this.min.botId}.gbai`;
|
||||||
const localName = Path.join('work', gbaiName, 'profile');
|
const profilePath = Path.join('work', gbaiName, 'profile');
|
||||||
|
|
||||||
const createClient = async (browserWSEndpoint) => {
|
const createClient = async (browserWSEndpoint) => {
|
||||||
let puppeteer: any = {
|
|
||||||
headless: false, args: [
|
if (!browserWSEndpoint) {
|
||||||
'--no-sandbox',
|
const browser = await createBrowser(profilePath);
|
||||||
'--disable-setuid-sandbox',
|
this.browserWSEndpoint = await browser.wsEndpoint();
|
||||||
'--disable-dev-shm-usage',
|
}
|
||||||
'--disable-accelerated-2d-canvas',
|
else{
|
||||||
'--no-first-run',
|
this.browserWSEndpoint = browserWSEndpoint;
|
||||||
'--no-zygote',
|
|
||||||
'--single-process',
|
|
||||||
'--disable-gpu',
|
|
||||||
'--disable-infobars',
|
|
||||||
'--disable-features=site-per-process',
|
|
||||||
`--user-data-dir=${localName}`]
|
|
||||||
};
|
|
||||||
if (browserWSEndpoint) {
|
|
||||||
puppeteer = { browserWSEndpoint: browserWSEndpoint };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = this.customClient = new Client({
|
const client = this.customClient = new Client({
|
||||||
authStrategy: new LocalAuth({
|
authStrategy: new LocalAuth({
|
||||||
clientId: this.min.botId,
|
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));
|
}).bind(this));
|
||||||
|
|
||||||
client.on('authenticated', async () => {
|
client.on('authenticated', async () => {
|
||||||
this.browserWSEndpoint = client.pupBrowser.wsEndpoint();
|
|
||||||
GBLog.verbose(`GBWhatsApp: QR Code authenticated for ${this.botId}.`);
|
GBLog.verbose(`GBWhatsApp: QR Code authenticated for ${this.botId}.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('ready', async () => {
|
client.on('ready', async () => {
|
||||||
|
const page = (await client.pupBrowser.pages())[0];
|
||||||
|
await page.close();
|
||||||
client.pupBrowser.on('disconnected', (async () => {
|
client.pupBrowser.on('disconnected', (async () => {
|
||||||
GBLog.info(`Browser terminated. Restarting ${this.min.botId} WhatsApp native provider.`);
|
GBLog.info(`Browser terminated. Restarting ${this.min.botId} WhatsApp native provider.`);
|
||||||
await (createClient.bind(this))(null);
|
await (createClient.bind(this))(null);
|
||||||
|
|
|
@ -13333,6 +13333,15 @@ puppeteer-extra-plugin@^3.1.2, puppeteer-extra-plugin@^3.2.2:
|
||||||
debug "^4.1.1"
|
debug "^4.1.1"
|
||||||
merge-deep "^3.0.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:
|
puppeteer@13.7.0, puppeteer@^13.0.0:
|
||||||
version "13.7.0"
|
version "13.7.0"
|
||||||
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-13.7.0.tgz#18e16f83e397cf02f7a0804c67c1603d381cfb0b"
|
resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-13.7.0.tgz#18e16f83e397cf02f7a0804c67c1603d381cfb0b"
|
||||||
|
|
Loading…
Add table
Reference in a new issue