2024-01-10 15:01:02 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
|
|
|
| |
|
2024-05-26 20:13:56 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
|
2024-01-10 15:01:02 -03:00
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| 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, |
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
2024-05-26 20:13:56 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.cloud. |
|
2024-01-10 15:01:02 -03:00
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
2024-01-09 17:40:48 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots local utility.
|
|
|
|
*/
|
2024-01-09 17:40:48 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
'use strict';
|
2024-05-26 20:13:56 -03:00
|
|
|
import * as YAML from 'yaml';
|
2024-08-19 16:12:23 -03:00
|
|
|
import SwaggerClient from 'swagger-client';
|
|
|
|
import Fs from 'fs';
|
2024-01-10 15:01:02 -03:00
|
|
|
|
|
|
|
export class GBUtil {
|
2024-05-26 20:13:56 -03:00
|
|
|
public static repeat(chr, count) {
|
|
|
|
let str = '';
|
|
|
|
for (let x = 0; x < count; x++) {
|
|
|
|
str += chr;
|
|
|
|
}
|
2024-01-13 14:23:04 -03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
public static padL(value, width, pad) {
|
|
|
|
if (!width || width < 1) return value;
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!pad) pad = ' ';
|
|
|
|
const length = width - value.length;
|
2024-01-13 14:23:04 -03:00
|
|
|
if (length < 1) return value.substr(0, width);
|
|
|
|
|
|
|
|
return (GBUtil.repeat(pad, length) + value).substr(0, width);
|
|
|
|
}
|
2024-08-19 16:12:23 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
public static padR(value, width, pad) {
|
|
|
|
if (!width || width < 1) return value;
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!pad) pad = ' ';
|
|
|
|
const length = width - value.length;
|
2024-01-13 14:23:04 -03:00
|
|
|
if (length < 1) value.substr(0, width);
|
|
|
|
|
|
|
|
return (value + GBUtil.repeat(pad, length)).substr(0, width);
|
|
|
|
}
|
|
|
|
|
2024-08-19 16:12:23 -03:00
|
|
|
public static async getDirectLineClient(min) {
|
|
|
|
|
|
|
|
let config = {
|
|
|
|
url: `http://127.0.0.1:${process.env.port}/api/messages`,
|
|
|
|
spec: JSON.parse(Fs.readFileSync('directline-3.0.json', 'utf8')),
|
|
|
|
requestInterceptor: req => {
|
|
|
|
req.headers['Authorization'] = `Bearer ${min.instance.webchatKey}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (process.env.STORAGE_FILE) {
|
|
|
|
config['spec'].servers = [{ url: `http://127.0.0.1:${process.env.PORT}/api/messages` }];
|
|
|
|
config['openapi'] = '3.0.0';
|
|
|
|
}
|
|
|
|
return await new SwaggerClient(config);
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:13:56 -03:00
|
|
|
public static toYAML(json) {
|
|
|
|
const doc = new YAML.Document();
|
|
|
|
doc.contents = json;
|
|
|
|
return doc.toString();
|
|
|
|
}
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-01-10 15:01:02 -03:00
|
|
|
public static sleep(ms) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
2024-01-10 15:01:02 -03:00
|
|
|
|
|
|
|
public static caseInsensitive(listOrRow) {
|
2024-05-26 20:13:56 -03:00
|
|
|
if (!listOrRow || typeof listOrRow !== 'object') {
|
2024-01-09 17:40:48 -03:00
|
|
|
return listOrRow;
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
|
|
|
const lowercase = oldKey => (typeof oldKey === 'string' ? oldKey.toLowerCase() : oldKey);
|
|
|
|
const createCaseInsensitiveProxy = obj => {
|
2024-01-09 17:40:48 -03:00
|
|
|
const propertiesMap = new Map(Object.keys(obj).map(propKey => [lowercase(propKey), obj[propKey]]));
|
|
|
|
const caseInsensitiveGetHandler = {
|
|
|
|
get: (target, property) => propertiesMap.get(lowercase(property))
|
|
|
|
};
|
|
|
|
return new Proxy(obj, caseInsensitiveGetHandler);
|
|
|
|
};
|
2024-05-26 20:13:56 -03:00
|
|
|
if (Array.isArray(listOrRow)) {
|
2024-01-09 17:40:48 -03:00
|
|
|
return listOrRow.map(row => createCaseInsensitiveProxy(row));
|
|
|
|
} else {
|
|
|
|
return createCaseInsensitiveProxy(listOrRow);
|
|
|
|
}
|
2024-05-26 20:13:56 -03:00
|
|
|
}
|
|
|
|
}
|