Skip to content

Commit

Permalink
fix(whatsapp.gblib): #288 unify puppteer params with GBSSR.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Mar 6, 2023
1 parent 0d5931d commit b96a78e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@nlpjs/basic": "4.26.1",
"@nosferatu500/textract": "3.1.2",
"@push-rpc/core": "1.1.5",
"@push-rpc/http": "1.1.5",
"@push-rpc/http": "^1.1.5",
"@push-rpc/websocket": "1.1.5",
"@semantic-release/changelog": "5.0.1",
"@semantic-release/exec": "5.0.0",
Expand Down Expand Up @@ -143,8 +143,8 @@
"puppeteer": "19.6.3",
"puppeteer-extra": "3.3.4",
"puppeteer-extra-plugin-stealth": "2.11.1",
"qrcode": "1.5.1",
"qr-scanner": "^1.4.2",
"qrcode": "1.5.1",
"qrcode-terminal": "0.12.0",
"readline": "1.3.0",
"reflect-metadata": "0.1.13",
Expand Down
14 changes: 10 additions & 4 deletions packages/core.gbapp/services/GBSSR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class GBSSR {
'tiqcdn'
];

public static async createBrowser(profilePath): Promise<any> {
public static preparePuppeteer(profilePath){
let args = [
'--check-for-update-interval=2592000',
'--disable-accelerated-2d-canvas',
Expand All @@ -113,15 +113,21 @@ export class GBSSR {
Fs.writeFileSync(preferences, JSON.stringify(data));
}
}
puppeteer.use(hidden());
const browser = await puppeteer.launch({

return {
args: args,
ignoreHTTPSErrors: true,
headless: false,
defaultViewport: null,
executablePath: executablePath(),
ignoreDefaultArgs: ['--enable-automation', '--enable-blink-features=IdleDetection']
});
};
}

public static async createBrowser(profilePath): Promise<any> {
const opts = this.preparePuppeteer(profilePath);
puppeteer.use(hidden());
const browser = await puppeteer.launch( opts );
return browser;
}

Expand Down
41 changes: 18 additions & 23 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
\*****************************************************************************/

import urlJoin from 'url-join';
import Swagger from 'swagger-client';
import SwaggerClient from 'swagger-client';
import Path from 'path';
import Fs from 'fs';
import { GBError, GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib';
Expand All @@ -49,6 +49,7 @@ import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { method } from 'lodash';
import pkg from 'whatsapp-web.js';
import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
const { Buttons, Client, MessageMedia } = pkg;

/**
Expand Down Expand Up @@ -116,28 +117,25 @@ export class WhatsappDirectLine extends GBService {
}

public async sendButton() {
let url = 'https://wwebjs.dev/logo.png';
let url = '';
const media = await MessageMedia.fromUrl(url);
media.mimetype = 'image/png';
media.filename = 'hello.png';
let btnClickableMenu = new Buttons(media as any, [{ id: 'customId', body: 'button1' }, { body: 'button2' }]);
await this.sendToDevice("5521996049063",btnClickableMenu as any,null)
await this.sendToDevice('', btnClickableMenu as any, null);
}
public async setup(setUrl: boolean) {
this.directLineClient = new Swagger({
const client = await new SwaggerClient({
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
usePromise: true
requestInterceptor: req => {
req.headers['Authorization'] = `Bearer ${this.min.instance.webchatKey}`;
}
});
const client = await this.directLineClient;
this.directLineClient = client;

let url: string;
let body: any;

/*client.clientAuthorizations.add(
'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
);*/
let options: any;
const phoneId = this.whatsappServiceNumber.split(';')[0];

switch (this.provider) {
case 'GeneralBots':
Expand All @@ -149,13 +147,9 @@ 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 createClient = async browserWSEndpoint => {
let puppeteer = { headless: false, args: ['--no-sandbox', '--disable-dev-shm-usage'] };
if (browserWSEndpoint) {
// puppeteer.browserWSEndpoint = browserWSEndpoint ;
}
const createClient = () => {
const client = (this.customClient = new Client({
puppeteer: puppeteer
puppeteer: GBSSR.preparePuppeteer(localName)
}));
client.on(
'message',
Expand Down Expand Up @@ -189,11 +183,11 @@ 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 () => {
GBLog.verbose(`GBWhatsApp: Emptying chat list for ${this.botId}...`);

// Keeps the chat list cleaned.
const chats = await client.getChats();
await CollectionUtil.asyncForEach(chats, async chat => {
Expand All @@ -213,7 +207,7 @@ export class WhatsappDirectLine extends GBService {
});
client.initialize();
};
await createClient.bind(this)(this.browserWSEndpoint);
createClient.bind(this)();
setUrl = false;
}
break;
Expand Down Expand Up @@ -250,6 +244,7 @@ export class WhatsappDirectLine extends GBService {

break;
case 'maytapi':
const phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1];
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
body = {
Expand Down Expand Up @@ -579,7 +574,7 @@ export class WhatsappDirectLine extends GBService {
} else if (user.agentMode === 'bot' || user.agentMode === null || user.agentMode === undefined) {
if (WhatsappDirectLine.conversationIds[botId + from + group] === undefined) {
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
const response = await client.Conversations.Conversations_StartConversation();
const response = await client.apis.Conversations.Conversations_StartConversation();
const generatedConversationId = response.obj.conversationId;

WhatsappDirectLine.conversationIds[botId + from + group] = generatedConversationId;
Expand Down Expand Up @@ -627,7 +622,7 @@ export class WhatsappDirectLine extends GBService {
}

public inputMessage(client, conversationId: string, text: string, from, fromName: string, group, attachments: File) {
return client.Conversations.Conversations_PostActivity({
return client.apis.Conversations.Conversations_PostActivity({
conversationId: conversationId,
activity: {
textFormat: 'plain',
Expand All @@ -652,7 +647,7 @@ export class WhatsappDirectLine extends GBService {

const worker = async () => {
try {
const response = await client.Conversations.Conversations_GetActivities({
const response = await client.apis.Conversations.Conversations_GetActivities({
conversationId: conversationId,
watermark: watermark
});
Expand Down

0 comments on commit b96a78e

Please sign in to comment.