2018-04-21 02:59:30 -03:00
|
|
|
|
/*****************************************************************************\
|
|
|
|
|
| ( )_ _ |
|
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-04-21 02:59:30 -03:00
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
|
| | | ( )_) | |
|
|
|
|
|
| (_) \___/' |
|
|
|
|
|
| |
|
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
|
| or under a proprietary license. |
|
|
|
|
|
| |
|
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
|
| |
|
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-11 19:40:53 -03:00
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
|
| |
|
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
|
| |
|
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
2018-11-26 14:09:09 -02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2020-08-29 15:02:19 -03:00
|
|
|
|
import { GBLog, GBMinInstance, GBService, IGBCoreService, GBDialogStep } from 'botlib';
|
2018-11-26 15:54:34 -02:00
|
|
|
|
import * as fs from 'fs';
|
2020-12-27 13:30:56 -03:00
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
2018-11-30 17:30:48 -02:00
|
|
|
|
import { TSCompiler } from './TSCompiler';
|
2020-06-14 21:40:41 -03:00
|
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2020-12-27 13:30:56 -03:00
|
|
|
|
import { DialogKeywords } from './DialogKeywords';
|
2021-08-09 17:55:55 -03:00
|
|
|
|
import { ScheduleServices } from './ScheduleServices';
|
2022-06-06 18:03:02 -03:00
|
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
2019-03-09 16:59:31 -03:00
|
|
|
|
//tslint:disable-next-line:no-submodule-imports
|
2022-11-02 16:03:25 -03:00
|
|
|
|
const urlJoin = require('url-join');
|
2022-10-28 23:17:35 -03:00
|
|
|
|
const { NodeVM, VMScript } = require('vm2');
|
|
|
|
|
const { createVm2Pool } = require('./vm2-process/index');
|
2020-12-27 13:30:56 -03:00
|
|
|
|
const vb2ts = require('./vbscript-to-typescript');
|
2019-03-08 06:49:22 -03:00
|
|
|
|
const beautify = require('js-beautify').js;
|
2020-12-31 15:36:19 -03:00
|
|
|
|
const textract = require('textract');
|
|
|
|
|
const walkPromise = require('walk-promise');
|
2022-11-02 16:03:25 -03:00
|
|
|
|
const child_process = require('child_process');
|
2022-06-01 19:45:43 -03:00
|
|
|
|
const Path = require('path');
|
2022-11-02 16:03:25 -03:00
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
|
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
|
/**
|
2018-11-30 11:55:44 -02:00
|
|
|
|
* @fileoverview Virtualization services for emulation of BASIC.
|
2022-09-24 12:51:47 -03:00
|
|
|
|
* This alpha version is using a antipattern hack in form of converter to
|
2019-10-10 07:40:15 -03:00
|
|
|
|
* translate BASIC to TS and string replacements to emulate await code.
|
2021-05-24 15:06:14 -03:00
|
|
|
|
* See https://github.com/uweg/vbscript-to-typescript for more info on vb2ts, so
|
2019-01-31 11:32:33 -02:00
|
|
|
|
* http://stevehanov.ca/blog/index.php?id=92 should be used to run it without
|
|
|
|
|
* translation and enhance classic BASIC experience.
|
2018-11-11 19:09:18 -02:00
|
|
|
|
*/
|
|
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
|
/**
|
|
|
|
|
* Basic services for BASIC manipulation.
|
|
|
|
|
*/
|
2019-03-08 06:37:13 -03:00
|
|
|
|
export class GBVMService extends GBService {
|
2019-02-23 13:17:21 -03:00
|
|
|
|
public async loadDialogPackage(folder: string, min: GBMinInstance, core: IGBCoreService, deployer: GBDeployer) {
|
|
|
|
|
const files = await walkPromise(folder);
|
|
|
|
|
|
2020-06-14 21:40:41 -03:00
|
|
|
|
await CollectionUtil.asyncForEach(files, async file => {
|
|
|
|
|
if (!file) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let filename: string = file.name;
|
|
|
|
|
|
|
|
|
|
if (filename.endsWith('.docx')) {
|
|
|
|
|
const wordFile = filename;
|
|
|
|
|
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
|
|
|
|
|
const fullVbsFile = urlJoin(folder, vbsFile);
|
|
|
|
|
const docxStat = fs.statSync(urlJoin(folder, wordFile));
|
2022-08-05 00:10:23 -03:00
|
|
|
|
const interval = 3000; // If compiled is older 30 seconds, then recompile.
|
2020-06-14 21:40:41 -03:00
|
|
|
|
let writeVBS = true;
|
|
|
|
|
if (fs.existsSync(fullVbsFile)) {
|
|
|
|
|
const vbsStat = fs.statSync(fullVbsFile);
|
2022-10-12 15:08:53 -03:00
|
|
|
|
if (docxStat['mtimeMs'] < vbsStat['mtimeMs'] + interval) {
|
2020-06-14 21:40:41 -03:00
|
|
|
|
writeVBS = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-09 17:55:55 -03:00
|
|
|
|
filename = vbsFile;
|
|
|
|
|
let mainName = GBVMService.getMethodNameFromVBSFilename(filename);
|
|
|
|
|
min.scriptMap[filename] = mainName;
|
|
|
|
|
|
2020-06-14 21:40:41 -03:00
|
|
|
|
if (writeVBS) {
|
|
|
|
|
let text = await this.getTextFromWord(folder, wordFile);
|
2020-05-14 12:46:57 -03:00
|
|
|
|
|
2021-08-09 17:55:55 -03:00
|
|
|
|
const schedule = GBVMService.getSetScheduleKeywordArgs(text);
|
|
|
|
|
const s = new ScheduleServices();
|
|
|
|
|
if (schedule) {
|
|
|
|
|
await s.createOrUpdateSchedule(min, schedule, mainName);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await s.deleteScheduleIfAny(min, mainName);
|
2021-08-16 17:38:05 -03:00
|
|
|
|
}
|
2021-08-09 17:55:55 -03:00
|
|
|
|
text = text.replace(/SET SCHEDULE (.*)/gi, '');
|
|
|
|
|
fs.writeFileSync(urlJoin(folder, vbsFile), text);
|
|
|
|
|
}
|
2020-06-04 13:44:02 -03:00
|
|
|
|
|
2022-11-02 16:03:25 -03:00
|
|
|
|
// Process node_modules install.
|
|
|
|
|
|
|
|
|
|
const node_modules = urlJoin(folder, 'node_modules');
|
|
|
|
|
if (!fs.existsSync(node_modules)) {
|
|
|
|
|
const packageJson = `
|
|
|
|
|
{
|
|
|
|
|
"name": "${min.botId}.gbdialog",
|
|
|
|
|
"version": "1.0.0",
|
|
|
|
|
"description": "${min.botId} transpiled .gbdialog",
|
|
|
|
|
"author": "${min.botId} owner.",
|
|
|
|
|
"license": "ISC",
|
|
|
|
|
"dependencies": {
|
|
|
|
|
"encoding": "0.1.13",
|
|
|
|
|
"isomorphic-fetch": "3.0.0",
|
|
|
|
|
"punycode": "2.1.1",
|
|
|
|
|
"typescript-rest-rpc": "1.0.10",
|
|
|
|
|
"vm2": "3.9.11"
|
|
|
|
|
}
|
|
|
|
|
}`;
|
|
|
|
|
fs.writeFileSync(urlJoin(folder, 'package.json'), packageJson);
|
|
|
|
|
|
|
|
|
|
GBLog.info(`BASIC: Installing .gbdialog node_modules for ${min.botId}...`);
|
|
|
|
|
const npmPath = urlJoin(process.env.PWD, 'node_modules', '.bin', 'npm');
|
|
|
|
|
child_process.execSync(`${npmPath} install`, { cwd: folder });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hot swap for .vbs files.
|
|
|
|
|
|
2020-06-14 21:40:41 -03:00
|
|
|
|
const fullFilename = urlJoin(folder, filename);
|
2022-11-02 16:03:25 -03:00
|
|
|
|
if (process.env.GBDIALOG_HOTSWAP) {
|
|
|
|
|
fs.watchFile(fullFilename, async () => {
|
|
|
|
|
await this.translateBASIC(fullFilename, min, deployer, mainName);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-14 21:40:41 -03:00
|
|
|
|
|
|
|
|
|
const compiledAt = fs.statSync(fullFilename);
|
|
|
|
|
const jsfile = urlJoin(folder, `${filename}.js`);
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(jsfile)) {
|
|
|
|
|
const jsStat = fs.statSync(jsfile);
|
|
|
|
|
const interval = 30000; // If compiled is older 30 seconds, then recompile.
|
2022-10-12 15:08:53 -03:00
|
|
|
|
if (compiledAt.isFile() && compiledAt['mtimeMs'] > jsStat['mtimeMs'] + interval) {
|
2022-11-02 16:03:25 -03:00
|
|
|
|
await this.translateBASIC(fullFilename, min, deployer, mainName);
|
2020-08-29 15:02:19 -03:00
|
|
|
|
} else {
|
2020-06-14 21:40:41 -03:00
|
|
|
|
const parsedCode: string = fs.readFileSync(jsfile, 'utf8');
|
2021-08-09 17:55:55 -03:00
|
|
|
|
|
2022-11-02 16:03:25 -03:00
|
|
|
|
min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode;
|
2020-06-04 13:44:02 -03:00
|
|
|
|
}
|
2020-08-29 15:02:19 -03:00
|
|
|
|
} else {
|
2022-11-02 16:03:25 -03:00
|
|
|
|
await this.translateBASIC(fullFilename, min, deployer, mainName);
|
2020-06-14 21:40:41 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-02-23 13:17:21 -03:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 19:40:59 -03:00
|
|
|
|
public async translateBASIC(filename: any, min: GBMinInstance, deployer: GBDeployer, mainName: string) {
|
|
|
|
|
|
|
|
|
|
// Converts General Bots BASIC into regular VBS
|
|
|
|
|
|
|
|
|
|
let basicCode: string = fs.readFileSync(filename, 'utf8');
|
|
|
|
|
|
|
|
|
|
// Processes END keyword, removing extracode, useful
|
2022-11-06 20:19:05 -03:00
|
|
|
|
// for development in .gbdialog.
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
if (process.env.GBDIALOG_NOEND === 'true') {
|
|
|
|
|
basicCode = basicCode.replace(/(^|\W)END(\W|\n)/gi, '');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
let end = /(^|\W)END(\W|\n)/gi.exec(basicCode);
|
|
|
|
|
if (end) {
|
|
|
|
|
basicCode = basicCode.substring(0, end.index);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
|
|
|
|
// Removes comments.
|
|
|
|
|
|
|
|
|
|
basicCode = basicCode.replace(/((^|\W)REM.*\n)/gi, '');
|
2022-11-03 11:06:26 -03:00
|
|
|
|
basicCode = basicCode.replace(/((^|\W)\'.*\n)/gi, '');
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
|
|
|
|
// Process INCLUDE keyword to include another
|
|
|
|
|
// dialog inside the dialog.
|
|
|
|
|
|
|
|
|
|
let include = null;
|
|
|
|
|
do {
|
|
|
|
|
include = /^include\b(.*)$/gmi.exec(basicCode);
|
|
|
|
|
|
|
|
|
|
if (include) {
|
|
|
|
|
let includeName = include[1].trim();
|
|
|
|
|
includeName = Path.join(Path.dirname(filename), includeName);
|
|
|
|
|
includeName = includeName.substr(0, includeName.lastIndexOf(".")) + ".vbs";
|
|
|
|
|
|
|
|
|
|
// To use include, two /publish will be necessary (for now)
|
|
|
|
|
// because of alphabet order may raise not found errors.
|
|
|
|
|
|
|
|
|
|
let includeCode: string = fs.readFileSync(includeName, 'utf8');
|
|
|
|
|
basicCode = basicCode.replace(/^include\b.*$/gmi, includeCode);
|
|
|
|
|
}
|
|
|
|
|
} while (include);
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
const vbsCode = await this.convertGBASICToVBS(min, basicCode);
|
2022-11-02 19:40:59 -03:00
|
|
|
|
const vbsFile = `${filename}.compiled`;
|
|
|
|
|
fs.writeFileSync(vbsFile, vbsCode);
|
|
|
|
|
|
|
|
|
|
// Converts VBS into TS.
|
|
|
|
|
|
|
|
|
|
vb2ts.convertFile(vbsFile);
|
|
|
|
|
|
|
|
|
|
// Convert TS into JS.
|
|
|
|
|
|
|
|
|
|
const tsfile: string = `${filename}.ts`;
|
|
|
|
|
let tsCode: string = fs.readFileSync(tsfile, 'utf8');
|
|
|
|
|
fs.writeFileSync(tsfile, tsCode);
|
|
|
|
|
const tsc = new TSCompiler();
|
|
|
|
|
tsc.compile([tsfile]);
|
|
|
|
|
|
|
|
|
|
// Run JS into the GB context.
|
|
|
|
|
|
|
|
|
|
const jsfile = `${tsfile}.js`.replace('.ts', '');
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(jsfile)) {
|
|
|
|
|
let code: string = fs.readFileSync(jsfile, 'utf8');
|
|
|
|
|
|
|
|
|
|
code = code.replace(/^.*exports.*$/gm, '');
|
|
|
|
|
|
|
|
|
|
code = `
|
|
|
|
|
|
|
|
|
|
return (async () => {
|
|
|
|
|
require('isomorphic-fetch');
|
|
|
|
|
const rest = require ('typescript-rest-rpc/lib/client');
|
|
|
|
|
|
|
|
|
|
// Interprocess communication from local HTTP to the BotServer.
|
|
|
|
|
|
|
|
|
|
const dk = rest.createClient('http://localhost:1111/api/v2/${min.botId}/dialog');
|
|
|
|
|
const sys = rest.createClient('http://localhost:1111/api/v2/${min.botId}/system');
|
2022-11-03 11:06:26 -03:00
|
|
|
|
const wa = rest.createClient('http://localhost:1111/api/v2/${min.botId}/webautomation');
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
|
|
|
|
// Local variables.
|
|
|
|
|
|
|
|
|
|
const gb = await dk.getSingleton();
|
|
|
|
|
const id = gb.id;
|
|
|
|
|
const username = gb.username;
|
|
|
|
|
const mobile = gb.mobile;
|
|
|
|
|
const from = gb.from;
|
|
|
|
|
const ENTER = gb.ENTER;
|
|
|
|
|
const headers = gb.headers;
|
|
|
|
|
const data = gb.data;
|
|
|
|
|
const list = gb.list;
|
|
|
|
|
const httpUsername = gb.httpUsername;
|
|
|
|
|
const httpPs = gb.httpPs;
|
2022-11-03 11:06:26 -03:00
|
|
|
|
let page = null;
|
|
|
|
|
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
|
|
|
|
// Local functions.
|
|
|
|
|
|
|
|
|
|
const ubound = (array) => {return array.length};
|
|
|
|
|
const isarray = (array) => {return Array.isArray(array) };
|
|
|
|
|
|
|
|
|
|
// Remote functions.
|
|
|
|
|
|
2022-11-03 11:06:26 -03:00
|
|
|
|
const weekday = (v) => { return (async () => { return await dk.getWeekFromDate({v}) })(); };
|
|
|
|
|
const hour = (v) => { return (async () => { return await dk.getHourFromDate({v}) })(); };
|
|
|
|
|
const base64 = (v) => { return (async () => { return await dk.getCoded({v}) })(); };
|
|
|
|
|
const tolist = (v) => { return (async () => { return await dk.getToLst({v}) })(); };
|
|
|
|
|
const now = (v) => { return (async () => { return await dk.getNow({v}) })(); };
|
|
|
|
|
const today = (v) => { return (async () => { return await dk.getToday({v}) })(); };
|
2022-11-02 19:40:59 -03:00
|
|
|
|
|
|
|
|
|
${code}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
// Finds all hear calls.
|
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
const parsedCode = beautify(code, { indent_size: 2, space_in_empty_paren: true, preserve_newlines: true, wrap_line_length: 240 });
|
2022-11-02 19:40:59 -03:00
|
|
|
|
fs.writeFileSync(jsfile, parsedCode);
|
|
|
|
|
|
|
|
|
|
min.sandBoxMap[mainName.toLowerCase().trim()] = parsedCode;
|
|
|
|
|
|
|
|
|
|
GBLog.info(`[GBVMService] Finished loading of ${filename}, JavaScript from Word: \n ${parsedCode}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 17:06:22 -03:00
|
|
|
|
public static getMethodNameFromVBSFilename(filename: string) {
|
|
|
|
|
let mainName = filename.replace(/\s|\-/gi, '').split('.')[0];
|
|
|
|
|
return mainName.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 17:55:55 -03:00
|
|
|
|
public static getSetScheduleKeywordArgs(code: string) {
|
|
|
|
|
if (!code)
|
|
|
|
|
return null;
|
|
|
|
|
const keyword = /SET SCHEDULE (.*)/gi;
|
|
|
|
|
const result = keyword.exec(code);
|
|
|
|
|
return result ? result[1] : null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
|
private async getTextFromWord(folder: string, filename: string) {
|
|
|
|
|
return new Promise<string>(async (resolve, reject) => {
|
2020-08-29 15:02:19 -03:00
|
|
|
|
textract.fromFileWithPath(urlJoin(folder, filename), { preserveLineBreaks: true }, (error, text) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
} else {
|
|
|
|
|
text = text.replace('“', '"');
|
|
|
|
|
text = text.replace('”', '"');
|
|
|
|
|
text = text.replace('‘', "'");
|
|
|
|
|
text = text.replace('’', "'");
|
|
|
|
|
|
|
|
|
|
resolve(text);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-05-15 14:07:30 -03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-23 13:17:21 -03:00
|
|
|
|
/**
|
|
|
|
|
* Converts General Bots BASIC
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param code General Bots BASIC
|
|
|
|
|
*/
|
2022-11-04 14:28:02 -03:00
|
|
|
|
public async convertGBASICToVBS(min: GBMinInstance, code: string) {
|
2019-02-23 13:17:21 -03:00
|
|
|
|
|
2022-11-02 16:03:25 -03:00
|
|
|
|
// Start and End of VB2TS tags of processing.
|
2020-11-22 11:22:55 -03:00
|
|
|
|
|
2020-05-11 10:41:41 -03:00
|
|
|
|
code = `<%\n
|
2022-11-02 16:03:25 -03:00
|
|
|
|
|
2020-11-08 13:39:18 -03:00
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
${process.env.ENABLE_AUTH ? `hear gbLogin as login` : ``}
|
2021-08-30 16:30:10 -03:00
|
|
|
|
|
2020-05-11 10:41:41 -03:00
|
|
|
|
${code}
|
2022-10-09 20:46:17 -03:00
|
|
|
|
|
2020-05-11 10:41:41 -03:00
|
|
|
|
`;
|
2019-02-23 13:17:21 -03:00
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
// Split all params by comma, not inside strings.
|
|
|
|
|
|
|
|
|
|
const getParams = (text, names) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
|
2022-11-05 17:59:41 -03:00
|
|
|
|
let ret = {};
|
2022-11-06 20:19:05 -03:00
|
|
|
|
const items = text.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g);
|
2022-11-04 14:28:02 -03:00
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
let i = 0;
|
|
|
|
|
names.forEach(name => {
|
|
|
|
|
let value = items[i];
|
|
|
|
|
|
|
|
|
|
// Add string to JSON without quotes.
|
|
|
|
|
|
|
|
|
|
if (value && (value.charAt(0) === '"' || value.charAt(0) === '\'')) {
|
|
|
|
|
value = value.substr(1, value.length - 2);
|
|
|
|
|
}
|
|
|
|
|
ret[name] = value;
|
|
|
|
|
i++;
|
2022-11-05 17:59:41 -03:00
|
|
|
|
});
|
2022-11-04 14:28:02 -03:00
|
|
|
|
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return JSON.stringify(ret);
|
2022-11-04 14:28:02 -03:00
|
|
|
|
};
|
|
|
|
|
|
2019-02-23 13:17:21 -03:00
|
|
|
|
// Keywords from General Bots BASIC.
|
2022-06-01 19:45:43 -03:00
|
|
|
|
|
2022-06-04 17:02:34 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*SELECT\s*(.*)/gi, ($0, $1, $2) => {
|
2022-06-04 20:08:39 -03:00
|
|
|
|
let tableName = /\sFROM\s(\w+)/.exec($2)[1];
|
|
|
|
|
let sql = `SELECT ${$2}`.replace(tableName, '?');
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await sys.executeSQL({data:${$1}, sql:"${sql}", tableName:"${tableName}"})\n`;
|
2022-06-04 17:02:34 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
code = code.replace(/open\s*(.*)/gi, ($0, $1, $2) => {
|
|
|
|
|
|
|
|
|
|
if (!$1.startsWith("\"") && $1.startsWith("\'")) {
|
|
|
|
|
$1 = `"${$1}"`;
|
|
|
|
|
}
|
|
|
|
|
const params = getParams($1, ['url', 'username', 'password']);
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `page = await wa.getPage(${params})\n`;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
});
|
2022-11-02 16:03:25 -03:00
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
code = code.replace(/(set hear on)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
|
|
|
|
return `hrOn = ${$3}\n`;
|
|
|
|
|
});
|
2019-02-23 13:17:21 -03:00
|
|
|
|
|
2021-08-30 16:30:10 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as login/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"login"})`;
|
2021-08-30 16:30:10 -03:00
|
|
|
|
});
|
2022-11-02 16:03:25 -03:00
|
|
|
|
|
2020-12-07 23:08:52 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as email/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"email"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as integer/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"integer"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as file/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"file"})`;
|
2022-06-03 23:52:28 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as boolean/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"boolean"})`;
|
2020-12-07 23:24:00 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as name/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"name"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as date/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"date"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as hour/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"hour"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as phone/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"phone"})`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as money/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"money")}`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as language/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"language")}`;
|
2020-12-22 21:48:41 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
code = code.replace(/hear (\w+) as zipcode/gi, ($0, $1) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"zipcode")}`;
|
2020-12-07 23:08:52 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/hear (\w+) as (.*)/gi, ($0, $1, $2) => {
|
2022-11-06 20:19:05 -03:00
|
|
|
|
return `${$1} = await dk.getHear({kind:"menu", args: [${$2}])}`;
|
2020-12-07 22:25:43 -03:00
|
|
|
|
});
|
2020-05-14 12:46:57 -03:00
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(hear)\s*(\w+)/gi, ($0, $1, $2) => {
|
2022-11-02 19:40:59 -03:00
|
|
|
|
return `${$2} = await dk.getHear({})`;
|
2018-11-30 17:30:48 -02:00
|
|
|
|
});
|
2020-07-07 10:15:39 -03:00
|
|
|
|
|
2021-12-20 20:27:02 -03:00
|
|
|
|
code = code.replace(/(\w)\s*\=\s*find contact\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `${$1} = await dk.fndContact({${$2})\n`;
|
2021-12-20 20:27:02 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-08-15 09:16:22 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*=\s*find\s*(.*)\s*or talk\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await sys.find({args:[${$2}])\n
|
2021-08-16 17:38:05 -03:00
|
|
|
|
if (!${$1}) {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
await dk.talk ({${$3}})\n;
|
2021-08-16 17:38:05 -03:00
|
|
|
|
return -1;
|
2021-05-18 14:22:10 -03:00
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
code = code.replace(/CALL\s*(.*)/gi, ($0, $1) => {
|
|
|
|
|
return `await ${$1}\n`;
|
2022-08-01 18:36:45 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(\w)\s*\=\s*find\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await sys.find({args: [${$2}]})\n`;
|
2020-05-14 12:46:57 -03:00
|
|
|
|
});
|
2019-02-23 13:17:21 -03:00
|
|
|
|
|
2021-12-20 18:27:10 -03:00
|
|
|
|
code = code.replace(/(\w)\s*\=\s*create deal(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
const params = getParams($3, ['dealName', 'contact', 'company', 'amount']);
|
2022-11-05 17:59:41 -03:00
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await dk.createDeal(${params})\n`;
|
2021-12-20 18:27:10 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-12-19 16:39:50 -03:00
|
|
|
|
code = code.replace(/(\w)\s*\=\s*active tasks/gi, ($0, $1) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await dk.getActiveTasks({})\n`;
|
2021-12-19 16:39:50 -03:00
|
|
|
|
});
|
2022-05-03 17:05:57 -03:00
|
|
|
|
|
2021-08-03 16:24:59 -03:00
|
|
|
|
code = code.replace(/(\w)\s*\=\s*append\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await sys.append({args:[${$2}]})\n`;
|
2021-08-03 16:24:59 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*sort\s*(\w+)\s*by(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await sys.sortBy({array: ${$2}, memberName: "${$3}"})\n`;
|
2021-08-03 16:24:59 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-16 14:53:52 -03:00
|
|
|
|
code = code.replace(/see\s*text\s*of\s*(\w+)\s*as\s*(\w+)\s*/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$2} = await sys.seeText({url: ${$1})\n`;
|
2021-11-16 14:53:52 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/see\s*caption\s*of\s*(\w+)\s*as(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$2} = await sys.seeCaption({url: ${$1})\n`;
|
2021-11-16 14:53:52 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(wait)\s*(\d+)/gi, ($0, $1, $2) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `await sys.wait({seconds:${$2})`;
|
2019-02-23 13:17:21 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(get stock for )(.*)/gi, ($0, $1, $2) => {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `stock = await sys.getStock({symbol: ${$2})`;
|
2019-02-23 13:17:21 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*get\s(.*)/gi, ($0, $1, $2, $3) => {
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
const count = ($2.match(/\,/g) || []).length;
|
|
|
|
|
const values = $2.split(',');
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-11-03 11:06:26 -03:00
|
|
|
|
// Handles GET "selector".
|
2022-06-01 19:45:43 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
if (count == 1) {
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await wa.getBySelector({handle:page, selector: ${values[0]}})`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
}
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-11-03 11:06:26 -03:00
|
|
|
|
// Handles GET "frameSelector", "selector"
|
2022-06-01 19:45:43 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
else if (count == 2) {
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await wa.getByFrame({handle: page, ${values[0]}, frameOrSelector: ${values[1]}, selector: ${values[2]}})`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
}
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
// Handles the GET http version.
|
|
|
|
|
|
|
|
|
|
else {
|
2022-11-04 14:28:02 -03:00
|
|
|
|
return `${$1} = await sys.get ({file: ${$2}, addressOrHeaders: headers, httpUsername, httpPs})`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
}
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
2022-08-05 00:10:23 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/\= NEW OBJECT/gi, ($0, $1, $2, $3) => {
|
|
|
|
|
return ` = {}`;
|
2020-11-06 15:23:56 -03:00
|
|
|
|
});
|
2022-08-26 09:53:00 -03:00
|
|
|
|
|
2022-08-05 00:10:23 -03:00
|
|
|
|
code = code.replace(/\= NEW ARRAY/gi, ($0, $1, $2, $3) => {
|
|
|
|
|
return ` = []`;
|
|
|
|
|
});
|
2022-08-26 09:53:00 -03:00
|
|
|
|
|
2020-11-06 15:23:56 -03:00
|
|
|
|
|
2021-11-16 15:52:09 -03:00
|
|
|
|
code = code.replace(/(go to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['fromOrDialogName', 'dialogName']);
|
|
|
|
|
return `await dk.gotoDialog(${params})\n`;
|
2021-11-16 15:52:09 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-12-22 21:48:41 -03:00
|
|
|
|
code = code.replace(/(set language)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `await dk.setLanguage ({${$3}})\n`;
|
2020-12-22 21:48:41 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-21 20:21:19 -03:00
|
|
|
|
code = code.replace(/set header\s*(.*)\sas\s(.*)/gi, ($0, $1, $2) => {
|
|
|
|
|
return `headers[${$1}]=${$2})`;
|
2021-11-20 13:44:18 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-21 20:21:19 -03:00
|
|
|
|
code = code.replace(/set http username\s*\=\s*(.*)/gi, ($0, $1) => {
|
2021-11-20 13:44:18 -03:00
|
|
|
|
return `httpUsername = ${$1}`;
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-21 20:21:19 -03:00
|
|
|
|
code = code.replace(/set http password\s*\=\s*(.*)/gi, ($0, $1) => {
|
|
|
|
|
return `httpPs = ${$1}`;
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-15 10:13:36 -03:00
|
|
|
|
code = code.replace(/(datediff)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['date1', 'date2', 'mode']);
|
|
|
|
|
return `await dk.dateDiff (${params}})\n`;
|
2021-08-15 10:13:36 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(dateadd)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['date', 'mode', 'units']);
|
|
|
|
|
return `await dk.dateAdd (${$3})\n`;
|
2021-08-15 10:13:36 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 10:07:59 -03:00
|
|
|
|
code = code.replace(/(set max lines)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.setMaxLines ({count: ${$3}})\n`;
|
2021-04-12 10:07:59 -03:00
|
|
|
|
});
|
2021-08-09 17:55:55 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
code = code.replace(/(set max columns)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.setMaxColumns ({count: ${$3}})\n`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-04-22 14:39:51 -03:00
|
|
|
|
code = code.replace(/(set translator)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.setTranslatorOn ({on: "${$3.toLowerCase()}"})\n`;
|
2021-04-22 14:39:51 -03:00
|
|
|
|
});
|
2021-04-12 10:07:59 -03:00
|
|
|
|
|
2022-06-04 17:02:34 -03:00
|
|
|
|
code = code.replace(/(set theme)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.setTheme ({theme: "${$3.toLowerCase()}"})\n`;
|
2022-06-04 17:02:34 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-08-21 09:18:28 -03:00
|
|
|
|
code = code.replace(/(set whole word)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.setWholeWord ({on: "${$3.toLowerCase()}"})\n`;
|
2021-08-21 09:18:28 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*post\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.postByHttp ({url:${$2}, data:${$3}, headers})`;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-08-05 00:10:23 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*put\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.putByHttp ({url:${$2}, data:${$3}, headers})`;
|
2022-08-05 00:10:23 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*download\s*(.*),\s*(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.download ({handle:page, selector: ${$2}, folder:${$3}})`;
|
2019-03-08 06:37:13 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-07-13 09:38:13 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*CREATE FOLDER\s*(.*)/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.createFolder ({name:${$2}})`;
|
2022-07-13 09:38:13 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/SHARE FOLDER\s*(.*)/gi, ($0, $1) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await sys.shareFolder ({name: ${$1}})`;
|
2022-07-13 09:38:13 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(create a bot farm using)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-03 11:06:26 -03:00
|
|
|
|
return `await sys.createABotFarmUsing ({${$3}})`;
|
2019-02-23 13:17:21 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-05-19 10:22:22 -03:00
|
|
|
|
code = code.replace(/(transfer to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.transferTo ({to:${$3}})\n`;
|
2022-05-19 10:22:22 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-05-19 12:17:28 -03:00
|
|
|
|
code = code.replace(/(\btransfer\b)(?=(?:[^"]|"[^"]*")*$)/gi, () => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.transferTo ({})\n`;
|
2020-10-14 14:04:02 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-02-07 18:28:54 -03:00
|
|
|
|
code = code.replace(/(exit)/gi, () => {
|
2022-11-02 16:03:25 -03:00
|
|
|
|
return ``;
|
2020-12-02 17:20:38 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-02-07 18:28:54 -03:00
|
|
|
|
code = code.replace(/(show menu)/gi, () => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await dk.showMenu ({})\n`;
|
2020-12-11 07:35:55 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-11-08 13:39:18 -03:00
|
|
|
|
code = code.replace(/(talk to)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['mobile', 'message']);
|
|
|
|
|
return `await sys.talkTo(${params})\n`;
|
2020-11-08 13:39:18 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(talk)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
if ($3.substr(0, 1) !== "\"") {
|
|
|
|
|
$3 = `"${$3}"`;
|
|
|
|
|
}
|
|
|
|
|
return `await dk.talk ({text: ${$3}})\n`;
|
2019-02-23 13:17:21 -03:00
|
|
|
|
});
|
2020-11-11 12:27:17 -03:00
|
|
|
|
|
2020-11-26 12:45:10 -03:00
|
|
|
|
code = code.replace(/(send sms to)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['mobile', 'message']);
|
|
|
|
|
return `await sys.sendSmsTo(${params})\n`;
|
2020-11-26 12:45:10 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-06-22 20:38:32 -03:00
|
|
|
|
code = code.replace(/(send email)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['to', 'subject', 'body']);
|
|
|
|
|
return `await dk.sendEmail(${params})\n`;
|
2022-06-22 19:49:53 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-06-22 20:38:32 -03:00
|
|
|
|
code = code.replace(/(send mail)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['to', 'subject', 'body']);
|
|
|
|
|
return `await dk.sendEmail(${params})\n`;
|
2022-06-22 20:38:32 -03:00
|
|
|
|
});
|
2022-08-26 09:53:00 -03:00
|
|
|
|
|
2020-11-11 11:10:40 -03:00
|
|
|
|
code = code.replace(/(send file to)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['mobile', 'filename', 'caption']);
|
|
|
|
|
return `await dk.sendFileTo(${params})\n`;
|
2020-11-11 11:10:40 -03:00
|
|
|
|
});
|
2019-02-23 13:17:21 -03:00
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
|
code = code.replace(/(hover)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['handle', 'selector']);
|
|
|
|
|
return `await wa.hover (${params})\n`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(click link text)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams('page,' + $3, ['handle', 'text', 'index']);
|
|
|
|
|
return `await wa.linkByText (${params})\n`;
|
2022-08-26 09:53:00 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
code = code.replace(/(click)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams('page,' + $3, ['handle', 'frameOrSelector', 'selector']);
|
|
|
|
|
return `await wa.click (${params})\n`;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
});
|
|
|
|
|
|
2020-11-11 11:38:19 -03:00
|
|
|
|
code = code.replace(/(send file)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['filename', 'caption']);
|
|
|
|
|
return `await dk.sendFile(${params})\n`;
|
2020-11-11 11:38:19 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-01-20 18:23:42 -03:00
|
|
|
|
code = code.replace(/(copy)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['src', 'dst']);
|
|
|
|
|
return `await sys.copyFile (${params})\n`;
|
2021-01-20 18:23:42 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(convert)(\s*)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($3, ['src', 'dst']);
|
|
|
|
|
return `await sys.convert (${params})\n`;
|
2021-01-15 11:50:43 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as chart/gi, ($0, $1, $2) => {
|
|
|
|
|
return `await dk.chart ({type:'bar', data: ${2}, legends:null, transpose: false})\n`;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(chart)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
|
|
|
|
const params = getParams($3, ['type', 'data', 'legends', 'transpose']);
|
|
|
|
|
return `await dk.chart (${params})\n`;
|
|
|
|
|
});
|
2022-07-06 12:36:51 -03:00
|
|
|
|
|
2022-08-07 23:54:19 -03:00
|
|
|
|
code = code.replace(/MERGE\s(.*)\sWITH\s(.*)BY\s(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await sys.merge({file: ${$1}, data: ${$2}, key1: ${$3}})\n`;
|
2022-08-07 23:54:19 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-11-05 17:59:41 -03:00
|
|
|
|
code = code.replace(/PRESS\s(.*)/gi, ($0, $1, $2) => {
|
|
|
|
|
return `await wa.pressKey({handle: page, char: ${$1})\n`;
|
2022-09-12 08:52:51 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/SCREENSHOT\s(.*)/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await wa.screenshot({handle: page, selector: ${$1}})\n`;
|
2022-09-12 08:52:51 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-09-24 12:51:47 -03:00
|
|
|
|
code = code.replace(/TWEET\s(.*)/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await sys.tweet({text: ${$1})\n`;
|
2022-09-24 12:51:47 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-06-04 17:02:34 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as image/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.asImage({data: ${$2}})\n`;
|
2022-06-04 20:08:39 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as pdf/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `${$1} = await sys.asPdf({data: ${$2})\n`;
|
2022-06-04 17:02:34 -03:00
|
|
|
|
});
|
|
|
|
|
|
2022-07-12 13:30:12 -03:00
|
|
|
|
code = code.replace(/(\w+)\s*\=\s*FILL\s(.*)\sWITH\s(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-06 20:19:05 -03:00
|
|
|
|
return `${$1} = await sys.fill({templateName: ${$2}, data: ${$3}})\n`;
|
2022-07-12 13:30:12 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-21 20:21:19 -03:00
|
|
|
|
code = code.replace(/save\s(.*)\sas\s(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
return `await sys.saveFile({file: ${$2}, data: ${$1})\n`;
|
2021-11-21 20:21:19 -03:00
|
|
|
|
});
|
2020-06-03 21:31:00 -03:00
|
|
|
|
code = code.replace(/(save)(\s)(.*)/gi, ($0, $1, $2, $3) => {
|
2022-11-06 20:19:05 -03:00
|
|
|
|
return `await sys.save({args: [${$3}]})\n`;
|
2020-05-11 10:41:41 -03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-21 20:21:19 -03:00
|
|
|
|
code = code.replace(/set\s(.*)/gi, ($0, $1, $2) => {
|
2022-11-05 17:59:41 -03:00
|
|
|
|
const params = getParams($1, ['file', 'address', 'value']);
|
|
|
|
|
return `await sys.set (${params})`;
|
2021-11-21 20:21:19 -03:00
|
|
|
|
});
|
|
|
|
|
|
2019-02-23 13:17:21 -03:00
|
|
|
|
code = `${code}\n%>`;
|
|
|
|
|
|
|
|
|
|
return code;
|
2018-12-01 17:31:57 -02:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 07:47:48 -03:00
|
|
|
|
/**
|
|
|
|
|
* Executes the converted JavaScript from BASIC code inside execution context.
|
|
|
|
|
*/
|
2022-11-02 19:40:59 -03:00
|
|
|
|
public static async callVM(text: string, min: GBMinInstance, step, GBDialogdeployer: GBDeployer) {
|
2021-01-13 13:36:44 -03:00
|
|
|
|
|
2021-01-05 07:47:48 -03:00
|
|
|
|
// Creates a class DialogKeywords which is the *this* pointer
|
|
|
|
|
// in BASIC.
|
2021-08-09 21:47:36 -03:00
|
|
|
|
|
2021-08-16 17:38:05 -03:00
|
|
|
|
const user = step ? await min.userProfile.get(step.context, {}) : null;
|
2022-06-06 18:03:02 -03:00
|
|
|
|
|
2022-11-02 19:40:59 -03:00
|
|
|
|
const sandbox = { user: user.systemUser };
|
2021-01-13 13:36:44 -03:00
|
|
|
|
|
2022-06-06 18:03:02 -03:00
|
|
|
|
const contentLocale = min.core.getParam<string>(
|
|
|
|
|
min.instance,
|
|
|
|
|
'Default Content Language',
|
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-01 18:36:45 -03:00
|
|
|
|
// Auto-NLP generates BASIC variables related to entities.
|
|
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
|
if (step && step.context.activity['originalText']) {
|
2022-06-07 18:37:29 -03:00
|
|
|
|
const entities = await min["nerEngine"].findEntities(
|
|
|
|
|
step.context.activity['originalText'],
|
|
|
|
|
contentLocale);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < entities.length; i++) {
|
|
|
|
|
const v = entities[i];
|
|
|
|
|
const variableName = `${v.entity}`;
|
|
|
|
|
sandbox[variableName] = v.option;
|
|
|
|
|
}
|
2022-06-06 18:03:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-28 23:17:35 -03:00
|
|
|
|
const botId = min.botId;
|
|
|
|
|
const gbdialogPath = urlJoin(process.cwd(), 'work', `${botId}.gbai`, `${botId}.gbdialog`);
|
|
|
|
|
const scriptPath = urlJoin(gbdialogPath, `${text}.js`);
|
2021-01-13 13:36:44 -03:00
|
|
|
|
|
2022-10-22 13:23:32 -03:00
|
|
|
|
let code = min.sandBoxMap[text];
|
2021-01-05 07:47:48 -03:00
|
|
|
|
|
2022-11-02 16:03:25 -03:00
|
|
|
|
if (GBConfigService.get('VM3') === 'true') {
|
2022-10-28 23:17:35 -03:00
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
const vm1 = new NodeVM({
|
|
|
|
|
allowAsync: true,
|
|
|
|
|
sandbox: {},
|
|
|
|
|
console: 'inherit',
|
|
|
|
|
wrapper: 'commonjs',
|
|
|
|
|
require: {
|
|
|
|
|
builtin: ['stream', 'http', 'https', 'url', 'zlib'],
|
|
|
|
|
root: ['./'],
|
|
|
|
|
external: true,
|
|
|
|
|
context: 'sandbox'
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const s = new VMScript(code, { filename: scriptPath });
|
|
|
|
|
let x = vm1.run(s);
|
|
|
|
|
return x;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`BASIC RUNTIME ERR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
|
|
|
|
|
}
|
2021-01-05 07:47:48 -03:00
|
|
|
|
|
2022-10-28 23:17:35 -03:00
|
|
|
|
} else {
|
|
|
|
|
const runnerPath = urlJoin(process.cwd(), 'dist', 'packages', 'basic.gblib', 'services', 'vm2-process', 'vm2ProcessRunner.js');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { run, drain } = createVm2Pool({
|
|
|
|
|
min: 1,
|
|
|
|
|
max: 1,
|
2022-11-06 20:19:05 -03:00
|
|
|
|
debuggerPort: 9222,
|
2022-10-28 23:17:35 -03:00
|
|
|
|
cpu: 100,
|
2022-11-02 16:03:25 -03:00
|
|
|
|
memory: 50000,
|
|
|
|
|
time: 60 * 60 * 24 * 14,
|
2022-10-28 23:17:35 -03:00
|
|
|
|
cwd: gbdialogPath,
|
|
|
|
|
script: runnerPath
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-06 20:19:05 -03:00
|
|
|
|
const port = run.port;
|
2022-11-02 16:03:25 -03:00
|
|
|
|
const result = await run(code, { filename: scriptPath, sandbox: sandbox });
|
2022-10-28 23:17:35 -03:00
|
|
|
|
|
|
|
|
|
drain();
|
2022-11-02 16:03:25 -03:00
|
|
|
|
return result;
|
2022-10-28 23:17:35 -03:00
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`BASIC RUNTIME ERR: ${error.message ? error.message : error}\n Stack:${error.stack}`);
|
|
|
|
|
}
|
2020-12-06 16:22:34 -03:00
|
|
|
|
}
|
2020-08-29 15:02:19 -03:00
|
|
|
|
}
|
2018-11-12 12:20:44 -02:00
|
|
|
|
}
|