Merge branch 'master' of https://github.com/GeneralBots/BotServer
This commit is contained in:
commit
6aa86a17c2
5 changed files with 44 additions and 22 deletions
|
@ -634,6 +634,17 @@ export class DialogKeywords {
|
||||||
this.user = user;
|
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.
|
* Defines the maximum lines to scan in spreedsheets.
|
||||||
*
|
*
|
||||||
|
|
|
@ -398,6 +398,14 @@ export class GBVMService extends GBService {
|
||||||
return `${$1} = sys().download (${$2}, ${$3})`;
|
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) => {
|
code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
return `sys().createABotFarmUsing (${$3})`;
|
return `sys().createABotFarmUsing (${$3})`;
|
||||||
});
|
});
|
||||||
|
|
|
@ -382,7 +382,17 @@ export class SystemKeywords {
|
||||||
* Retrives a random id with a length of five, every time it is called.
|
* Retrives a random id with a length of five, every time it is called.
|
||||||
*/
|
*/
|
||||||
public async getRandomId() {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -467,10 +467,15 @@ export class KBService implements IGBKBService {
|
||||||
GBLog.info(`[GBImporter] Answer is NULL related to Question '${question}'.`);
|
GBLog.info(`[GBImporter] Answer is NULL related to Question '${question}'.`);
|
||||||
answer =
|
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.';
|
'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);
|
const mediaFilename = urlJoin(path.dirname(filePath), '..', 'articles', answer);
|
||||||
if (Fs.existsSync(mediaFilename)) {
|
if (Fs.existsSync(mediaFilename)) {
|
||||||
|
|
||||||
|
// TODO: Convert DOCX to MD.
|
||||||
|
|
||||||
answer = Fs.readFileSync(mediaFilename, 'utf8');
|
answer = Fs.readFileSync(mediaFilename, 'utf8');
|
||||||
|
|
||||||
format = '.md';
|
format = '.md';
|
||||||
media = path.basename(mediaFilename);
|
media = path.basename(mediaFilename);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -34,6 +34,7 @@ const urlJoin = require('url-join');
|
||||||
|
|
||||||
const Swagger = require('swagger-client');
|
const Swagger = require('swagger-client');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const Path = require('path');
|
||||||
import { GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib';
|
import { GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib';
|
||||||
import { CollectionUtil } from 'pragmatismo-io-framework';
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
||||||
import * as request from 'request-promise-native';
|
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 { SecService } from '../../security.gbapp/services/SecService';
|
||||||
import { Messages } from '../strings';
|
import { Messages } from '../strings';
|
||||||
import { GuaribasUser } from '../../security.gbapp/models';
|
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';
|
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords';
|
||||||
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');
|
||||||
|
@ -106,8 +105,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
this.directLineClient =
|
this.directLineClient =
|
||||||
new Swagger({
|
new Swagger({
|
||||||
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')),
|
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), usePromise: true
|
||||||
usePromise: true
|
|
||||||
});
|
});
|
||||||
const client = await this.directLineClient;
|
const client = await this.directLineClient;
|
||||||
|
|
||||||
|
@ -120,11 +118,10 @@ export class WhatsappDirectLine extends GBService {
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
|
|
||||||
const Path = require('path');
|
// Initialize the browser using a local profile for each bot.
|
||||||
|
|
||||||
const gbaiName = `${this.min.botId}.gbai`;
|
const gbaiName = `${this.min.botId}.gbai`;
|
||||||
let localName = Path.join('work', gbaiName, 'profile');
|
let localName = Path.join('work', gbaiName, 'profile');
|
||||||
|
|
||||||
let client = this.customClient = new Client({
|
let client = this.customClient = new Client({
|
||||||
authStrategy: new LocalAuth(),
|
authStrategy: new LocalAuth(),
|
||||||
puppeteer: {
|
puppeteer: {
|
||||||
|
@ -132,9 +129,10 @@ export class WhatsappDirectLine extends GBService {
|
||||||
`--user-data-dir=${localName}`]
|
`--user-data-dir=${localName}`]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.initialize();
|
client.initialize();
|
||||||
|
|
||||||
|
// Dispatches messages to the received method.
|
||||||
|
|
||||||
client.on('message', (async message => {
|
client.on('message', (async message => {
|
||||||
await this.received(message, null);
|
await this.received(message, null);
|
||||||
}).bind(this));
|
}).bind(this));
|
||||||
|
@ -200,7 +198,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1]
|
let productId = this.whatsappServiceNumber.split(';')[1]
|
||||||
|
|
||||||
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
||||||
WhatsappDirectLine.phones[phoneId] = this.botId;
|
WhatsappDirectLine.phones[phoneId] = this.botId;
|
||||||
|
|
||||||
|
@ -230,9 +227,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async resetConversationId(botId, number, group = '') {
|
public async resetConversationId(botId, number, group = '') {
|
||||||
|
@ -245,15 +240,12 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`,
|
url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`,
|
||||||
method: 'GET'
|
method: 'GET'};
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
const res = await request(options);
|
const res = await request(options);
|
||||||
const json = JSON.parse(res);
|
const json = JSON.parse(res);
|
||||||
|
|
||||||
return json.accountStatus === 'authenticated';
|
return json.accountStatus === 'authenticated';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async received(req, res) {
|
public async received(req, res) {
|
||||||
|
@ -383,8 +375,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
|
await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
|
||||||
await e.onExchangeData(this.min, 'whatsappMessage', message);
|
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,
|
const user = await sec.ensureUser(this.min.instance.instanceId, from,
|
||||||
fromName, '', 'whatsapp', fromName, null);
|
fromName, '', 'whatsapp', fromName, null);
|
||||||
const locale = user.locale ? user.locale : 'pt';
|
const locale = user.locale ? user.locale : 'pt';
|
||||||
|
@ -416,10 +408,9 @@ export class WhatsappDirectLine extends GBService {
|
||||||
`No momento estou apenas conseguindo ler mensagens de texto.`, null);
|
`No momento estou apenas conseguindo ler mensagens de texto.`, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const conversationId = WhatsappDirectLine.conversationIds[botId + from + group];
|
const conversationId = WhatsappDirectLine.conversationIds[botId + from + group];
|
||||||
|
|
||||||
const client = await this.directLineClient;
|
const client = await this.directLineClient;
|
||||||
|
|
||||||
WhatsappDirectLine.lastMessage[botId + from] = message;
|
WhatsappDirectLine.lastMessage[botId + from] = message;
|
||||||
|
|
||||||
// Check if this message is from a Human Agent itself.
|
// Check if this message is from a Human Agent itself.
|
||||||
|
@ -777,11 +768,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
case 'maytapi':
|
case 'maytapi':
|
||||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1]
|
let productId = this.whatsappServiceNumber.split(';')[1]
|
||||||
|
|
||||||
|
|
||||||
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
|
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
|
||||||
|
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
url: url,
|
url: url,
|
||||||
method: 'post',
|
method: 'post',
|
||||||
|
|
Loading…
Add table
Reference in a new issue