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

This commit is contained in:
rodrigorodriguez 2022-10-14 11:54:31 -03:00
parent a748cb274d
commit 8df8ed5f91

View file

@ -47,7 +47,8 @@ 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');
@ -142,10 +143,9 @@ 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 profilePath = Path.join('work', gbaiName, 'profile'); const localName = Path.join('work', gbaiName, 'profile');
const createClient = async (browserWSEndpoint) => { const createClient = async (browserWSEndpoint) => {
let puppeteer: any = { let puppeteer: any = {
headless: false, args: [ headless: false, args: [
'--no-sandbox', '--no-sandbox',
@ -158,7 +158,7 @@ export class WhatsappDirectLine extends GBService {
'--disable-gpu', '--disable-gpu',
'--disable-infobars', '--disable-infobars',
'--disable-features=site-per-process', '--disable-features=site-per-process',
`--user-data-dir=${profilePath}`] `--user-data-dir=${localName}`]
}; };
if (browserWSEndpoint) { if (browserWSEndpoint) {
puppeteer = { browserWSEndpoint: browserWSEndpoint }; puppeteer = { browserWSEndpoint: browserWSEndpoint };
@ -167,7 +167,7 @@ export class WhatsappDirectLine extends GBService {
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: profilePath dataPath: localName
}), }),
puppeteer: puppeteer puppeteer: puppeteer
}); });
@ -184,13 +184,13 @@ export class WhatsappDirectLine extends GBService {
// Sends QR Code to boot bot admin. // Sends QR Code to boot bot admin.
const msg = `Please, scan the QR Code to restore bot ${this.botId}.`; const msg = `Please, scan QR Code with for bot ${this.botId}.`;
GBLog.info(msg); GBLog.info(msg);
qrcode.generate(qr, { small: true, scale: 0.5 }); qrcode.generate(qr, { small: true, scale: 0.5 });
// While handling other bots uses boot instance of this class to send QR Codes. // While handling other bots uses boot instance of this class to send QR Codes.
const s = new DialogKeywords(this.min, null, null, null); const s = new DialogKeywords(null, null, null, null);
const qrBuf = await s.getQRCode(qr); const qrBuf = await s.getQRCode(qr);
const gbaiName = `${this.min.botId}.gbai`; const gbaiName = `${this.min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`); const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`);
@ -201,19 +201,13 @@ export class WhatsappDirectLine extends GBService {
'cache', 'cache',
Path.basename(localName) Path.basename(localName)
); );
if (minBoot.botId !== this.botId) { GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg);
GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg); s.sendEmail(adminEmail, `Check your WhatsApp for bot ${this.botId}`, msg);
}
// The e-mail is sent to all bot owners.
const html = `<P>${msg}</P><IMG src="${url}"/>`
await s.sendEmail(adminEmail, `Check your WhatsApp for bot ${this.botId}`, html);
}).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}.`);
}); });