fix(whatsapp.gblib): #288 unify puppteer params with GBSSR.
This commit is contained in:
parent
0d5931dde1
commit
b96a78e810
3 changed files with 30 additions and 29 deletions
|
@ -69,7 +69,7 @@
|
||||||
"@nlpjs/basic": "4.26.1",
|
"@nlpjs/basic": "4.26.1",
|
||||||
"@nosferatu500/textract": "3.1.2",
|
"@nosferatu500/textract": "3.1.2",
|
||||||
"@push-rpc/core": "1.1.5",
|
"@push-rpc/core": "1.1.5",
|
||||||
"@push-rpc/http": "1.1.5",
|
"@push-rpc/http": "^1.1.5",
|
||||||
"@push-rpc/websocket": "1.1.5",
|
"@push-rpc/websocket": "1.1.5",
|
||||||
"@semantic-release/changelog": "5.0.1",
|
"@semantic-release/changelog": "5.0.1",
|
||||||
"@semantic-release/exec": "5.0.0",
|
"@semantic-release/exec": "5.0.0",
|
||||||
|
@ -143,8 +143,8 @@
|
||||||
"puppeteer": "19.6.3",
|
"puppeteer": "19.6.3",
|
||||||
"puppeteer-extra": "3.3.4",
|
"puppeteer-extra": "3.3.4",
|
||||||
"puppeteer-extra-plugin-stealth": "2.11.1",
|
"puppeteer-extra-plugin-stealth": "2.11.1",
|
||||||
"qrcode": "1.5.1",
|
|
||||||
"qr-scanner": "^1.4.2",
|
"qr-scanner": "^1.4.2",
|
||||||
|
"qrcode": "1.5.1",
|
||||||
"qrcode-terminal": "0.12.0",
|
"qrcode-terminal": "0.12.0",
|
||||||
"readline": "1.3.0",
|
"readline": "1.3.0",
|
||||||
"reflect-metadata": "0.1.13",
|
"reflect-metadata": "0.1.13",
|
||||||
|
|
|
@ -91,7 +91,7 @@ export class GBSSR {
|
||||||
'tiqcdn'
|
'tiqcdn'
|
||||||
];
|
];
|
||||||
|
|
||||||
public static async createBrowser(profilePath): Promise<any> {
|
public static preparePuppeteer(profilePath){
|
||||||
let args = [
|
let args = [
|
||||||
'--check-for-update-interval=2592000',
|
'--check-for-update-interval=2592000',
|
||||||
'--disable-accelerated-2d-canvas',
|
'--disable-accelerated-2d-canvas',
|
||||||
|
@ -113,15 +113,21 @@ export class GBSSR {
|
||||||
Fs.writeFileSync(preferences, JSON.stringify(data));
|
Fs.writeFileSync(preferences, JSON.stringify(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
puppeteer.use(hidden());
|
|
||||||
const browser = await puppeteer.launch({
|
return {
|
||||||
args: args,
|
args: args,
|
||||||
ignoreHTTPSErrors: true,
|
ignoreHTTPSErrors: true,
|
||||||
headless: false,
|
headless: false,
|
||||||
defaultViewport: null,
|
defaultViewport: null,
|
||||||
executablePath: executablePath(),
|
executablePath: executablePath(),
|
||||||
ignoreDefaultArgs: ['--enable-automation', '--enable-blink-features=IdleDetection']
|
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;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
\*****************************************************************************/
|
\*****************************************************************************/
|
||||||
|
|
||||||
import urlJoin from 'url-join';
|
import urlJoin from 'url-join';
|
||||||
import Swagger from 'swagger-client';
|
import SwaggerClient from 'swagger-client';
|
||||||
import Path from 'path';
|
import Path from 'path';
|
||||||
import Fs from 'fs';
|
import Fs from 'fs';
|
||||||
import { GBError, GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib';
|
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 { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
|
||||||
import { method } from 'lodash';
|
import { method } from 'lodash';
|
||||||
import pkg from 'whatsapp-web.js';
|
import pkg from 'whatsapp-web.js';
|
||||||
|
import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
|
||||||
const { Buttons, Client, MessageMedia } = pkg;
|
const { Buttons, Client, MessageMedia } = pkg;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,28 +117,25 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendButton() {
|
public async sendButton() {
|
||||||
let url = 'https://wwebjs.dev/logo.png';
|
let url = '';
|
||||||
const media = await MessageMedia.fromUrl(url);
|
const media = await MessageMedia.fromUrl(url);
|
||||||
media.mimetype = 'image/png';
|
media.mimetype = 'image/png';
|
||||||
media.filename = 'hello.png';
|
media.filename = 'hello.png';
|
||||||
let btnClickableMenu = new Buttons(media as any, [{ id: 'customId', body: 'button1' }, { body: 'button2' }]);
|
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) {
|
public async setup(setUrl: boolean) {
|
||||||
this.directLineClient = new Swagger({
|
const client = await new SwaggerClient({
|
||||||
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
|
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 url: string;
|
||||||
let body: any;
|
let body: any;
|
||||||
|
|
||||||
/*client.clientAuthorizations.add(
|
|
||||||
'AuthorizationBotConnector',
|
|
||||||
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
|
|
||||||
);*/
|
|
||||||
let options: any;
|
let options: any;
|
||||||
const phoneId = this.whatsappServiceNumber.split(';')[0];
|
|
||||||
|
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
|
@ -149,13 +147,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 localName = Path.join('work', gbaiName, 'profile');
|
const localName = Path.join('work', gbaiName, 'profile');
|
||||||
const createClient = async browserWSEndpoint => {
|
const createClient = () => {
|
||||||
let puppeteer = { headless: false, args: ['--no-sandbox', '--disable-dev-shm-usage'] };
|
|
||||||
if (browserWSEndpoint) {
|
|
||||||
// puppeteer.browserWSEndpoint = browserWSEndpoint ;
|
|
||||||
}
|
|
||||||
const client = (this.customClient = new Client({
|
const client = (this.customClient = new Client({
|
||||||
puppeteer: puppeteer
|
puppeteer: GBSSR.preparePuppeteer(localName)
|
||||||
}));
|
}));
|
||||||
client.on(
|
client.on(
|
||||||
'message',
|
'message',
|
||||||
|
@ -189,11 +183,11 @@ 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 () => {
|
||||||
GBLog.verbose(`GBWhatsApp: Emptying chat list for ${this.botId}...`);
|
GBLog.verbose(`GBWhatsApp: Emptying chat list for ${this.botId}...`);
|
||||||
|
|
||||||
// Keeps the chat list cleaned.
|
// Keeps the chat list cleaned.
|
||||||
const chats = await client.getChats();
|
const chats = await client.getChats();
|
||||||
await CollectionUtil.asyncForEach(chats, async chat => {
|
await CollectionUtil.asyncForEach(chats, async chat => {
|
||||||
|
@ -213,7 +207,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
});
|
});
|
||||||
client.initialize();
|
client.initialize();
|
||||||
};
|
};
|
||||||
await createClient.bind(this)(this.browserWSEndpoint);
|
createClient.bind(this)();
|
||||||
setUrl = false;
|
setUrl = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -250,6 +244,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'maytapi':
|
case 'maytapi':
|
||||||
|
const phoneId = this.whatsappServiceNumber.split(';')[0];
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
let productId = this.whatsappServiceNumber.split(';')[1];
|
||||||
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
||||||
body = {
|
body = {
|
||||||
|
@ -579,7 +574,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
} else if (user.agentMode === 'bot' || user.agentMode === null || user.agentMode === undefined) {
|
} else if (user.agentMode === 'bot' || user.agentMode === null || user.agentMode === undefined) {
|
||||||
if (WhatsappDirectLine.conversationIds[botId + from + group] === undefined) {
|
if (WhatsappDirectLine.conversationIds[botId + from + group] === undefined) {
|
||||||
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
|
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;
|
const generatedConversationId = response.obj.conversationId;
|
||||||
|
|
||||||
WhatsappDirectLine.conversationIds[botId + from + group] = generatedConversationId;
|
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) {
|
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,
|
conversationId: conversationId,
|
||||||
activity: {
|
activity: {
|
||||||
textFormat: 'plain',
|
textFormat: 'plain',
|
||||||
|
@ -652,7 +647,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
const worker = async () => {
|
const worker = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await client.Conversations.Conversations_GetActivities({
|
const response = await client.apis.Conversations.Conversations_GetActivities({
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
watermark: watermark
|
watermark: watermark
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue