From 2eea15438e2c3a582b7c83a387a03c7979489262 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Wed, 13 Jul 2022 09:38:13 -0300 Subject: [PATCH] new(whatsapp.gblib): General Bots WhatsApp provider. --- .../basic.gblib/services/DialogKeywords.ts | 11 +++++++ packages/basic.gblib/services/GBVMService.ts | 8 +++++ .../basic.gblib/services/SystemKeywords.ts | 12 +++++++- packages/kb.gbapp/services/KBService.ts | 7 ++++- .../services/WhatsappDirectLine.ts | 30 ++++++------------- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index b144203c..0b65bc18 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -634,6 +634,17 @@ export class DialogKeywords { this.user = user; } + /** + * Defines the id generation policy. + * + * @example SET ID NUMBER + * + */ + public async setIdGeneration(mode) { + this['idGeneration'] = mode; + this['id'] = await this.sys().getRandomId(); + } + /** * Defines the maximum lines to scan in spreedsheets. * diff --git a/packages/basic.gblib/services/GBVMService.ts b/packages/basic.gblib/services/GBVMService.ts index 9a3548a3..d82897c3 100644 --- a/packages/basic.gblib/services/GBVMService.ts +++ b/packages/basic.gblib/services/GBVMService.ts @@ -398,6 +398,14 @@ export class GBVMService extends GBService { return `${$1} = sys().download (${$2}, ${$3})`; }); + code = code.replace(/(\w+)\s*\=\s*CREATE FOLDER\s*(.*)/gi, ($0, $1, $2) => { + return `${$1} = sys().createFolder (${$2})`; + }); + + code = code.replace(/SHARE FOLDER\s*(.*)/gi, ($0, $1) => { + return `sys().shareFolder (${$1})`; + }); + code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => { return `sys().createABotFarmUsing (${$3})`; }); diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index fd14f222..812d5c30 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -382,7 +382,17 @@ export class SystemKeywords { * Retrives a random id with a length of five, every time it is called. */ public async getRandomId() { - return GBAdminService.getRndReadableIdentifier().substr(5); + + const idGeneration = this.dk['idGeneration']; + if (idGeneration.toLowerCase() === 'number') + { + return GBAdminService.getNumberIdentifier(); + } + else + { + return GBAdminService.getRndReadableIdentifier().substr(5); + } + } /** diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 22f8de16..aab6a967 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -467,10 +467,15 @@ export class KBService implements IGBKBService { GBLog.info(`[GBImporter] Answer is NULL related to Question '${question}'.`); answer = 'Existe um problema na base de conhecimento. Fui treinado para entender sua pergunta, avise a quem me criou que a resposta não foi informada para esta pergunta.'; - } else if (answer.indexOf('.md') > -1) { + } else if (answer.indexOf('.md') > -1 || answer.indexOf('.docx') > -1) { + const mediaFilename = urlJoin(path.dirname(filePath), '..', 'articles', answer); if (Fs.existsSync(mediaFilename)) { + + // TODO: Convert DOCX to MD. + answer = Fs.readFileSync(mediaFilename, 'utf8'); + format = '.md'; media = path.basename(mediaFilename); } else { diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 91ea3363..09428268 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -34,6 +34,7 @@ const urlJoin = require('url-join'); const Swagger = require('swagger-client'); const fs = require('fs'); +const Path = require('path'); import { GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib'; import { CollectionUtil } from 'pragmatismo-io-framework'; import * as request from 'request-promise-native'; @@ -42,8 +43,6 @@ import { GBConversationalService } from '../../core.gbapp/services/GBConversatio import { SecService } from '../../security.gbapp/services/SecService'; import { Messages } from '../strings'; import { GuaribasUser } from '../../security.gbapp/models'; -import { GBConfigService } from '../../core.gbapp/services/GBConfigService'; -import { GBAdminService } from '../../admin.gbapp/services/GBAdminService'; import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords'; const { MessageMedia, Client, LocalAuth } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); @@ -106,8 +105,7 @@ export class WhatsappDirectLine extends GBService { this.directLineClient = new Swagger({ - spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), - usePromise: true + spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), usePromise: true }); const client = await this.directLineClient; @@ -120,11 +118,10 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': - const Path = require('path'); + // Initialize the browser using a local profile for each bot. const gbaiName = `${this.min.botId}.gbai`; let localName = Path.join('work', gbaiName, 'profile'); - let client = this.customClient = new Client({ authStrategy: new LocalAuth(), puppeteer: { @@ -132,9 +129,10 @@ export class WhatsappDirectLine extends GBService { `--user-data-dir=${localName}`] } }); - client.initialize(); + // Dispatches messages to the received method. + client.on('message', (async message => { await this.received(message, null); }).bind(this)); @@ -153,7 +151,7 @@ export class WhatsappDirectLine extends GBService { // While handling other bots uses boot instance of this class to send QR Codes. - + if (this.botId !== GBServer.globals.minBoot.botId) { GBServer.globals.minBoot.whatsAppDirectLine.sendMessage(msg); GBServer.globals.minBoot.whatsAppDirectLine.sendMessage(adminNumber, qr); @@ -192,7 +190,6 @@ export class WhatsappDirectLine extends GBService { let phoneId = this.whatsappServiceNumber.split(';')[0]; let productId = this.whatsappServiceNumber.split(';')[1] - let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`; WhatsappDirectLine.phones[phoneId] = this.botId; @@ -222,9 +219,7 @@ export class WhatsappDirectLine extends GBService { } catch (error) { GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`); } - } - } public async resetConversationId(botId, number, group = '') { @@ -237,15 +232,12 @@ export class WhatsappDirectLine extends GBService { const options = { url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`, - method: 'GET' - - }; + method: 'GET'}; const res = await request(options); const json = JSON.parse(res); return json.accountStatus === 'authenticated'; - } public async received(req, res) { @@ -371,8 +363,8 @@ export class WhatsappDirectLine extends GBService { await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => { await e.onExchangeData(this.min, 'whatsappMessage', message); }); - const sec = new SecService(); + const sec = new SecService(); const user = await sec.ensureUser(this.min.instance.instanceId, from, fromName, '', 'whatsapp', fromName, null); const locale = user.locale ? user.locale : 'pt'; @@ -404,10 +396,9 @@ export class WhatsappDirectLine extends GBService { `No momento estou apenas conseguindo ler mensagens de texto.`, null); } } + const conversationId = WhatsappDirectLine.conversationIds[botId + from + group]; - const client = await this.directLineClient; - WhatsappDirectLine.lastMessage[botId + from] = message; // Check if this message is from a Human Agent itself. @@ -762,11 +753,8 @@ export class WhatsappDirectLine extends GBService { case 'maytapi': let phoneId = this.whatsappServiceNumber.split(';')[0]; let productId = this.whatsappServiceNumber.split(';')[1] - - let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`; - options = { url: url, method: 'post',