diff --git a/package.json b/package.json index 6c3bf73e2..b9d2aec91 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/packages/core.gbapp/services/GBSSR.ts b/packages/core.gbapp/services/GBSSR.ts index 275c11654..bc0191f21 100644 --- a/packages/core.gbapp/services/GBSSR.ts +++ b/packages/core.gbapp/services/GBSSR.ts @@ -91,7 +91,7 @@ export class GBSSR { 'tiqcdn' ]; - public static async createBrowser(profilePath): Promise { + public static preparePuppeteer(profilePath){ let args = [ '--check-for-update-interval=2592000', '--disable-accelerated-2d-canvas', @@ -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 { + const opts = this.preparePuppeteer(profilePath); + puppeteer.use(hidden()); + const browser = await puppeteer.launch( opts ); return browser; } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 4eebcb381..8119ab283 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -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'; @@ -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; /** @@ -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': @@ -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', @@ -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 => { @@ -213,7 +207,7 @@ export class WhatsappDirectLine extends GBService { }); client.initialize(); }; - await createClient.bind(this)(this.browserWSEndpoint); + createClient.bind(this)(); setUrl = false; } break; @@ -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 = { @@ -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; @@ -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', @@ -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 });