2020-12-27 13:30:56 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
| "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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2021-04-12 10:07:59 -03:00
|
|
|
import { GBDialogStep, GBLog, GBMinInstance } from 'botlib';
|
2021-08-11 13:37:41 -03:00
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
2022-01-03 13:11:21 -03:00
|
|
|
const urlJoin = require('url-join');
|
2020-12-27 13:30:56 -03:00
|
|
|
import { GBServer } from '../../../src/app';
|
2020-12-31 15:36:19 -03:00
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
2020-12-27 13:30:56 -03:00
|
|
|
import { SecService } from '../../security.gbapp/services/SecService';
|
|
|
|
import { SystemKeywords } from './SystemKeywords';
|
2021-10-13 09:39:24 -03:00
|
|
|
import { GBMinService } from '../../core.gbapp/services/GBMinService';
|
2021-12-19 16:39:50 -03:00
|
|
|
import { HubSpotServices } from '../../hubspot.gblib/services/HubSpotServices';
|
2021-12-31 09:39:23 -03:00
|
|
|
import { WhatsappDirectLine } from '../../whatsapp.gblib/services/WhatsappDirectLine';
|
2022-05-29 15:16:38 -03:00
|
|
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
|
|
|
|
import * as fs from 'fs';
|
2022-05-03 17:05:57 -03:00
|
|
|
const DateDiff = require('date-diff');
|
|
|
|
const puppeteer = require('puppeteer');
|
2022-05-29 15:16:38 -03:00
|
|
|
const Path = require('path');
|
2022-06-03 07:43:20 -03:00
|
|
|
const c3ChartMaker = require('c3-chart-maker');
|
2020-12-27 13:30:56 -03:00
|
|
|
|
|
|
|
/**
|
2020-12-28 18:43:34 -03:00
|
|
|
* Base services of conversation to be called by BASIC which
|
|
|
|
* requries step variable to work.
|
2020-12-27 13:30:56 -03:00
|
|
|
*/
|
|
|
|
export class DialogKeywords {
|
2020-12-28 18:43:34 -03:00
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
/**
|
|
|
|
* Reference to minimal bot instance.
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public min: GBMinInstance;
|
2020-12-28 18:43:34 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the base system keywords functions to be called.
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public internalSys: SystemKeywords;
|
2021-06-01 10:05:52 -03:00
|
|
|
|
2021-04-30 13:20:49 -03:00
|
|
|
/**
|
|
|
|
* Current user object to get BASIC properties read.
|
|
|
|
*/
|
|
|
|
public user;
|
2021-04-22 14:07:59 -03:00
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
/**
|
|
|
|
* HTML browser for conversation over page interaction.
|
|
|
|
*/
|
|
|
|
browser: any;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number used in this execution for HEAR calls (useful for SET SCHEDULE).
|
|
|
|
*/
|
2022-05-29 15:16:38 -03:00
|
|
|
hrOn: string;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* When creating this keyword facade, a bot instance is
|
|
|
|
* specified among the deployer service.
|
|
|
|
*/
|
2021-04-22 14:07:59 -03:00
|
|
|
constructor(min: GBMinInstance, deployer: GBDeployer, step: GBDialogStep, user) {
|
2020-12-27 13:30:56 -03:00
|
|
|
this.min = min;
|
2021-04-22 14:07:59 -03:00
|
|
|
this.user = user;
|
2021-04-30 13:20:49 -03:00
|
|
|
this.internalSys = new SystemKeywords(min, deployer, this);
|
2020-12-27 13:30:56 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Base reference of system keyword facade, called directly
|
|
|
|
* by the script.
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public sys(): SystemKeywords {
|
|
|
|
return this.internalSys;
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
/**
|
|
|
|
* Returns the page object.
|
|
|
|
*
|
|
|
|
* @example x = GET PAGE
|
|
|
|
*/
|
|
|
|
public async getPage(step, url) {
|
2022-05-19 10:22:22 -03:00
|
|
|
|
2022-05-29 15:16:38 -03:00
|
|
|
if (!this.browser) {
|
2022-05-19 10:22:22 -03:00
|
|
|
this.browser = await puppeteer.launch({
|
|
|
|
args: [
|
2022-05-29 15:16:38 -03:00
|
|
|
'--ignore-certificate-errors',
|
|
|
|
'--no-sandbox',
|
|
|
|
'--disable-setuid-sandbox',
|
|
|
|
'--window-size=1920,1080',
|
|
|
|
"--disable-accelerated-2d-canvas",
|
|
|
|
"--disable-gpu"],
|
2022-05-19 10:22:22 -03:00
|
|
|
ignoreHTTPSErrors: true,
|
|
|
|
headless: false,
|
|
|
|
});
|
|
|
|
}
|
2022-05-03 17:05:57 -03:00
|
|
|
const page = await this.browser.newPage();
|
|
|
|
await page.goto(url);
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2022-06-03 07:43:20 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* https://c3js.org/examples.html
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @param legends
|
|
|
|
* @see https://www.npmjs.com/package/plot
|
|
|
|
*/
|
|
|
|
public async getChart(data, legends)
|
|
|
|
{
|
|
|
|
const chartDefinition = {
|
|
|
|
"size": {
|
|
|
|
"height": 600,
|
|
|
|
"width": 1200
|
|
|
|
},
|
|
|
|
"series": {
|
|
|
|
"x": "Date",
|
|
|
|
"Close": "Close",
|
|
|
|
"Volume": "Volume"
|
|
|
|
},
|
|
|
|
"data": {
|
|
|
|
"x": "x",
|
|
|
|
"type": "line",
|
|
|
|
"axes": {
|
|
|
|
"Volume": "y2"
|
|
|
|
},
|
|
|
|
"types": {
|
|
|
|
"Volume": "bar"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"axis": {
|
|
|
|
"x": {
|
|
|
|
"type": "timeseries",
|
|
|
|
"tick": {
|
|
|
|
"format": "%Y-%m-%d"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"y": {
|
|
|
|
"label": {
|
|
|
|
"text": "Close",
|
|
|
|
"position": "outer-middle"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"y2": {
|
|
|
|
"show": true,
|
|
|
|
"label": {
|
|
|
|
"text": "Signal",
|
|
|
|
"position": "outer-middle"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"transition": {
|
|
|
|
"duration": 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const outputFilePath = "output.png";
|
|
|
|
|
|
|
|
await c3ChartMaker(data, chartDefinition, outputFilePath);
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
/**
|
|
|
|
* Find element on page DOM.
|
|
|
|
*
|
|
|
|
* @example GET page, "elementName", "text"
|
|
|
|
*/
|
2022-05-29 15:16:38 -03:00
|
|
|
public async getBySelector(page, elementName) {
|
2022-05-19 10:22:22 -03:00
|
|
|
await page.waitForSelector(elementName)
|
|
|
|
let element = await page.$(elementName);
|
|
|
|
return element;
|
2022-05-03 17:05:57 -03:00
|
|
|
}
|
|
|
|
|
2022-05-29 15:16:38 -03:00
|
|
|
/**
|
|
|
|
* Find element on page DOM.
|
|
|
|
*
|
|
|
|
* @example GET page, "frameSelector, "elementSelector"
|
|
|
|
*/
|
2022-06-01 19:45:43 -03:00
|
|
|
public async getByFrame(page, frame, selector) {
|
2022-05-29 15:16:38 -03:00
|
|
|
await page.waitForSelector(frame)
|
|
|
|
let frameHandle = await page.$(frame);
|
|
|
|
const f = await frameHandle.contentFrame();
|
|
|
|
await f.waitForSelector(selector);
|
|
|
|
const element = await f.$(selector);
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
/**
|
2022-05-19 10:22:22 -03:00
|
|
|
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
|
2022-05-03 17:05:57 -03:00
|
|
|
*
|
|
|
|
* @example x = TODAY
|
|
|
|
*/
|
|
|
|
public async click(step, page, idOrName) {
|
2022-05-29 15:16:38 -03:00
|
|
|
const e = await this.getBySelector(page, idOrName);
|
2022-05-03 17:05:57 -03:00
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
page.waitForNavigation(),
|
|
|
|
page.click(e.name)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the screenshot of page or element
|
|
|
|
*
|
|
|
|
* @example file = SCREENSHOT page
|
|
|
|
*/
|
|
|
|
public async screenshot(step, page, idOrName, localName) {
|
2022-05-29 15:16:38 -03:00
|
|
|
const e = await this.getBySelector(page, idOrName);
|
2022-05-03 17:05:57 -03:00
|
|
|
await e.screenshot({ path: localName });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs the download to the .gbdrive Download folder.
|
|
|
|
*
|
|
|
|
* @example file = DOWNLOAD page, "tableName", row
|
|
|
|
*/
|
|
|
|
public async download(step, page, idOrName, localName) {
|
|
|
|
|
2022-05-29 15:16:38 -03:00
|
|
|
const e = await this.getBySelector(page, idOrName);
|
2022-05-03 17:05:57 -03:00
|
|
|
const context = await this.browser.newContext({ acceptDownloads: true });
|
|
|
|
|
|
|
|
var cells = e.rows[0].cells;
|
|
|
|
|
|
|
|
const [download] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
page.click(cells[0])
|
|
|
|
]);
|
|
|
|
|
|
|
|
const path = await download.path();
|
|
|
|
|
|
|
|
console.log(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Types the text into the text field.
|
|
|
|
*
|
|
|
|
* @example TYPE page, "elementName", "text"
|
|
|
|
*/
|
2022-05-29 15:16:38 -03:00
|
|
|
public async type(step, page, idOrName, text) {
|
|
|
|
const e = await this.getBySelector(page, idOrName);
|
2022-05-03 17:05:57 -03:00
|
|
|
await e.type(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
|
|
|
|
*
|
|
|
|
* @example x = TODAY
|
|
|
|
*/
|
|
|
|
public async getOCR(step, localFile) {
|
|
|
|
const tesseract = require("node-tesseract-ocr")
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
lang: "eng",
|
|
|
|
oem: 1,
|
|
|
|
psm: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
return await tesseract.recognize(localFile, config);
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example x = TODAY
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async getToday(step) {
|
2020-12-31 15:36:19 -03:00
|
|
|
let d = new Date(),
|
2020-12-27 13:30:56 -03:00
|
|
|
month = '' + (d.getMonth() + 1),
|
|
|
|
day = '' + d.getDate(),
|
|
|
|
year = d.getFullYear();
|
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
if (month.length < 2) { month = '0' + month; }
|
|
|
|
if (day.length < 2) { day = '0' + day; }
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2021-08-11 13:37:41 -03:00
|
|
|
const contentLocale = this.min.core.getParam<string>(
|
|
|
|
this.min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
2021-08-03 10:06:59 -03:00
|
|
|
|
2021-08-11 13:37:41 -03:00
|
|
|
switch (contentLocale) {
|
2021-07-16 08:12:58 -03:00
|
|
|
case 'pt':
|
2020-12-27 13:30:56 -03:00
|
|
|
return [day, month, year].join('/');
|
|
|
|
|
2021-07-16 08:12:58 -03:00
|
|
|
case 'en':
|
2020-12-27 13:30:56 -03:00
|
|
|
return [month, day, year].join('/');
|
|
|
|
|
|
|
|
default:
|
|
|
|
return [year, month, day].join('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Quits the dialog, currently required to get out of VM context.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example EXIT
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async exit(step) {
|
2022-05-03 17:05:57 -03:00
|
|
|
if (this.browser) {
|
|
|
|
await this.browser.close();
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
await step.endDialog();
|
|
|
|
}
|
|
|
|
|
2021-12-19 16:39:50 -03:00
|
|
|
/**
|
2021-12-20 18:27:10 -03:00
|
|
|
* Get active tasks.
|
2021-12-19 16:39:50 -03:00
|
|
|
*
|
|
|
|
* @example list = ACTIVE TASKS
|
|
|
|
*/
|
2021-12-20 18:27:10 -03:00
|
|
|
public async getActiveTasks() {
|
|
|
|
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
2021-12-19 16:39:50 -03:00
|
|
|
return await s.getActiveTasks();
|
|
|
|
}
|
|
|
|
|
2021-12-20 18:27:10 -03:00
|
|
|
/**
|
|
|
|
* Creates a new deal.
|
|
|
|
*
|
|
|
|
* @example CREATE DEAL dealname, contato, empresa, amount
|
|
|
|
*/
|
|
|
|
public async createDeal(dealName, contact, company, amount) {
|
|
|
|
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
|
|
|
let deal = await s.createDeal(dealName, contact, company, amount);
|
|
|
|
return deal;
|
|
|
|
}
|
|
|
|
|
2021-12-20 20:27:02 -03:00
|
|
|
/**
|
|
|
|
* Finds contacts in XRM.
|
|
|
|
*
|
|
|
|
* @example list = FIND CONTACT "Sandra"
|
|
|
|
*/
|
2022-05-03 17:05:57 -03:00
|
|
|
public async fndContact(name) {
|
2021-12-20 20:27:02 -03:00
|
|
|
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
|
|
|
|
return await s.searchContact(name);
|
|
|
|
}
|
|
|
|
|
2021-12-20 18:27:10 -03:00
|
|
|
|
2021-08-11 13:37:41 -03:00
|
|
|
public getContentLocaleWithCulture(contentLocale) {
|
|
|
|
switch (contentLocale) {
|
|
|
|
case 'pt':
|
2021-08-28 16:53:43 -03:00
|
|
|
return 'pt-BR';
|
2021-08-11 13:37:41 -03:00
|
|
|
|
|
|
|
case 'en':
|
2021-08-28 16:53:43 -03:00
|
|
|
return 'en-US';
|
2021-08-11 13:37:41 -03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return 'en-us';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-03 10:06:59 -03:00
|
|
|
/**
|
|
|
|
* Returns specified date week day in format 'Mon'.
|
|
|
|
*
|
|
|
|
* @example day = WEEKDAY (date)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public getWeekFromDate(date) {
|
2021-08-11 13:37:41 -03:00
|
|
|
|
|
|
|
const contentLocale = this.min.core.getParam<string>(
|
|
|
|
this.min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
|
|
|
|
2021-08-16 17:38:05 -03:00
|
|
|
let dt = SystemKeywords.getDateFromLocaleString(date, contentLocale);
|
2021-08-28 21:19:49 -03:00
|
|
|
GBLog.info(`BASIC WEEKDAY contentLocale: ${this.getContentLocaleWithCulture(contentLocale)}`);
|
|
|
|
GBLog.info(`BASIC WEEKDAY date: ${dt}`);
|
|
|
|
GBLog.info(dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' }));
|
|
|
|
|
2021-08-16 17:38:05 -03:00
|
|
|
if (dt) {
|
|
|
|
if (!(dt instanceof Date)) {
|
|
|
|
dt = new Date(dt);
|
|
|
|
}
|
|
|
|
let week = dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' });
|
2021-08-28 21:19:49 -03:00
|
|
|
return week.substr(0, 3);
|
2021-08-16 17:38:05 -03:00
|
|
|
}
|
|
|
|
return 'NULL';
|
2021-08-03 10:06:59 -03:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:24:59 -03:00
|
|
|
/**
|
2021-08-14 19:29:27 -03:00
|
|
|
* Returns an object ready to get information about difference in several ways
|
|
|
|
* like years, months or days.
|
2021-08-03 16:24:59 -03:00
|
|
|
*
|
2021-08-15 10:13:36 -03:00
|
|
|
* @example days = DATEDIFF date1, date2, mode
|
2021-08-03 16:24:59 -03:00
|
|
|
*
|
|
|
|
*/
|
2021-08-15 10:13:36 -03:00
|
|
|
public dateDiff(date1, date2, mode) {
|
2021-08-14 19:29:27 -03:00
|
|
|
let dt1 = date1;
|
|
|
|
let dt2 = date2;
|
|
|
|
if (!(dt1 instanceof Date)) {
|
|
|
|
dt1 = new Date(dt1);
|
2021-08-10 20:59:56 -03:00
|
|
|
}
|
2021-08-14 19:29:27 -03:00
|
|
|
if (!(dt2 instanceof Date)) {
|
|
|
|
dt2 = new Date(dt2);
|
2021-08-05 11:20:06 -03:00
|
|
|
}
|
2021-08-15 10:13:36 -03:00
|
|
|
const diff = new DateDiff(date1, date2);
|
|
|
|
switch (mode) {
|
|
|
|
case 'year': return diff.years();
|
|
|
|
case 'month': return diff.months();
|
|
|
|
case 'week': return diff.weeks();
|
|
|
|
case 'day': return diff.days();
|
|
|
|
case 'hour': return diff.hours();
|
|
|
|
case 'minute': return diff.minutes();
|
|
|
|
}
|
2021-08-14 19:29:27 -03:00
|
|
|
}
|
2021-08-05 11:20:06 -03:00
|
|
|
|
2021-08-14 19:29:27 -03:00
|
|
|
/**
|
|
|
|
* Returns specified date week day in format 'Mon'.
|
|
|
|
*
|
2021-08-15 10:13:36 -03:00
|
|
|
* @example DATEADD date, "minute", 60
|
2021-08-14 19:29:27 -03:00
|
|
|
*
|
|
|
|
* https://stackoverflow.com/a/1214753/18511
|
|
|
|
*/
|
2021-08-15 10:13:36 -03:00
|
|
|
public dateAdd(date, mode, units) {
|
2021-08-14 19:29:27 -03:00
|
|
|
let dateCopy = date;
|
|
|
|
if (!(dateCopy instanceof Date)) {
|
|
|
|
dateCopy = new Date(dateCopy);
|
|
|
|
}
|
|
|
|
var ret = new Date(dateCopy); //don't change original date
|
|
|
|
var checkRollover = function () { if (ret.getDate() != date.getDate()) ret.setDate(0); };
|
|
|
|
switch (String(mode).toLowerCase()) {
|
|
|
|
case 'year': ret.setFullYear(ret.getFullYear() + units); checkRollover(); break;
|
|
|
|
case 'quarter': ret.setMonth(ret.getMonth() + 3 * units); checkRollover(); break;
|
|
|
|
case 'month': ret.setMonth(ret.getMonth() + units); checkRollover(); break;
|
|
|
|
case 'week': ret.setDate(ret.getDate() + 7 * units); break;
|
|
|
|
case 'day': ret.setDate(ret.getDate() + units); break;
|
|
|
|
case 'hour': ret.setTime(ret.getTime() + units * 3600000); break;
|
|
|
|
case 'minute': ret.setTime(ret.getTime() + units * 60000); break;
|
|
|
|
case 'second': ret.setTime(ret.getTime() + units * 1000); break;
|
|
|
|
default: ret = undefined; break;
|
|
|
|
}
|
|
|
|
return ret;
|
2021-08-03 16:24:59 -03:00
|
|
|
}
|
|
|
|
|
2021-08-14 19:29:27 -03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns specified list member separated by comma.
|
|
|
|
*
|
|
|
|
* @example TALK TOLIST (array, member)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public getToLst(array, member) {
|
2021-08-15 10:13:36 -03:00
|
|
|
if (!array) {
|
|
|
|
return "<Empty>"
|
|
|
|
}
|
|
|
|
if (array[0] && array[0]['gbarray']) {
|
|
|
|
array = array.slice(1);
|
|
|
|
}
|
|
|
|
array = array.filter((v, i, a) => a.findIndex(t => (t[member] === v[member])) === i);
|
|
|
|
array = array.filter(function (item, pos) { return item != undefined; });
|
|
|
|
array = array.map((item) => { return item[member]; })
|
|
|
|
array = array.join(", ");
|
2021-08-14 19:29:27 -03:00
|
|
|
|
2021-08-15 10:13:36 -03:00
|
|
|
return array;
|
|
|
|
}
|
2021-08-14 19:29:27 -03:00
|
|
|
|
2021-08-03 10:06:59 -03:00
|
|
|
/**
|
|
|
|
* Returns the specified time in format hh:dd.
|
|
|
|
*
|
|
|
|
* @example hour = HOUR (date)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public getHourFromDate(date) {
|
2021-08-15 10:13:36 -03:00
|
|
|
function addZero(i) {
|
|
|
|
if (i < 10) {
|
|
|
|
i = "0" + i;
|
|
|
|
}
|
|
|
|
return i;
|
2021-08-03 17:35:02 -03:00
|
|
|
}
|
2021-08-16 17:38:05 -03:00
|
|
|
|
|
|
|
const contentLocale = this.min.core.getParam<string>(
|
|
|
|
this.min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
|
|
|
|
|
|
|
let dt = SystemKeywords.getDateFromLocaleString(date, contentLocale);
|
|
|
|
|
|
|
|
if (dt) {
|
|
|
|
if (!(dt instanceof Date)) {
|
|
|
|
dt = new Date(dt);
|
|
|
|
}
|
|
|
|
return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
|
|
|
|
}
|
|
|
|
return 'NULL';
|
2021-08-03 10:06:59 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns current time in format hh:dd.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example SAVE "file.xlsx", name, email, NOW
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async getNow() {
|
2021-08-15 10:13:36 -03:00
|
|
|
const contentLocale = this.min.core.getParam<string>(
|
|
|
|
this.min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
|
|
|
|
|
|
|
const nowUTC = new Date();
|
2022-05-03 17:05:57 -03:00
|
|
|
const now = typeof nowUTC === 'string' ?
|
2021-08-15 10:13:36 -03:00
|
|
|
new Date(nowUTC) :
|
2021-12-25 22:13:52 -03:00
|
|
|
nowUTC;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
|
|
|
const nowText = now.toLocaleString(this.getContentLocaleWithCulture(contentLocale),
|
|
|
|
{ timeZone: process.env.DEFAULT_TIMEZONE });
|
|
|
|
|
2021-12-25 22:13:52 -03:00
|
|
|
return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0];
|
2021-08-15 10:13:36 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Sends a file to a given mobile.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2021-03-08 19:14:12 -03:00
|
|
|
* @example SEND FILE TO "+199988887777", "image.jpg", caption
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2021-03-08 18:36:11 -03:00
|
|
|
public async sendFileTo(step, mobile, filename, caption) {
|
2021-08-15 10:13:36 -03:00
|
|
|
GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`);
|
|
|
|
return await this.internalSendFile(null, mobile, filename, caption);
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Sends a file to the current user.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example SEND FILE "image.jpg"
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async sendFile(step, filename, caption) {
|
2021-10-12 16:28:49 -03:00
|
|
|
const mobile = await this.userMobile(step);
|
2021-10-12 16:37:34 -03:00
|
|
|
GBLog.info(`BASIC: SEND FILE (current: ${mobile}, filename '${filename}'.`);
|
|
|
|
return await this.internalSendFile(step, mobile, filename, caption);
|
2021-08-15 10:13:36 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Defines the current language of the bot conversation.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example SET LANGUAGE "pt"
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async setLanguage(step, language) {
|
2021-08-15 10:13:36 -03:00
|
|
|
const user = await this.min.userProfile.get(step.context, {});
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2021-08-15 10:13:36 -03:00
|
|
|
const sec = new SecService();
|
|
|
|
user.systemUser = await sec.updateUserLocale(user.systemUser.userId, language);
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2021-08-15 10:13:36 -03:00
|
|
|
await this.min.userProfile.set(step.context, user);
|
|
|
|
this.user = user;
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2021-04-12 10:07:59 -03:00
|
|
|
/**
|
|
|
|
* Defines the maximum lines to scan in spreedsheets.
|
|
|
|
*
|
|
|
|
* @example SET MAX LINES 5000
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setMaxLines(step, count) {
|
2021-08-15 10:13:36 -03:00
|
|
|
const user = await this.min.userProfile.get(step.context, {});
|
|
|
|
user.basicOptions.maxLines = count;
|
|
|
|
await this.min.userProfile.set(step.context, user);
|
|
|
|
this.user = user;
|
|
|
|
}
|
2021-06-01 10:05:52 -03:00
|
|
|
|
2021-08-21 09:19:07 -03:00
|
|
|
/**
|
|
|
|
* Defines the FIND behaviour to consider whole words while searching.
|
|
|
|
*
|
|
|
|
* @example SET WHOLE WORD ON
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setWholeWord(step, on) {
|
|
|
|
const user = await this.min.userProfile.get(step.context, {});
|
|
|
|
user.basicOptions.wholeWord = (on.trim() === "on");
|
|
|
|
await this.min.userProfile.set(step.context, user);
|
|
|
|
this.user = user;
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:39:51 -03:00
|
|
|
/**
|
|
|
|
* Defines translator behaviour.
|
|
|
|
*
|
|
|
|
* @example SET TRANSLATOR ON | OFF
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setTranslatorOn(step, on) {
|
2021-08-15 10:13:36 -03:00
|
|
|
const user = await this.min.userProfile.get(step.context, {});
|
|
|
|
user.basicOptions.translatorOn = (on.trim() === "on");
|
|
|
|
await this.min.userProfile.set(step.context, user);
|
|
|
|
this.user = user;
|
|
|
|
}
|
2021-04-22 14:39:51 -03:00
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns the name of the user acquired by WhatsApp API.
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async userName(step) {
|
2021-12-31 09:39:23 -03:00
|
|
|
return step ? WhatsappDirectLine.usernames[await this.userMobile(step)] : 'N/A';
|
2021-08-15 10:13:36 -03:00
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* OBSOLETE.
|
|
|
|
*/
|
|
|
|
public async getFrom(step) {
|
2021-08-15 10:13:36 -03:00
|
|
|
return step ? await this.userMobile(step) : 'N/A';
|
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns current mobile number from user in conversation.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example SAVE "file.xlsx", name, email, MOBILE
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async userMobile(step) {
|
2021-10-13 09:39:24 -03:00
|
|
|
return GBMinService.userMobile(step);
|
2020-12-27 13:30:56 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Shows the subject menu to the user
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example MENU
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async showMenu(step) {
|
2021-08-15 10:13:36 -03:00
|
|
|
return await step.beginDialog('/menu');
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Performs the transfer of the conversation to a human agent.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example TRANSFER
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2022-05-19 12:17:28 -03:00
|
|
|
public async transferTo(step, to: string = null) {
|
2022-05-29 15:16:38 -03:00
|
|
|
return await step.beginDialog('/t', { to: to });
|
2021-08-15 10:13:36 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Hears something from user and put it in a variable
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
* @example HEAR name
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async hear(step, promise, previousResolve, kind, ...args) {
|
2021-08-15 10:13:36 -03:00
|
|
|
function random(low, high) {
|
|
|
|
return Math.random() * (high - low) + low;
|
|
|
|
}
|
|
|
|
const idPromise = random(0, 120000000);
|
|
|
|
this.min.cbMap[idPromise] = {};
|
|
|
|
this.min.cbMap[idPromise].promise = promise;
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
let opts = { id: idPromise, previousResolve: previousResolve, kind: kind, args };
|
|
|
|
|
|
|
|
if (this.hrOn) {
|
|
|
|
|
|
|
|
let sleep = ms => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Waits for next message in HEAR delegated context.
|
2022-05-29 15:16:38 -03:00
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
const mobile = await this.userMobile(step);
|
2022-05-29 15:16:38 -03:00
|
|
|
while (true) {
|
|
|
|
if (WhatsappDirectLine.state[mobile] === 3) {
|
2022-05-03 17:05:57 -03:00
|
|
|
break;
|
|
|
|
}
|
2022-05-29 15:16:38 -03:00
|
|
|
sleep(5000);
|
2022-05-03 17:05:57 -03:00
|
|
|
}
|
|
|
|
const result = WhatsappDirectLine.lastMessage[mobile];
|
|
|
|
opts = await promise(step, result);
|
|
|
|
|
|
|
|
if (previousResolve !== undefined) {
|
|
|
|
previousResolve(opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (previousResolve !== undefined) {
|
|
|
|
previousResolve(opts);
|
|
|
|
} else {
|
|
|
|
await step.beginDialog('/hear', opts);
|
|
|
|
}
|
2021-08-15 10:13:36 -03:00
|
|
|
}
|
2021-08-14 19:29:27 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2021-09-13 18:36:26 -03:00
|
|
|
/**
|
|
|
|
* Prepares the next dialog to be shown to the specified user.
|
|
|
|
*/
|
2021-09-18 09:41:18 -03:00
|
|
|
public async gotoDialog(step, fromOrDialogName: string, dialogName: string) {
|
2021-09-13 18:36:26 -03:00
|
|
|
if (dialogName) {
|
2021-09-18 09:41:18 -03:00
|
|
|
if (dialogName.charAt(0) === '/') {
|
|
|
|
await step.beginDialog(fromOrDialogName);
|
|
|
|
} else {
|
|
|
|
let sec = new SecService();
|
|
|
|
let user = await sec.getUserFromSystemId(fromOrDialogName);
|
|
|
|
if (!user) {
|
|
|
|
user = await sec.ensureUser(this.min.instance.instanceId, fromOrDialogName,
|
|
|
|
fromOrDialogName, null, 'whatsapp', 'from', null);
|
|
|
|
}
|
|
|
|
await sec.updateUserHearOnDialog(user.userId, dialogName);
|
2021-09-13 18:36:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-09-18 09:41:18 -03:00
|
|
|
await step.beginDialog(fromOrDialogName);
|
2021-09-13 18:36:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Talks to the user by using the specified text.
|
|
|
|
*/
|
2020-12-27 13:30:56 -03:00
|
|
|
public async talk(step, text: string) {
|
2021-08-15 10:13:36 -03:00
|
|
|
await this.min.conversationalService['sendTextWithOptions'](this.min, step, text,
|
|
|
|
this.user.basicOptions.translatorOn, null);
|
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2021-06-01 10:05:52 -03:00
|
|
|
private static getChannel(step): string {
|
2022-05-29 15:16:38 -03:00
|
|
|
if (!step) return 'whatsapp';
|
2021-08-15 10:13:36 -03:00
|
|
|
if (!isNaN(step.context.activity['mobile'])) {
|
|
|
|
return 'webchat';
|
|
|
|
} else {
|
|
|
|
if (step.context.activity.from && !isNaN(step.context.activity.from.id)) {
|
|
|
|
return 'whatsapp';
|
|
|
|
}
|
|
|
|
return 'webchat';
|
2021-08-03 10:06:59 -03:00
|
|
|
}
|
2021-06-01 10:05:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
/**
|
|
|
|
* Processes the sending of the file.
|
|
|
|
*/
|
|
|
|
private async internalSendFile(step, mobile, filename, caption) {
|
2022-05-29 15:16:38 -03:00
|
|
|
|
|
|
|
// Handles SEND FILE TO mobile, element in Web Automation.
|
|
|
|
|
|
|
|
const page = filename._page;
|
|
|
|
if (page) {
|
|
|
|
const gbaiName = `${this.min.botId}.gbai`;
|
2022-06-01 19:45:43 -03:00
|
|
|
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
2022-05-29 15:16:38 -03:00
|
|
|
await filename.screenshot({ path: localName });
|
2022-06-01 19:45:43 -03:00
|
|
|
|
2022-05-29 15:16:38 -03:00
|
|
|
const url = urlJoin(
|
|
|
|
GBServer.globals.publicAddress,
|
|
|
|
this.min.botId,
|
|
|
|
'cache',
|
|
|
|
Path.basename(localName)
|
|
|
|
);
|
|
|
|
|
|
|
|
GBLog.info(`BASIC: WebAutomation: Sending the file ${url} to mobile ${mobile}.`);
|
|
|
|
await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles Markdown.
|
|
|
|
|
2022-06-01 19:45:43 -03:00
|
|
|
else if (filename.indexOf('.md') > -1) {
|
2021-08-15 10:13:36 -03:00
|
|
|
GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
|
|
|
|
const md = await this.min.kbService.getAnswerTextByMediaName(this.min.instance.instanceId, filename);
|
|
|
|
if (!md) {
|
|
|
|
GBLog.info(`BASIC: Markdown file ${filename} not found on database for ${this.min.instance.botId}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.min.conversationalService['playMarkdown'](this.min, md,
|
2021-10-12 16:13:47 -03:00
|
|
|
DialogKeywords.getChannel(step), step, mobile);
|
2022-05-29 15:16:38 -03:00
|
|
|
|
2021-08-15 10:13:36 -03:00
|
|
|
} else {
|
|
|
|
GBLog.info(`BASIC: Sending the file ${filename} to mobile ${mobile}.`);
|
|
|
|
const url = urlJoin(
|
|
|
|
GBServer.globals.publicAddress,
|
|
|
|
'kb',
|
|
|
|
`${this.min.botId}.gbai`,
|
|
|
|
`${this.min.botId}.gbkb`,
|
|
|
|
'assets',
|
|
|
|
filename
|
|
|
|
);
|
|
|
|
|
|
|
|
await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
|
2020-12-31 15:36:19 -03:00
|
|
|
}
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
}
|