new(all): Vm isolated working with IPC BASIC 3.0;

This commit is contained in:
rodrigorodriguez 2022-11-05 17:59:41 -03:00
parent 5076e254fd
commit fa0324dc06
4 changed files with 190 additions and 159 deletions

View file

@ -80,10 +80,12 @@ export class GBBasicPackage implements IGBPackage {
public async loadBot(min: GBMinInstance): Promise<void> {
const dk = new DialogKeywords(min, null, null);
const wa = new WebAutomationKeywords(min, null, dk);
const sys = new SystemKeywords(min, null, dk, wa)
dk.wa = wa;
wa.sys = sys;
const dialogRouter = createServerRouter(`/api/v2/${min.botId}/dialog`, dk);
const waRouter = createServerRouter(`/api/v2/${min.botId}/webautomation`, wa );
const sysRouter = createServerRouter(`/api/v2/${min.botId}/system`, new SystemKeywords(min, null, dk, wa));
const sysRouter = createServerRouter(`/api/v2/${min.botId}/system`, sys);
app.use(dialogRouter.routes());
app.use(sysRouter.routes());
app.use(waRouter.routes());

View file

@ -334,14 +334,14 @@ export class GBVMService extends GBService {
const getParams = async (text, names) => {
let ret = {};
const items = text.split(','); // TODO: NOT IN STRING.
let ret = {};
const items = text.split(','); // TODO: NOT IN STRING.
await CollectionUtil.asyncForEach(names, async name => {
ret[name] = items[0];
});
await CollectionUtil.asyncForEach(names, async name => {
ret[name] = items[0];
});
return JSON.stringify(ret);
return JSON.stringify(ret);
};
// Keywords from General Bots BASIC.
@ -440,7 +440,7 @@ export class GBVMService extends GBService {
code = code.replace(/(\w)\s*\=\s*create deal(\s)(.*)/gi, ($0, $1, $2, $3) => {
const params = getParams($3, ['dealName', 'contact', 'company', 'amount']);
return `${$1} = await dk.createDeal(${params})\n`;
});
@ -509,8 +509,8 @@ export class GBVMService extends GBService {
code = code.replace(/(go to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
// TODO: fromOrDialogName, dialogName
return `await dk.gotoDialog({${$3}})\n`;
const params = getParams($3, ['fromOrDialogName', 'dialogName']);
return `await dk.gotoDialog(${params})\n`;
});
code = code.replace(/(set language)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
@ -530,51 +530,53 @@ export class GBVMService extends GBService {
});
code = code.replace(/(datediff)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.dateDiff ({${$3}})\n`;
const params = getParams($3, ['date1', 'date2', 'mode']);
return `await dk.dateDiff (${params}})\n`;
});
code = code.replace(/(dateadd)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.dateAdd ({${$3}})\n`;
const params = getParams($3, ['date', 'mode', 'units']);
return `await dk.dateAdd (${$3})\n`;
});
code = code.replace(/(set max lines)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.setMaxLines ({${$3}})\n`;
return `await dk.setMaxLines ({count: ${$3}})\n`;
});
code = code.replace(/(set max columns)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.setMaxColumns ({${$3}})\n`;
return `await dk.setMaxColumns ({count: ${$3}})\n`;
});
code = code.replace(/(set translator)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.setTranslatorOn ({"${$3.toLowerCase()}"})\n`;
return `await dk.setTranslatorOn ({on: "${$3.toLowerCase()}"})\n`;
});
code = code.replace(/(set theme)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.setTheme ({"${$3.toLowerCase()}"})\n`;
return `await dk.setTheme ({theme: "${$3.toLowerCase()}"})\n`;
});
code = code.replace(/(set whole word)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.setWholeWord ({"${$3.toLowerCase()}"})\n`;
return `await dk.setWholeWord ({on: "${$3.toLowerCase()}"})\n`;
});
code = code.replace(/(\w+)\s*\=\s*post\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
return `${$1} = await sys.postByHttp ({${$2}, ${$3}, headers})`;
return `${$1} = await sys.postByHttp ({url:${$2}, data:${$3}, headers})`;
});
code = code.replace(/(\w+)\s*\=\s*put\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
return `${$1} = await sys.putByHttp ({${$2}, ${$3}, headers})`;
return `${$1} = await sys.putByHttp ({url:${$2}, data:${$3}, headers})`;
});
code = code.replace(/(\w+)\s*\=\s*download\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
return `${$1} = await sys.download ({${$2}, ${$3}})`;
return `${$1} = await sys.download ({handle:page, selector: ${$2}, folder:${$3}})`;
});
code = code.replace(/(\w+)\s*\=\s*CREATE FOLDER\s*(.*)/gi, ($0, $1, $2) => {
return `${$1} = await sys.createFolder ({${$2}})`;
return `${$1} = await sys.createFolder ({name:${$2}})`;
});
code = code.replace(/SHARE FOLDER\s*(.*)/gi, ($0, $1) => {
return `await sys.shareFolder ({${$1}})`;
return `await sys.shareFolder ({name: ${$1}})`;
});
code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => {
@ -582,15 +584,17 @@ export class GBVMService extends GBService {
});
code = code.replace(/(chart)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.chart ({${$3}})\n`;
const params = getParams($3, ['type', 'data', 'legends', 'transpose']);
return `await dk.chart (${params})\n`;
});
code = code.replace(/(transfer to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.transferTo ({${$3}})\n`;
return `await dk.transferTo ({to:${$3}})\n`;
});
code = code.replace(/(\btransfer\b)(?=(?:[^"]|"[^"]*")*$)/gi, () => {
return `await dk.transferTo ()\n`;
return `await dk.transferTo ({})\n`;
});
code = code.replace(/(exit)/gi, () => {
@ -598,55 +602,69 @@ export class GBVMService extends GBService {
});
code = code.replace(/(show menu)/gi, () => {
return `await dk.showMenu ()\n`;
return `await dk.showMenu ({})\n`;
});
code = code.replace(/(talk to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.talkTo({${$3}})\n`;
const params = getParams($3, ['mobile', 'message']);
return `await sys.talkTo(${params})\n`;
});
code = code.replace(/(talk)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.talk ({${$3}})\n`;
if ($3.substr(0, 1) !== "\"") {
$3 = `"${$3}"`;
}
return `await dk.talk ({text: ${$3}})\n`;
});
code = code.replace(/(send sms to)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.sendSmsTo ({${$3}})\n`;
const params = getParams($3, ['mobile', 'message']);
return `await sys.sendSmsTo(${params})\n`;
});
code = code.replace(/(send email)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.sendEmail ({${$3}})\n`;
const params = getParams($3, ['to', 'subject', 'body']);
return `await dk.sendEmail(${params})\n`;
});
code = code.replace(/(send mail)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.sendEmail ({${$3}})\n`;
const params = getParams($3, ['to', 'subject', 'body']);
return `await dk.sendEmail(${params})\n`;
});
code = code.replace(/(send file to)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.sendFileTo ({${$3}})\n`;
const params = getParams($3, ['mobile', 'filename', 'caption']);
return `await dk.sendFileTo(${params})\n`;
});
code = code.replace(/(hover)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await wa.hover ({page, ${$3}})\n`;
const params = getParams($3, ['handle', 'selector']);
return `await wa.hover (${params})\n`;
});
code = code.replace(/(click link text)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await wa.linkByText ({page, ${$3}})\n`;
const params = getParams('page,' + $3, ['handle', 'text', 'index']);
return `await wa.linkByText (${params})\n`;
});
code = code.replace(/(click)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await wa.click (page, {${$3}})\n`;
const params = getParams('page,' + $3, ['handle', 'frameOrSelector', 'selector']);
return `await wa.click (${params})\n`;
});
code = code.replace(/(send file)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await dk.sendFile ({${$3}})\n`;
const params = getParams($3, ['filename', 'caption']);
return `await dk.sendFile(${params})\n`;
});
code = code.replace(/(copy)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.copyFile({${$3}})\n`;
const params = getParams($3, ['src', 'dst']);
return `await sys.copyFile (${params})\n`;
});
code = code.replace(/(convert)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.convert({${$3}})\n`;
const params = getParams($3, ['src', 'dst']);
return `await sys.convert (${params})\n`;
});
// TODO: AS CHART.
@ -655,42 +673,43 @@ export class GBVMService extends GBService {
// });
code = code.replace(/MERGE\s(.*)\sWITH\s(.*)BY\s(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.merge({${$1}, ${$2}, ${$3}})\n`;
return `await sys.merge({file: ${$1}, data: ${$2}, key1: ${$3}})\n`;
});
code = code.replace(/PRESS\s(.*)\sON\s(.*)/gi, ($0, $1, $2) => {
return `await wa.pressKey({page, ${$2}, ${$1})\n`;
code = code.replace(/PRESS\s(.*)/gi, ($0, $1, $2) => {
return `await wa.pressKey({handle: page, char: ${$1})\n`;
});
code = code.replace(/SCREENSHOT\s(.*)/gi, ($0, $1, $2) => {
return `await wa.screenshot({page, ${$1})\n`;
return `await wa.screenshot({handle: page, selector: ${$1}})\n`;
});
code = code.replace(/TWEET\s(.*)/gi, ($0, $1, $2) => {
return `await sys.tweet({${$1})\n`;
return `await sys.tweet({text: ${$1})\n`;
});
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as image/gi, ($0, $1, $2) => {
return `${$1} = await sys.asImage({${$2})\n`;
return `${$1} = await sys.asImage({data: ${$2}})\n`;
});
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as pdf/gi, ($0, $1, $2) => {
return `${$1} = await sys.asPdf({${$2})\n`;
return `${$1} = await sys.asPdf({data: ${$2})\n`;
});
code = code.replace(/(\w+)\s*\=\s*FILL\s(.*)\sWITH\s(.*)/gi, ($0, $1, $2, $3) => {
return `${1} = await sys.fill({${$2}, ${$3}})\n`;
return `${1} = await sys.fill({templateName: ${$2}, data: ${$3}})\n`;
});
code = code.replace(/save\s(.*)\sas\s(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.saveFile({${$2}, ${$1})\n`;
return `await sys.saveFile({file: ${$2}, data: ${$1})\n`;
});
code = code.replace(/(save)(\s)(.*)/gi, ($0, $1, $2, $3) => {
return `await sys.save({[${$3}]})\n`;
});
code = code.replace(/set\s(.*)/gi, ($0, $1, $2) => {
return `await sys.set ({${$1})`;
const params = getParams($1, ['file', 'address', 'value']);
return `await sys.set (${params})`;
});
code = `${code}\n%>`;

View file

@ -38,14 +38,13 @@ import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
import { DialogKeywords } from './DialogKeywords';
import { GBServer } from '../../../src/app';
import * as fs from 'fs';
import { GBVMService } from './GBVMService';
import { ThisPath } from 'botbuilder-dialogs';
const Fs = require('fs');
const Excel = require('exceljs');
import { createBrowser } from '../../core.gbapp/services/GBSSR';
const urlJoin = require('url-join');
const url = require('url');
const { TwitterApi } = require('twitter-api-v2');
@ -1059,96 +1058,6 @@ export class SystemKeywords {
return ret;
}
/**
* Performs the download to the .gbdrive Download folder.
*
* @example file = DOWNLOAD element, folder
*/
public async download({element, folder}) {
const page = element['_page'];
const container = element['_frame'] ? element['_frame'] : element['_page'];
await page.setRequestInterception(true);
await container.click(element.originalSelector);
const xRequest = await new Promise(resolve => {
page.on('request', interceptedRequest => {
interceptedRequest.abort(); //stop intercepting requests
resolve(interceptedRequest);
});
});
const options = {
encoding: null,
method: xRequest['._method'],
uri: xRequest['_url'],
body: xRequest['_postData'],
headers: xRequest['_headers']
}
const cookies = await page.cookies();
options.headers.Cookie = cookies.map(ck => ck.name + '=' + ck.value).join(';');
GBLog.info(`BASIC: DOWNLOADING '${options.uri}...'`);
let local;
let filename;
if (options.uri.indexOf('file://') != -1) {
local = url.fileURLToPath(options.uri);
filename = Path.basename(local);
}
else {
const getBasenameFormUrl = (urlStr) => {
const url = new URL(urlStr)
return Path.basename(url.pathname)
};
filename = getBasenameFormUrl(options.uri);
}
let result: Buffer;
if (local) {
result = fs.readFileSync(local);
} else {
result = await request.get(options);
}
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
const botId = this.min.instance.botId;
// Normalizes all slashes.
folder = folder.replace(/\\/gi, '/');
// Determines full path at source and destination.
const root = urlJoin(`/${botId}.gbai/${botId}.gbdrive`);
const dstPath = urlJoin(root, folder, filename);
// Checks if the destination contains subfolders that
// need to be created.
folder = await this.createFolder(folder);
// Performs the conversion operation getting a reference
// to the source and calling /content on drive API.
let file;
try {
file = await client
.api(`${baseUrl}/drive/root:/${dstPath}:/content`)
.put(result);
} catch (error) {
if (error.code === "nameAlreadyExists") {
GBLog.info(`BASIC: DOWNLOAD destination file already exists: ${dstPath}.`);
}
throw error;
}
return file;
}
/**
* Creates a folder in the bot instance drive.
*
@ -1213,7 +1122,11 @@ export class SystemKeywords {
*
*/
public async shareFolder({folderReference, email, message}) {
let [, client] = await GBDeployer.internalGetDriveClient(this.min);
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
const srcFile = await client.api(
`${baseUrl}/drive/root:/${folder}`)
.get();
const driveId = folderReference.parentReference.driveId;
const itemId = folderReference.id;
const body = {
@ -1490,7 +1403,7 @@ export class SystemKeywords {
// Loads the file as binary content.
const content = fs.readFileSync(localName, "binary");
const content = Fs.readFileSync(localName, "binary");
const zip = new PizZip(content);
const doc = new Docxtemplater(zip, { paragraphLoop: true, linebreaks: true, });
if (localName.endsWith('.pptx')) {

View file

@ -38,9 +38,13 @@ import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
import { createBrowser } from '../../core.gbapp/services/GBSSR';
import { GuaribasUser } from '../../security.gbapp/models';
import { DialogKeywords } from './DialogKeywords';
import * as request from 'request-promise-native';
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
const urlJoin = require('url-join');
const Path = require('path');
const Fs = require('fs');
const url = require('url');
/**
* Web Automation services of conversation to be called by BASIC.
@ -67,6 +71,8 @@ export class WebAutomationKeywords {
*/
browser: any;
sys: any;
/**
* The number used in this execution for HEAR calls (useful for SET SCHEDULE).
*/
@ -91,10 +97,10 @@ export class WebAutomationKeywords {
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
@ -119,7 +125,7 @@ export class WebAutomationKeywords {
*
* @example x = GET PAGE
*/
public async getPage({url, username, password}) {
public async getPage({ url, username, password }) {
GBLog.info(`BASIC: Web Automation GET PAGE ${url}.`);
if (!this.browser) {
this.browser = await createBrowser(null);
@ -137,16 +143,16 @@ export class WebAutomationKeywords {
return handle;
}
public getPageByHandle(hash){
return this.pageMap[hash] ;
public getPageByHandle(hash) {
return this.pageMap[hash];
}
/**
* Find element on page DOM.
*
* @example GET page,"selector"
*/
public async getBySelector({handle, selector}) {
public async getBySelector({ handle, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation GET element: ${selector}.`);
await page.waitForSelector(selector)
@ -170,7 +176,7 @@ export class WebAutomationKeywords {
*
* @example GET page,"frameSelector,"elementSelector"
*/
public async getByFrame({handle, frame, selector}) {
public async getByFrame({ handle, frame, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation GET element by frame: ${selector}.`);
await page.waitForSelector(frame)
@ -190,10 +196,10 @@ export class WebAutomationKeywords {
/**
* Simulates a mouse hover an web page element.
*/
public async hover({handle, selector}) {
public async hover({ handle, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation HOVER element: ${selector}.`);
await this.getBySelector({handle, selector: selector});
await this.getBySelector({ handle, selector: selector });
await page.hover(selector);
await this.debugStepWeb(page);
}
@ -203,7 +209,7 @@ export class WebAutomationKeywords {
*
* @example CLICK page,"#idElement"
*/
public async click({handle, frameOrSelector, selector}) {
public async click({ handle, frameOrSelector, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation CLICK element: ${frameOrSelector}.`);
if (selector) {
@ -231,7 +237,7 @@ export class WebAutomationKeywords {
const mobile = this.min.core.getParam(this.min.instance, 'Bot Admin Number', null);
const filename = page;
if (mobile) {
await this.dk.sendFileTo({mobile , filename, caption:"General Bots Debugger"});
await this.dk.sendFileTo({ mobile, filename, caption: "General Bots Debugger" });
}
this.lastDebugWeb = new Date();
}
@ -242,7 +248,7 @@ export class WebAutomationKeywords {
*
* @example PRESS ENTER ON page
*/
public async pressKey({handle, char, frame}) {
public async pressKey({ handle, char, frame }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation PRESS ${char} ON element: ${frame}.`);
if (char.toLowerCase() === "enter") {
@ -259,7 +265,7 @@ export class WebAutomationKeywords {
}
}
public async linkByText({handle, text, index}) {
public async linkByText({ handle, text, index }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation CLICK LINK TEXT: ${text} ${index}.`);
if (!index) {
@ -277,7 +283,7 @@ export class WebAutomationKeywords {
*
* @example file = SCREENSHOT page
*/
public async screenshot({handle, selector}) {
public async screenshot({ handle, selector }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation SCREENSHOT ${selector}.`);
@ -303,13 +309,104 @@ export class WebAutomationKeywords {
*
* @example SET page,"selector","text"
*/
public async setElementText({handle, selector, text}) {
public async setElementText({ handle, selector, text }) {
const page = this.getPageByHandle(handle);
GBLog.info(`BASIC: Web Automation TYPE on ${selector}: ${text}.`);
const e = await this.getBySelector({handle, selector});
const e = await this.getBySelector({ handle, selector });
await e.click({ clickCount: 3 });
await page.keyboard.press('Backspace');
await e.type(text, { delay: 200 });
await this.debugStepWeb(page);
}
/**
* Performs the download to the .gbdrive Download folder.
*
* @example file = DOWNLOAD element, folder
*/
public async download({ handle, selector, folder }) {
const page = this.getPageByHandle(handle);
const container = page; // TODO: element['_frame'] ? element['_frame'] : element['_page'];
const element = await this.getBySelector({ handle, selector });
await page.setRequestInterception(true);
await container.click(element.originalSelector);
const xRequest = await new Promise(resolve => {
page.on('request', interceptedRequest => {
interceptedRequest.abort(); //stop intercepting requests
resolve(interceptedRequest);
});
});
const options = {
encoding: null,
method: xRequest['._method'],
uri: xRequest['_url'],
body: xRequest['_postData'],
headers: xRequest['_headers']
}
const cookies = await page.cookies();
options.headers.Cookie = cookies.map(ck => ck.name + '=' + ck.value).join(';');
GBLog.info(`BASIC: DOWNLOADING '${options.uri}...'`);
let local;
let filename;
if (options.uri.indexOf('file://') != -1) {
local = url.fileURLToPath(options.uri);
filename = Path.basename(local);
}
else {
const getBasenameFormUrl = (urlStr) => {
const url = new URL(urlStr)
return Path.basename(url.pathname)
};
filename = getBasenameFormUrl(options.uri);
}
let result: Buffer;
if (local) {
result = Fs.readFileSync(local);
} else {
result = await request.get(options);
}
let [baseUrl, client] = await GBDeployer.internalGetDriveClient(this.min);
const botId = this.min.instance.botId;
// Normalizes all slashes.
folder = folder.replace(/\\/gi, '/');
// Determines full path at source and destination.
const root = urlJoin(`/${botId}.gbai/${botId}.gbdrive`);
const dstPath = urlJoin(root, folder, filename);
// Checks if the destination contains subfolders that
// need to be created.
folder = await this.sys.createFolder(folder);
// Performs the conversion operation getting a reference
// to the source and calling /content on drive API.
let file;
try {
file = await client
.api(`${baseUrl}/drive/root:/${dstPath}:/content`)
.put(result);
} catch (error) {
if (error.code === "nameAlreadyExists") {
GBLog.info(`BASIC: DOWNLOAD destination file already exists: ${dstPath}.`);
}
throw error;
}
return file;
}
}