2020-12-27 13:30:56 -03:00
|
|
|
/*****************************************************************************\
|
2024-08-17 20:30:00 -03:00
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
2020-12-27 13:30:56 -03:00
|
|
|
| |
|
2024-08-17 20:30:00 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
|
2020-12-27 13:30:56 -03:00
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
2024-08-17 20:30:00 -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, |
|
2020-12-27 13:30:56 -03:00
|
|
|
| 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. |
|
|
|
|
| |
|
2024-08-17 20:30:00 -03:00
|
|
|
| This program is distributed in the hope that it will be useful, |
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2020-12-27 13:30:56 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
2024-08-17 20:30:00 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.cloud. |
|
2020-12-27 13:30:56 -03:00
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
2024-08-17 20:30:00 -03:00
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
2020-12-27 13:30:56 -03:00
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
import { GBLog, GBMinInstance } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
|
|
|
|
import { ChartServices } from './ChartServices.js';
|
|
|
|
import urlJoin from 'url-join';
|
|
|
|
import { GBServer } from '../../../src/app.js';
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
|
|
|
|
import { SecService } from '../../security.gbapp/services/SecService.js';
|
|
|
|
import { SystemKeywords } from './SystemKeywords.js';
|
|
|
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
|
|
|
|
import { Messages } from '../strings.js';
|
2022-10-22 13:23:32 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService.js';
|
2024-09-06 15:30:03 -03:00
|
|
|
import fs from 'fs';
|
2023-07-14 18:51:36 -03:00
|
|
|
import libphonenumber from 'google-libphonenumber';
|
2023-12-18 11:14:38 -03:00
|
|
|
import * as df from 'date-diff';
|
2022-11-18 22:39:14 -03:00
|
|
|
import tesseract from 'node-tesseract-ocr';
|
2024-09-06 15:30:03 -03:00
|
|
|
import path from 'path';
|
2022-11-18 22:39:14 -03:00
|
|
|
import sgMail from '@sendgrid/mail';
|
|
|
|
import mammoth from 'mammoth';
|
|
|
|
import qrcode from 'qrcode';
|
2023-02-04 10:57:05 -03:00
|
|
|
import { WebAutomationServices } from './WebAutomationServices.js';
|
2023-03-02 17:46:45 -03:00
|
|
|
import QrScanner from 'qr-scanner';
|
2023-03-06 20:36:27 -03:00
|
|
|
import pkg from 'whatsapp-web.js';
|
2023-03-26 19:33:58 -03:00
|
|
|
import { ActivityTypes } from 'botbuilder';
|
2023-03-06 20:36:27 -03:00
|
|
|
const { List, Buttons } = pkg;
|
2023-05-25 21:20:40 -03:00
|
|
|
import mime from 'mime-types';
|
2023-08-23 11:24:48 -03:00
|
|
|
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
|
2024-01-10 15:01:02 -03:00
|
|
|
import { GBUtil } from '../../../src/util.js';
|
2024-03-03 16:20:50 -03:00
|
|
|
import { GBVMService } from './GBVMService.js';
|
2024-09-05 01:23:49 -03:00
|
|
|
import { ChatServices } from '../../../packages/llm.gblib/services/ChatServices.js';
|
|
|
|
import puppeteer from 'puppeteer';
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2023-01-31 11:46:37 -03:00
|
|
|
/**
|
|
|
|
* Default check interval for user replay
|
|
|
|
*/
|
|
|
|
const DEFAULT_HEAR_POLL_INTERVAL = 500;
|
2024-04-17 16:07:19 -03:00
|
|
|
const API_RETRIES = 120;
|
2020-12-27 13:30:56 -03:00
|
|
|
|
|
|
|
/**
|
2022-11-01 00:59:35 -03:00
|
|
|
* Base services of conversation to be called by BASIC.
|
2020-12-27 13:30:56 -03:00
|
|
|
*/
|
|
|
|
export class DialogKeywords {
|
2024-09-05 01:23:49 -03:00
|
|
|
public async llmChart({ pid, data, prompt }) {
|
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
|
|
|
|
// The prompt for the LLM, including the data.
|
|
|
|
|
|
|
|
const llmPrompt = `
|
|
|
|
You are given the following data: ${JSON.stringify(data)}.
|
|
|
|
|
|
|
|
Based on this data, generate a configuration for a Billboard.js chart. The output should be valid JSON, following Billboard.js conventions. Ensure the JSON is returned without markdown formatting, explanations, or comments.
|
|
|
|
|
2024-09-06 15:15:42 -03:00
|
|
|
The chart should be ${prompt}. Return only the one-line only JSON configuration, nothing else.`;
|
2024-09-05 01:23:49 -03:00
|
|
|
|
|
|
|
// Send the prompt to the LLM and get the response
|
|
|
|
|
|
|
|
const response = await ChatServices.invokeLLM(min, llmPrompt);
|
|
|
|
const args = JSON.parse(response.content); // Ensure the LLM generates valid JSON
|
|
|
|
|
|
|
|
// Launch Puppeteer to render the chart
|
|
|
|
|
|
|
|
const browser = await puppeteer.launch();
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
|
|
|
// Load Billboard.js styles and scripts
|
|
|
|
|
|
|
|
await page.addStyleTag({ url: 'https://cdn.jsdelivr.net/npm/billboard.js/dist/theme/datalab.min.css' });
|
|
|
|
await page.addScriptTag({ url: 'https://cdn.jsdelivr.net/npm/billboard.js/dist/billboard.pkgd.min.js' });
|
|
|
|
|
|
|
|
// Pass the args to render the chart
|
|
|
|
|
|
|
|
await page.evaluate(`bb.generate(${JSON.stringify(args)});`);
|
|
|
|
|
|
|
|
// Get the chart container and take a screenshot
|
|
|
|
|
|
|
|
const content = await page.$('.bb');
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2024-09-06 15:30:03 -03:00
|
|
|
const localName = path.join('work', gbaiName, 'cache', `chart${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
2024-09-05 01:23:49 -03:00
|
|
|
await content.screenshot({ path: localName, omitBackground: true });
|
|
|
|
await browser.close();
|
2024-09-06 15:30:03 -03:00
|
|
|
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
|
2024-09-05 01:23:49 -03:00
|
|
|
GBLogEx.info(min, `Visualization: Chart generated at ${url}.`);
|
|
|
|
|
|
|
|
return { localName, url };
|
|
|
|
}
|
2022-06-03 07:43:20 -03:00
|
|
|
/**
|
2022-11-19 23:34:58 -03:00
|
|
|
*
|
|
|
|
* Data = [10,20,30]
|
|
|
|
* Legends = "Steve;Yui;Carlos"
|
|
|
|
* img = CHART "pie",data,legends
|
|
|
|
*
|
2022-06-03 07:43:20 -03:00
|
|
|
* https://c3js.org/examples.html
|
2022-12-26 07:51:41 -03:00
|
|
|
* https://c3js.org/samples/timeseries.html (used here)
|
2022-11-19 23:34:58 -03:00
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @param legends
|
2022-06-03 07:43:20 -03:00
|
|
|
* @see https://www.npmjs.com/package/plot
|
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async chart({ pid, type, data, legends, transpose }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-07-06 12:36:51 -03:00
|
|
|
let table = [[]];
|
2022-06-03 15:09:24 -03:00
|
|
|
|
2022-07-06 12:36:51 -03:00
|
|
|
if (legends) {
|
|
|
|
const legends_ = legends.split(';');
|
|
|
|
|
|
|
|
// Columns and data are merged like:
|
|
|
|
// columns: [
|
2022-11-02 19:40:59 -03:00
|
|
|
// ['data1',30,200,100,400,150,250],
|
|
|
|
// ['data2',50,20,10,40,15,25]
|
2022-07-06 12:36:51 -03:00
|
|
|
// ]
|
|
|
|
|
|
|
|
for (let i = 0; i < legends_.length; i++) {
|
|
|
|
table[i] = [legends_[i]];
|
|
|
|
table[i] = table[i].concat(data);
|
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2022-07-06 12:36:51 -03:00
|
|
|
table = SystemKeywords.JSONAsGBTable(data, false);
|
|
|
|
table.shift();
|
2022-06-03 15:09:24 -03:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:36:51 -03:00
|
|
|
if (transpose) {
|
2022-11-19 23:34:58 -03:00
|
|
|
const transpose = array => {
|
|
|
|
return array.reduce((prev, next) => next.map((item, i) => (prev[i] || []).concat(next[i])), []);
|
|
|
|
};
|
2022-07-06 12:36:51 -03:00
|
|
|
table = transpose(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
let definition = {
|
2022-06-03 15:09:24 -03:00
|
|
|
size: {
|
2022-11-19 23:34:58 -03:00
|
|
|
height: 420,
|
|
|
|
width: 680
|
2022-06-03 07:43:20 -03:00
|
|
|
},
|
2022-06-03 15:09:24 -03:00
|
|
|
data: {
|
2022-07-06 12:36:51 -03:00
|
|
|
columns: table,
|
2022-06-03 15:09:24 -03:00
|
|
|
type: type
|
2022-06-03 07:43:20 -03:00
|
|
|
},
|
2022-06-03 15:09:24 -03:00
|
|
|
bar: {
|
2022-07-06 12:36:51 -03:00
|
|
|
ratio: 0.5
|
2022-06-03 07:43:20 -03:00
|
|
|
}
|
2022-06-03 15:09:24 -03:00
|
|
|
};
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2022-07-06 12:36:51 -03:00
|
|
|
if (type === 'timeseries') {
|
|
|
|
definition['axis'][table[0]] = {
|
|
|
|
type: 'timeseries',
|
|
|
|
tick: {
|
|
|
|
format: '%Y-%m-%d'
|
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
};
|
2022-07-06 12:36:51 -03:00
|
|
|
}
|
|
|
|
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2024-09-06 15:30:03 -03:00
|
|
|
const localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
2022-06-03 15:09:24 -03:00
|
|
|
|
|
|
|
await ChartServices.screenshot(definition, localName);
|
|
|
|
|
2024-09-06 15:30:03 -03:00
|
|
|
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
|
2022-06-03 15:09:24 -03:00
|
|
|
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Visualization: Chart generated at ${url}.`);
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2022-06-03 15:09:24 -03:00
|
|
|
return url;
|
2022-06-03 07:43:20 -03:00
|
|
|
}
|
|
|
|
|
2022-05-03 17:05:57 -03:00
|
|
|
/**
|
2022-11-02 16:03:25 -03:00
|
|
|
* Returns the OCR of image file.
|
2022-05-03 17:05:57 -03:00
|
|
|
*
|
|
|
|
*/
|
2024-04-30 15:05:31 -03:00
|
|
|
public async getOCR({ pid, localFile }) {
|
2024-04-21 23:39:39 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `OCR processing on ${localFile}.`);
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
const config = {
|
2022-11-19 23:34:58 -03:00
|
|
|
lang: 'eng',
|
2022-06-21 17:46:46 -03:00
|
|
|
oem: 1,
|
2022-11-19 23:34:58 -03:00
|
|
|
psm: 3
|
|
|
|
};
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
return await tesseract.recognize(localFile, config);
|
|
|
|
}
|
2022-06-17 19:50:26 -03:00
|
|
|
|
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
|
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async getToday({ pid }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-06-21 17:46:46 -03:00
|
|
|
let d = new Date(),
|
|
|
|
month = '' + (d.getMonth() + 1),
|
|
|
|
day = '' + d.getDate(),
|
|
|
|
year = d.getFullYear();
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (month.length < 2) {
|
|
|
|
month = '0' + month;
|
|
|
|
}
|
|
|
|
if (day.length < 2) {
|
|
|
|
day = '0' + day;
|
|
|
|
}
|
2022-06-21 17:46:46 -03:00
|
|
|
|
2023-03-04 16:27:25 -03:00
|
|
|
const contentLocale = min.core.getParam(
|
|
|
|
min.instance,
|
2022-06-21 17:46:46 -03:00
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
switch (contentLocale) {
|
|
|
|
case 'pt':
|
|
|
|
return [day, month, year].join('/');
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
case 'en':
|
|
|
|
return [month, day, year].join('/');
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
default:
|
|
|
|
return [year, month, day].join('/');
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
2022-11-02 19:40:59 -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
|
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async exit({}) {}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
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
|
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async getActiveTasks({ pid }) {}
|
2021-12-19 16:39:50 -03:00
|
|
|
|
2021-12-20 18:27:10 -03:00
|
|
|
/**
|
|
|
|
* Creates a new deal.
|
|
|
|
*
|
2022-11-02 19:40:59 -03:00
|
|
|
* @example CREATE DEAL dealname,contato,empresa,amount
|
2021-12-20 18:27:10 -03:00
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async createDeal({ pid, dealName, contact, company, amount }) {}
|
2021-12-20 18:27:10 -03:00
|
|
|
|
2021-12-20 20:27:02 -03:00
|
|
|
/**
|
|
|
|
* Finds contacts in XRM.
|
|
|
|
*
|
|
|
|
* @example list = FIND CONTACT "Sandra"
|
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async fndContact({ pid, name }) {}
|
2021-12-20 20:27:02 -03:00
|
|
|
|
2022-12-14 08:23:39 -03:00
|
|
|
public getContentLocaleWithCulture(contentLocale) {
|
2022-06-21 17:46:46 -03:00
|
|
|
switch (contentLocale) {
|
|
|
|
case 'pt':
|
|
|
|
return 'pt-BR';
|
2021-08-11 13:37:41 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
case 'en':
|
|
|
|
return 'en-US';
|
2021-08-11 13:37:41 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
default:
|
|
|
|
return 'en-us';
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2023-01-13 16:09:45 -03:00
|
|
|
public async getCoded({ pid, value }) {
|
2022-08-05 00:10:23 -03:00
|
|
|
// Checks if it is a GB FILE object.
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
if (value.data && value.filename) {
|
2022-08-05 00:10:23 -03:00
|
|
|
value = value.data;
|
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
return Buffer.from(value).toString('base64');
|
2022-08-01 20:36:38 -03:00
|
|
|
}
|
|
|
|
|
2021-08-03 10:06:59 -03:00
|
|
|
/**
|
|
|
|
* Returns specified date week day in format 'Mon'.
|
|
|
|
*
|
2022-11-19 23:34:58 -03:00
|
|
|
* @example day = WEEKDAY (date)
|
2021-08-03 10:06:59 -03:00
|
|
|
*
|
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async getWeekFromDate({ pid, date }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
const contentLocale = min.core.getParam(
|
|
|
|
min.instance,
|
2022-06-21 17:46:46 -03:00
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
2021-08-11 13:37:41 -03:00
|
|
|
|
2023-01-13 16:09:45 -03:00
|
|
|
let dt = SystemKeywords.getDateFromLocaleString(pid, date, contentLocale);
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `BASIC WEEKDAY contentLocale: ${this.getContentLocaleWithCulture(contentLocale)}`);
|
|
|
|
GBLogEx.info(min, `BASIC WEEKDAY date: ${dt}`);
|
|
|
|
GBLogEx.info(min, dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' }));
|
2021-08-28 21:19:49 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
if (dt) {
|
|
|
|
if (!(dt instanceof Date)) {
|
|
|
|
dt = new Date(dt);
|
|
|
|
}
|
|
|
|
let week = dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' });
|
|
|
|
return week.substr(0, 3);
|
2021-08-16 17:38:05 -03:00
|
|
|
}
|
2022-06-21 17:46:46 -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
|
2022-11-02 19:40:59 -03:00
|
|
|
* like years,months or days.
|
2021-08-03 16:24:59 -03:00
|
|
|
*
|
2022-11-02 19:40:59 -03:00
|
|
|
* @example days = DATEDIFF date1,date2,mode
|
2021-08-03 16:24:59 -03:00
|
|
|
*
|
|
|
|
*/
|
2024-01-11 14:42:00 -03:00
|
|
|
public async getDateDiff({ pid, date1, date2, mode }) {
|
2022-06-21 17:46:46 -03:00
|
|
|
let dt1 = date1;
|
|
|
|
let dt2 = date2;
|
|
|
|
if (!(dt1 instanceof Date)) {
|
|
|
|
dt1 = new Date(dt1);
|
|
|
|
}
|
|
|
|
if (!(dt2 instanceof Date)) {
|
|
|
|
dt2 = new Date(dt2);
|
|
|
|
}
|
2023-12-18 11:14:38 -03:00
|
|
|
const diff1 = df.default.constructor(date1, date2);
|
|
|
|
const diff = Date['diff'](date1, date2);
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
switch (mode) {
|
2022-11-19 23:34:58 -03:00
|
|
|
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();
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2022-06-17 19:50:26 -03:00
|
|
|
}
|
2021-08-05 11:20:06 -03:00
|
|
|
|
2024-01-11 14:42:00 -03:00
|
|
|
// https://weblog.west-wind.com/posts/2008/Mar/18/A-simple-formatDate-function-for-JavaScript
|
2024-03-03 16:20:50 -03:00
|
|
|
public async format({ pid, value, format }) {
|
2024-01-13 14:23:04 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
const contentLocale = min.core.getParam(
|
|
|
|
min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!(value instanceof Date)) {
|
2024-03-03 16:20:50 -03:00
|
|
|
value = SystemKeywords.getDateFromLocaleString(pid, value, contentLocale);
|
2024-01-13 14:23:04 -03:00
|
|
|
}
|
2024-03-03 16:20:50 -03:00
|
|
|
var date: any = new Date(value); //don't change original date
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
if (!format) format = 'MM/dd/yyyy';
|
2024-01-11 14:42:00 -03:00
|
|
|
|
|
|
|
var month = date.getMonth() + 1;
|
|
|
|
var year = date.getFullYear();
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
format = format.replace('MM', GBUtil.padL(month.toString(), 2, '0'));
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
if (format.indexOf('yyyy') > -1) format = format.replace('yyyy', year.toString());
|
|
|
|
else if (format.indexOf('yy') > -1) format = format.replace('yy', year.toString().substr(2, 2));
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
format = format.replace('dd', GBUtil.padL(date.getDate().toString(), 2, '0'));
|
2024-01-11 14:42:00 -03:00
|
|
|
|
|
|
|
var hours = date.getHours();
|
2024-08-04 17:16:04 -03:00
|
|
|
if (format.indexOf('t') > -1) {
|
|
|
|
if (hours > 11) format = format.replace('t', 'pm');
|
|
|
|
else format = format.replace('t', 'am');
|
2024-01-11 14:42:00 -03:00
|
|
|
}
|
2024-08-04 17:16:04 -03:00
|
|
|
if (format.indexOf('HH') > -1) format = format.replace('HH', GBUtil.padL(hours.toString(), 2, '0'));
|
|
|
|
if (format.indexOf('hh') > -1) {
|
2024-01-11 14:42:00 -03:00
|
|
|
if (hours > 12) hours - 12;
|
|
|
|
if (hours == 0) hours = 12;
|
2024-08-04 17:16:04 -03:00
|
|
|
format = format.replace('hh', hours.toString().padL(2, '0'));
|
2024-01-11 14:42:00 -03:00
|
|
|
}
|
2024-08-04 17:16:04 -03:00
|
|
|
if (format.indexOf('mm') > -1) format = format.replace('mm', GBUtil.padL(date.getMinutes().toString(), 2, '0'));
|
|
|
|
if (format.indexOf('ss') > -1) format = format.replace('ss', GBUtil.padL(date.getSeconds().toString(), 2, '0'));
|
2024-01-13 14:23:04 -03:00
|
|
|
|
2024-01-11 14:42:00 -03:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2021-08-14 19:29:27 -03:00
|
|
|
/**
|
|
|
|
* Returns specified date week day in format 'Mon'.
|
|
|
|
*
|
2022-11-19 23:34:58 -03:00
|
|
|
* @example DATEADD date,"minute",60
|
|
|
|
*
|
2021-08-14 19:29:27 -03:00
|
|
|
* https://stackoverflow.com/a/1214753/18511
|
|
|
|
*/
|
2024-03-03 16:20:50 -03:00
|
|
|
public async dateAdd({ pid, date, mode, units }) {
|
2024-01-13 14:23:04 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
const contentLocale = min.core.getParam(
|
|
|
|
min.instance,
|
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
let dateCopy = date;
|
|
|
|
if (!(dateCopy instanceof Date)) {
|
2024-03-03 16:20:50 -03:00
|
|
|
dateCopy = SystemKeywords.getDateFromLocaleString(pid, dateCopy, contentLocale);
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
|
|
|
var ret = new Date(dateCopy); //don't change original date
|
2022-11-19 23:34:58 -03:00
|
|
|
var checkRollover = function () {
|
|
|
|
if (ret.getDate() != date.getDate()) ret.setDate(0);
|
|
|
|
};
|
2022-06-21 17:46:46 -03:00
|
|
|
switch (String(mode).toLowerCase()) {
|
2022-11-19 23:34:58 -03:00
|
|
|
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':
|
2024-01-11 14:42:00 -03:00
|
|
|
case 'days':
|
|
|
|
case 'd':
|
2022-11-19 23:34:58 -03:00
|
|
|
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;
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
*
|
2022-11-19 23:34:58 -03:00
|
|
|
* @example TALK TOLIST (array,member)
|
2021-08-14 19:29:27 -03:00
|
|
|
*
|
|
|
|
*/
|
2024-09-05 01:23:49 -03:00
|
|
|
public async getToLst({ pid, array, member }) {
|
2023-01-31 11:46:37 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
if (!array) {
|
2022-11-19 23:34:58 -03:00
|
|
|
return '<Empty>';
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
|
|
|
if (array[0] && array[0]['gbarray']) {
|
|
|
|
array = array.slice(1);
|
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
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(',');
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2022-06-21 17:46:46 -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)
|
|
|
|
*
|
|
|
|
*/
|
2023-01-25 10:49:00 -03:00
|
|
|
public async getHourFromDate(pid, date) {
|
2023-01-31 11:46:37 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
|
2022-12-14 08:23:39 -03:00
|
|
|
function addZero(i) {
|
2022-06-21 17:46:46 -03:00
|
|
|
if (i < 10) {
|
2022-11-19 23:34:58 -03:00
|
|
|
i = '0' + i;
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
|
|
|
return i;
|
2021-08-03 17:35:02 -03:00
|
|
|
}
|
2021-08-16 17:38:05 -03:00
|
|
|
|
2023-03-04 16:27:25 -03:00
|
|
|
const contentLocale = min.core.getParam(
|
2023-01-25 10:49:00 -03:00
|
|
|
min.instance,
|
2022-06-21 17:46:46 -03:00
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
2021-08-16 17:38:05 -03:00
|
|
|
|
2023-01-13 16:09:45 -03:00
|
|
|
let dt = SystemKeywords.getDateFromLocaleString(pid, date, contentLocale);
|
2021-08-16 17:38:05 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
if (dt) {
|
|
|
|
if (!(dt instanceof Date)) {
|
|
|
|
dt = new Date(dt);
|
|
|
|
}
|
|
|
|
return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
|
2021-08-16 17:38:05 -03:00
|
|
|
}
|
2022-06-21 17:46:46 -03:00
|
|
|
return 'NULL';
|
2021-08-03 10:06:59 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
2023-04-01 08:01:10 -03:00
|
|
|
* Returns current time in format hh:mm.
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2023-04-01 08:01:10 -03:00
|
|
|
* @example NOW
|
2020-12-31 15:36:19 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async getNow({ pid }) {
|
2023-03-04 16:27:25 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
const contentLocale = min.core.getParam(
|
|
|
|
min.instance,
|
2022-06-21 17:46:46 -03:00
|
|
|
'Default Content Language',
|
|
|
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
|
|
|
);
|
2021-08-15 10:13:36 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
const nowUTC = new Date();
|
2022-11-19 23:34:58 -03:00
|
|
|
const now = typeof nowUTC === 'string' ? new Date(nowUTC) : nowUTC;
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
const nowText = now.toLocaleString(this.getContentLocaleWithCulture(contentLocale), {
|
|
|
|
timeZone: process.env.DEFAULT_TIMEZONE
|
|
|
|
});
|
2022-05-03 17:05:57 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0];
|
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2022-06-30 12:53:42 -03:00
|
|
|
/**
|
|
|
|
* Sends an e-mail.
|
2022-11-19 23:34:58 -03:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
*
|
2022-11-02 19:40:59 -03:00
|
|
|
* SEND MAIL "email@domain.com","Subject", "Message text."
|
2022-11-19 23:34:58 -03:00
|
|
|
*
|
2022-06-30 12:53:42 -03:00
|
|
|
*/
|
2023-01-13 16:09:45 -03:00
|
|
|
public async sendEmail({ pid, to, subject, body }) {
|
2024-04-21 23:39:39 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2024-04-30 15:05:31 -03:00
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
if (!process.env.EMAIL_FROM) {
|
|
|
|
return;
|
2024-06-09 10:32:49 -03:00
|
|
|
}
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
if (!body) {
|
|
|
|
body = '';
|
|
|
|
}
|
2024-04-02 17:52:17 -03:00
|
|
|
|
2022-06-30 12:53:42 -03:00
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `[E-mail]: to:${to},subject: ${subject},body: ${body}.`);
|
2022-06-30 12:53:42 -03:00
|
|
|
const emailToken = process.env.EMAIL_API_KEY;
|
|
|
|
|
2022-07-12 13:30:12 -03:00
|
|
|
// Inline word document used as e-mail body.
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (typeof body === 'object') {
|
2022-07-15 09:05:16 -03:00
|
|
|
const result = await mammoth.convertToHtml({ buffer: body });
|
2022-07-12 13:30:12 -03:00
|
|
|
body = result.value;
|
|
|
|
}
|
2022-07-15 09:05:16 -03:00
|
|
|
|
2024-04-30 15:05:31 -03:00
|
|
|
if (emailToken) {
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
|
sgMail.setApiKey(emailToken);
|
|
|
|
const msg = {
|
|
|
|
to: to,
|
|
|
|
from: process.env.EMAIL_FROM,
|
|
|
|
subject: subject,
|
|
|
|
text: body,
|
|
|
|
html: body
|
|
|
|
};
|
|
|
|
sgMail.send(msg, false, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
|
|
|
});
|
2022-06-30 12:53:42 -03:00
|
|
|
});
|
2024-08-04 17:16:04 -03:00
|
|
|
} else {
|
2024-04-30 15:05:31 -03:00
|
|
|
let { client } = await GBDeployer.internalGetDriveClient(min);
|
|
|
|
|
|
|
|
const data = {
|
2024-08-04 17:16:04 -03:00
|
|
|
message: {
|
|
|
|
subject: subject,
|
|
|
|
body: {
|
|
|
|
contentType: 'Text',
|
|
|
|
content: body
|
2024-04-30 15:05:31 -03:00
|
|
|
},
|
2024-08-04 17:16:04 -03:00
|
|
|
toRecipients: [
|
2024-04-30 15:05:31 -03:00
|
|
|
{
|
2024-08-04 17:16:04 -03:00
|
|
|
emailAddress: {
|
|
|
|
address: to
|
2024-04-30 15:05:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2024-08-04 17:16:04 -03:00
|
|
|
from: {
|
|
|
|
emailAddress: {
|
|
|
|
address: process.env.EMAIL_FROM
|
2024-04-30 15:05:31 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
await client.api('/me/sendMail').post(data);
|
2024-04-30 15:05:31 -03:00
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
GBLogEx.info(min, `E-mail ${to} (${subject}) sent.`);
|
2024-04-30 15:05:31 -03:00
|
|
|
}
|
2022-06-30 12:53:42 -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
|
|
|
*
|
2022-11-02 19:40:59 -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
|
|
|
*/
|
2023-01-13 16:09:45 -03:00
|
|
|
public async sendFileTo({ pid, mobile, filename, caption }) {
|
2023-08-13 19:47:39 -03:00
|
|
|
const { min, user, proc } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `SEND FILE TO '${mobile}',filename '${filename}'.`);
|
2023-08-13 19:47:39 -03:00
|
|
|
return await this.internalSendFile({ pid, mobile, channel: proc.channel, filename, caption });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2024-05-06 17:52:08 -03:00
|
|
|
/**
|
|
|
|
* Sends a template to a given mobile.
|
|
|
|
*
|
|
|
|
* @example SEND TEMPLATE TO "+199988887777","image.jpg"
|
|
|
|
*
|
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async sendTemplateTo({ pid, mobile, filename }) {
|
2024-05-06 17:52:08 -03:00
|
|
|
const { min, user, proc } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `SEND TEMPLATE TO '${mobile}',filename '${filename}'.`);
|
2024-05-06 17:52:08 -03:00
|
|
|
const service = new GBConversationalService(min.core);
|
2024-05-06 18:07:34 -03:00
|
|
|
|
|
|
|
let text;
|
|
|
|
if (filename.endsWith('.docx')) {
|
|
|
|
text = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
|
2024-08-04 17:16:04 -03:00
|
|
|
} else {
|
2024-05-06 18:07:34 -03:00
|
|
|
text = filename;
|
|
|
|
}
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
return await service.fillAndBroadcastTemplate(min, filename, mobile, text);
|
2024-05-06 17:52:08 -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
|
|
|
*/
|
2023-01-13 16:09:45 -03:00
|
|
|
public async sendFile({ pid, filename, caption }) {
|
2023-03-26 19:33:58 -03:00
|
|
|
const { min, user, proc } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `SEND FILE (to: ${user.userSystemId},filename '${filename}'.`);
|
2023-02-23 17:41:29 -03:00
|
|
|
const mobile = await this.userMobile({ pid });
|
2023-03-26 19:33:58 -03:00
|
|
|
return await this.internalSendFile({ pid, channel: proc.channel, mobile, filename, caption });
|
2022-06-21 17:46:46 -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
|
|
|
*/
|
2023-03-04 16:27:25 -03:00
|
|
|
public async setLanguage({ pid, language }) {
|
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-06-21 17:46:46 -03:00
|
|
|
const sec = new SecService();
|
2023-03-04 16:27:25 -03:00
|
|
|
await sec.updateUserLocale(user.userId, language);
|
2024-02-28 12:15:30 -03:00
|
|
|
GBLogEx.info(min, `SET LANGUAGE: ${language}.`);
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2023-08-23 11:24:48 -03:00
|
|
|
/**
|
|
|
|
* Defines the current security context for dialogs based on roles.
|
|
|
|
*
|
|
|
|
* @example ALLOW ROLE "DevOps"
|
|
|
|
*
|
|
|
|
*/
|
2023-08-25 18:13:10 -03:00
|
|
|
public async allowRole({ pid, role }) {
|
2023-08-23 11:24:48 -03:00
|
|
|
const { min, user, proc } = await DialogKeywords.getProcessInfo(pid);
|
2024-01-11 14:42:00 -03:00
|
|
|
const sys = new SystemKeywords();
|
2023-08-23 11:24:48 -03:00
|
|
|
|
2024-01-11 14:42:00 -03:00
|
|
|
if (!role) {
|
2023-08-25 18:13:10 -03:00
|
|
|
throw new Error(`Invalid access. NULL role specified.`);
|
|
|
|
}
|
|
|
|
|
2023-08-23 11:24:48 -03:00
|
|
|
// Updates current roles allowed from now on this dialog/process.
|
|
|
|
|
2023-08-25 18:07:38 -03:00
|
|
|
proc.roles = role;
|
2023-08-23 11:24:48 -03:00
|
|
|
|
|
|
|
// Checks access.
|
|
|
|
|
2024-08-04 17:16:04 -03:00
|
|
|
const filters = ['People.xlsx', `${role}=x`, `id=${user.userSystemId}`];
|
2024-01-11 14:42:00 -03:00
|
|
|
const people = await sys.find({ pid, handle: null, args: filters });
|
2023-08-23 11:24:48 -03:00
|
|
|
|
2024-01-11 14:42:00 -03:00
|
|
|
if (!people) {
|
2023-08-25 18:07:38 -03:00
|
|
|
throw new Error(`Invalid access. Check if People sheet has the role ${role} checked.`);
|
2024-08-04 17:16:04 -03:00
|
|
|
} else {
|
2024-02-24 15:02:36 -03:00
|
|
|
GBLogEx.info(min, `Allowed access for ${user.userSystemId} on ${role}`);
|
|
|
|
return people;
|
|
|
|
}
|
2023-08-23 11:24:48 -03:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:38:13 -03:00
|
|
|
/**
|
|
|
|
* Defines the id generation policy.
|
|
|
|
*
|
2022-11-19 23:34:58 -03:00
|
|
|
* @example SET ID NUMBER
|
2022-07-13 09:38:13 -03:00
|
|
|
*
|
|
|
|
*/
|
2022-12-14 08:23:39 -03:00
|
|
|
public async setIdGeneration({ mode }) {
|
2022-07-13 09:38:13 -03:00
|
|
|
this['idGeneration'] = mode;
|
2023-03-04 16:27:25 -03:00
|
|
|
this['id'] = new SystemKeywords().getRandomId();
|
2022-07-13 09:38:13 -03:00
|
|
|
}
|
|
|
|
|
2023-04-01 10:42:44 -03:00
|
|
|
public static isUserSystemParam(name: string): Boolean {
|
2023-02-01 11:15:58 -03:00
|
|
|
const names = [
|
|
|
|
'welcomed',
|
|
|
|
'loaded',
|
|
|
|
'subjects',
|
|
|
|
'cb',
|
|
|
|
'welcomed',
|
|
|
|
'maxLines',
|
|
|
|
'translatorOn',
|
|
|
|
'wholeWord',
|
|
|
|
'theme',
|
|
|
|
'maxColumns'
|
|
|
|
];
|
|
|
|
|
|
|
|
return names.indexOf(name) > -1;
|
|
|
|
}
|
|
|
|
|
2023-04-01 10:42:44 -03:00
|
|
|
public static async setOption({ pid, name, value }) {
|
2023-03-19 20:09:54 -03:00
|
|
|
// if (this.isUserSystemParam(name)) {
|
|
|
|
// throw new Error(`Not possible to define ${name} as it is a reserved system param name.`);
|
|
|
|
// }
|
2023-01-31 11:46:37 -03:00
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `${name} = ${value} (botId: ${min.botId})`);
|
2023-07-26 12:47:13 -03:00
|
|
|
const sec = new SecService();
|
2023-08-13 17:42:16 -03:00
|
|
|
if (user) {
|
|
|
|
await sec.setParam(user.userId, name, value);
|
2023-07-26 12:47:13 -03:00
|
|
|
return { min, user, params };
|
2023-08-13 17:42:16 -03:00
|
|
|
} else {
|
2023-07-26 12:47:13 -03:00
|
|
|
min[name] = value;
|
|
|
|
}
|
2023-01-31 11:46:37 -03:00
|
|
|
}
|
|
|
|
|
2024-01-11 14:42:00 -03:00
|
|
|
public static async getOption({ pid, name, root = false }) {
|
2023-08-13 18:46:47 -03:00
|
|
|
if (this.isUserSystemParam(name) && !root) {
|
2023-02-01 12:23:47 -03:00
|
|
|
throw new Error(`Not possible to retrieve ${name} system param.`);
|
|
|
|
}
|
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2023-07-26 12:47:13 -03:00
|
|
|
|
2023-08-13 17:42:16 -03:00
|
|
|
if (user) {
|
2023-07-26 12:47:13 -03:00
|
|
|
const sec = new SecService();
|
|
|
|
return await sec.getParam(user, name);
|
2023-08-13 17:42:16 -03:00
|
|
|
} else {
|
2023-07-26 12:47:13 -03:00
|
|
|
return min[name];
|
|
|
|
}
|
2023-02-01 12:23:47 -03:00
|
|
|
}
|
|
|
|
|
2021-04-12 10:07:59 -03:00
|
|
|
/**
|
|
|
|
* Defines the maximum lines to scan in spreedsheets.
|
|
|
|
*
|
|
|
|
* @example SET MAX LINES 5000
|
|
|
|
*
|
|
|
|
*/
|
2023-01-31 11:46:37 -03:00
|
|
|
public async setMaxLines({ pid, count }) {
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'maxLines', value: count });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2021-06-01 10:05:52 -03:00
|
|
|
|
2023-07-23 17:33:13 -03:00
|
|
|
/**
|
|
|
|
* Define array as output.
|
|
|
|
*
|
|
|
|
* @example SET OUTPUT ARRAY
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setOutput({ pid, value }) {
|
|
|
|
await DialogKeywords.setOption({ pid, name: 'output', value: value });
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:15:58 -03:00
|
|
|
/**
|
|
|
|
* Defines a custom user param to be persisted to storage.
|
|
|
|
*
|
|
|
|
* @example SET PARAM name AS value
|
|
|
|
*
|
|
|
|
*/
|
2023-02-23 17:41:29 -03:00
|
|
|
public async setUserParam({ pid, name, value }) {
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name, value });
|
2023-02-01 11:15:58 -03:00
|
|
|
}
|
|
|
|
|
2023-10-05 10:06:03 -03:00
|
|
|
/**
|
|
|
|
* Returns current if any continuation token for paginated HTTP requests.
|
|
|
|
*
|
|
|
|
* @example CONTINUATION TOKEN
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async getContinuationToken({ pid }) {
|
|
|
|
let { min, user, params, proc } = await DialogKeywords.getProcessInfo(pid);
|
|
|
|
|
|
|
|
return DialogKeywords.getOption({ pid, name: `${proc.executable}-continuationToken` });
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:15:58 -03:00
|
|
|
/**
|
2023-07-23 18:02:10 -03:00
|
|
|
* Returns bot param persisted on storage.
|
2023-02-01 11:15:58 -03:00
|
|
|
*
|
2023-07-23 18:02:10 -03:00
|
|
|
* @example GET CONFIG name
|
2023-02-01 11:15:58 -03:00
|
|
|
*
|
|
|
|
*/
|
2023-07-23 18:02:10 -03:00
|
|
|
public async getConfig({ pid, name }) {
|
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-29 19:53:56 -03:00
|
|
|
return min.core.getParam(min.instance, name, null, false);
|
2023-02-01 11:15:58 -03:00
|
|
|
}
|
|
|
|
|
2022-08-26 09:53:00 -03:00
|
|
|
/**
|
2022-11-19 23:34:58 -03:00
|
|
|
* Defines the maximum lines to scan in spreedsheets.
|
|
|
|
*
|
|
|
|
* @example SET MAX COLUMNS 5000
|
|
|
|
*
|
|
|
|
*/
|
2023-01-31 11:46:37 -03:00
|
|
|
public async setMaxColumns({ pid, count }) {
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'setMaxColumns', value: count });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a custom user filter for SET calls.
|
|
|
|
*
|
|
|
|
* @example SET FILTER "ColumnName=33"
|
|
|
|
* SET "file.xlsx", "C", "200000"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setFilter({ pid, value }) {
|
|
|
|
await DialogKeywords.setOption({ pid, name: 'filter', value });
|
2022-08-26 09:53:00 -03:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:21:51 -03:00
|
|
|
/**
|
|
|
|
* Defines page mode for paged GET calls.
|
|
|
|
*
|
|
|
|
* @example SET PAGE MODE "auto"
|
2024-08-04 17:16:04 -03:00
|
|
|
* data = GET url
|
2023-10-04 15:21:51 -03:00
|
|
|
* FOR EACH item in data
|
|
|
|
* ...
|
|
|
|
* END FOR
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async setPageMode({ pid, value }) {
|
|
|
|
await DialogKeywords.setOption({ pid, name: 'pageMode', value });
|
|
|
|
}
|
2021-08-21 09:19:07 -03:00
|
|
|
/**
|
|
|
|
* Defines the FIND behaviour to consider whole words while searching.
|
|
|
|
*
|
|
|
|
* @example SET WHOLE WORD ON
|
|
|
|
*
|
|
|
|
*/
|
2023-01-31 11:46:37 -03:00
|
|
|
public async setWholeWord({ pid, on }) {
|
2023-02-23 17:41:29 -03:00
|
|
|
const value = on.trim() === 'on';
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'wholeWord', value: value });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2021-08-21 09:19:07 -03:00
|
|
|
|
2023-03-06 20:36:27 -03:00
|
|
|
/**
|
2023-03-06 07:09:24 -03:00
|
|
|
* Defines the FIND behaviour to consider whole words while searching.
|
|
|
|
*
|
|
|
|
* @example SET FILTER TYPE date, string
|
|
|
|
*
|
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async setFilterTypes({ pid, types }) {
|
|
|
|
const value = types;
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'filterTypes', value: value });
|
2023-03-06 20:36:27 -03:00
|
|
|
}
|
2023-03-06 07:09:24 -03:00
|
|
|
|
2022-06-17 19:50:26 -03:00
|
|
|
/**
|
2022-11-19 23:34:58 -03:00
|
|
|
* Defines the theme for assets generation.
|
|
|
|
*
|
|
|
|
* @example SET THEME "themename"
|
|
|
|
*
|
|
|
|
*/
|
2023-01-31 11:46:37 -03:00
|
|
|
public async setTheme({ pid, theme }) {
|
|
|
|
const value = theme.trim();
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'theme', value: value });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2022-06-04 17:02:34 -03:00
|
|
|
|
2021-04-22 14:39:51 -03:00
|
|
|
/**
|
|
|
|
* Defines translator behaviour.
|
|
|
|
*
|
|
|
|
* @example SET TRANSLATOR ON | OFF
|
|
|
|
*
|
|
|
|
*/
|
2023-01-31 11:46:37 -03:00
|
|
|
public async setTranslatorOn({ pid, on }) {
|
2023-02-23 17:41:29 -03:00
|
|
|
const value = on.trim() === 'on';
|
2023-04-01 10:42:44 -03:00
|
|
|
await DialogKeywords.setOption({ pid, name: 'translatorOn', value: value });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2021-04-22 14:39:51 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns the name of the user acquired by WhatsApp API.
|
2023-08-13 17:42:16 -03:00
|
|
|
*
|
2023-08-02 14:27:15 -03:00
|
|
|
* SAVE "file.xlsx", username, now
|
2023-08-13 17:42:16 -03:00
|
|
|
*
|
2020-12-28 18:43:34 -03:00
|
|
|
*/
|
2023-02-23 17:41:29 -03:00
|
|
|
public async userName({ pid }) {
|
2023-01-31 11:46:37 -03:00
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2023-08-13 17:42:16 -03:00
|
|
|
if (user) return user.userName;
|
|
|
|
else return 'unattended';
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Returns current mobile number from user in conversation.
|
|
|
|
*/
|
2023-02-23 17:41:29 -03:00
|
|
|
public async userMobile({ pid }) {
|
2023-01-31 11:46:37 -03:00
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2023-08-13 17:42:16 -03:00
|
|
|
if (user) return user.userSystemId;
|
|
|
|
else return 'unattended';
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
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
|
|
|
*/
|
2024-08-04 17:16:04 -03:00
|
|
|
public async showMenu({}) {
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/237
|
|
|
|
// return await beginDialog('/menu');
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2023-01-31 11:46:37 -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-12-14 08:23:39 -03:00
|
|
|
public async transferTo({ to }) {
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/150
|
|
|
|
// return await beginDialog('/t',{ to: to });
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-27 13:30:56 -03:00
|
|
|
|
2023-02-23 17:41:29 -03:00
|
|
|
public static getFileByHandle(hash) {
|
2023-02-03 10:25:58 -03:00
|
|
|
return GBServer.globals.files[hash];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2023-03-06 20:36:27 -03:00
|
|
|
public async hear({ pid, kind, args }) {
|
2023-08-13 17:42:16 -03:00
|
|
|
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
|
2023-01-13 16:09:45 -03:00
|
|
|
|
2022-11-02 19:40:59 -03:00
|
|
|
// Handles first arg as an array of args.
|
|
|
|
|
2023-03-06 20:36:27 -03:00
|
|
|
let args1 = [];
|
|
|
|
if (args && args.length) {
|
|
|
|
args1 = args;
|
2022-11-02 19:40:59 -03:00
|
|
|
}
|
2023-03-06 20:36:27 -03:00
|
|
|
args = args1;
|
2022-11-02 19:40:59 -03:00
|
|
|
|
2022-10-22 13:23:32 -03:00
|
|
|
try {
|
|
|
|
const isIntentYes = (locale, utterance) => {
|
|
|
|
return utterance.toLowerCase().match(Messages[locale].affirmative_sentences);
|
2022-11-19 23:34:58 -03:00
|
|
|
};
|
2023-01-17 11:03:25 -03:00
|
|
|
|
2023-01-13 16:09:45 -03:00
|
|
|
const sec = new SecService();
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-01-19 10:39:37 -03:00
|
|
|
// If SET HEAR ON is defined an impersonated context is created
|
|
|
|
// containing the specified user other than the actual user
|
|
|
|
// TODO: Store hrOn in processInfo.
|
|
|
|
|
2023-03-04 16:27:25 -03:00
|
|
|
if (params.hrOn) {
|
|
|
|
user = await sec.getUserFromAgentSystemId(params.hrOn);
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
2023-01-19 10:39:37 -03:00
|
|
|
|
2022-10-22 13:23:32 -03:00
|
|
|
const userId = user.userId;
|
|
|
|
let result;
|
|
|
|
|
|
|
|
const locale = user.locale ? user.locale : 'en-US';
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/266
|
2021-08-15 10:13:36 -03:00
|
|
|
|
2022-10-22 13:23:32 -03:00
|
|
|
if (args && args.length > 1) {
|
2023-03-06 20:36:27 -03:00
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
if (args.length > 3) {
|
|
|
|
let section = { title: '', rows: [] };
|
|
|
|
await CollectionUtil.asyncForEach(args, async arg => {
|
|
|
|
i++;
|
|
|
|
section.rows.push({ title: arg, id: `button${i}` });
|
|
|
|
});
|
|
|
|
const list = new List('Select:', '', [section], '', '');
|
|
|
|
await this.talk({ pid: pid, text: list });
|
|
|
|
} else {
|
|
|
|
let buttons = [];
|
|
|
|
await CollectionUtil.asyncForEach(args, async arg => {
|
|
|
|
i++;
|
|
|
|
buttons.push({ body: arg, id: `button${i}` });
|
|
|
|
});
|
|
|
|
let button = new Buttons('Select:', buttons, '', 'General Bots');
|
|
|
|
await this.talk({ pid: pid, text: button });
|
|
|
|
}
|
2022-12-14 08:23:39 -03:00
|
|
|
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `HEAR with [${args.toString()}] (Asking for input).`);
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, 'HEAR (Asking for input).');
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
2022-11-01 00:59:35 -03:00
|
|
|
|
2022-10-22 13:23:32 -03:00
|
|
|
// Wait for the user to answer.
|
|
|
|
|
2023-02-23 17:41:29 -03:00
|
|
|
min.cbMap[userId] = {};
|
2023-01-25 10:49:00 -03:00
|
|
|
min.cbMap[userId]['promise'] = '!GBHEAR';
|
2023-08-13 17:42:16 -03:00
|
|
|
|
2023-01-25 10:49:00 -03:00
|
|
|
while (min.cbMap[userId].promise === '!GBHEAR') {
|
2024-01-10 15:01:02 -03:00
|
|
|
await GBUtil.sleep(DEFAULT_HEAR_POLL_INTERVAL);
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-03-07 12:29:02 -03:00
|
|
|
const answer = min.cbMap[userId].promise;
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-03-07 12:28:45 -03:00
|
|
|
if (!kind) {
|
|
|
|
result = answer;
|
|
|
|
} else if (kind === 'sheet') {
|
2023-02-23 17:41:29 -03:00
|
|
|
// Retrieves the .xlsx file associated with the HEAR var AS file.xlsx.
|
|
|
|
|
2023-03-04 16:27:25 -03:00
|
|
|
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
2023-02-23 17:41:29 -03:00
|
|
|
const botId = min.instance.botId;
|
2024-09-07 00:08:23 -03:00
|
|
|
const packagePath = GBUtil.getGBAIPath(botId);
|
2023-02-23 17:41:29 -03:00
|
|
|
let url = `${baseUrl}/drive/root:/${path}:/children`;
|
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
GBLogEx.info(min, `Loading HEAR AS .xlsx options from Sheet: ${url}`);
|
2023-02-23 17:41:29 -03:00
|
|
|
const res = await client.api(url).get();
|
|
|
|
|
|
|
|
// Finds .xlsx specified by arg.
|
|
|
|
|
|
|
|
const document = res.value.filter(m => {
|
2023-03-06 20:36:27 -03:00
|
|
|
return m.name === args;
|
2023-02-23 17:41:29 -03:00
|
|
|
});
|
|
|
|
if (document === undefined || document.length === 0) {
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `${args} not found on .gbdata folder, check the package.`);
|
2023-02-23 17:41:29 -03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads all rows to be used as menu items in HEAR validation.
|
|
|
|
|
|
|
|
let sheets = await client.api(`${baseUrl}/drive/items/${document[0].id}/workbook/worksheets`).get();
|
|
|
|
const results = await client
|
|
|
|
.api(
|
|
|
|
`${baseUrl}/drive/items/${document[0].id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A1:A256')`
|
|
|
|
)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
// Builds an array of items found in sheet file.
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
let list = [];
|
|
|
|
for (; index < results.text.length; index++) {
|
|
|
|
if (results.text[index][0] !== '') {
|
2023-03-03 19:10:31 -03:00
|
|
|
list.push(results.text[index][0]);
|
2023-03-06 20:36:27 -03:00
|
|
|
} else {
|
2023-02-23 17:41:29 -03:00
|
|
|
break;
|
2023-03-03 19:10:31 -03:00
|
|
|
}
|
2023-02-23 17:41:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search the answer in one of valid list items loaded from sheeet.
|
|
|
|
|
|
|
|
result = null;
|
|
|
|
await CollectionUtil.asyncForEach(list, async item => {
|
|
|
|
if (GBConversationalService.kmpSearch(answer, item) != -1) {
|
|
|
|
result = item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// In case of unmatch, asks the person to try again.
|
|
|
|
|
|
|
|
if (result === null) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2023-02-23 17:41:29 -03:00
|
|
|
}
|
|
|
|
} else if (kind === 'file') {
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
2024-03-03 16:20:50 -03:00
|
|
|
const handle = WebAutomationServices.cyrb53({ pid, str: min.botId + answer.filename });
|
2023-02-03 10:25:58 -03:00
|
|
|
GBServer.globals.files[handle] = answer;
|
|
|
|
result = handle;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'boolean') {
|
2023-02-01 12:23:47 -03:00
|
|
|
if (isIntentYes('pt-BR', answer)) {
|
2022-10-22 13:23:32 -03:00
|
|
|
result = true;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2022-10-22 13:23:32 -03:00
|
|
|
result = false;
|
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'email') {
|
|
|
|
const extractEntity = text => {
|
2022-10-22 13:23:32 -03:00
|
|
|
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
|
2022-11-19 23:34:58 -03:00
|
|
|
};
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-02-01 11:15:58 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
|
|
|
if (value === null) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um e-mail válido.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:44:52 -03:00
|
|
|
result = answer;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'name') {
|
2022-10-22 13:23:32 -03:00
|
|
|
const extractEntity = text => {
|
2023-10-26 09:51:23 -03:00
|
|
|
return text.match(/[_a-zA-Z][_a-zA-Z0-9]{0,30}/gi);
|
2022-10-22 13:23:32 -03:00
|
|
|
};
|
|
|
|
|
2023-02-01 11:15:58 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-10-26 09:51:23 -03:00
|
|
|
if (value === null || value.length == 1) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um nome válido.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:44:52 -03:00
|
|
|
result = answer;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'integer') {
|
2022-10-22 13:23:32 -03:00
|
|
|
const extractEntity = text => {
|
|
|
|
return text.match(/\d+/gi);
|
|
|
|
};
|
|
|
|
|
2023-02-01 12:23:47 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
|
|
|
if (value === null || value.length != 1) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um número válido.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:44:52 -03:00
|
|
|
result = answer;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'date') {
|
2023-04-05 12:29:23 -03:00
|
|
|
const parseDate = str => {
|
2023-08-13 17:42:16 -03:00
|
|
|
function pad(x) {
|
|
|
|
return (('' + x).length == 2 ? '' : '0') + x;
|
|
|
|
}
|
|
|
|
var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/),
|
|
|
|
d = m ? new Date(m[3], m[2] - 1, m[1]) : null,
|
|
|
|
matchesPadded = d && str == [pad(d.getDate()), pad(d.getMonth() + 1), d.getFullYear()].join('/'),
|
|
|
|
matchesNonPadded = d && str == [d.getDate(), d.getMonth() + 1, d.getFullYear()].join('/');
|
|
|
|
return matchesPadded || matchesNonPadded ? d : null;
|
|
|
|
};
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-04-05 12:29:23 -03:00
|
|
|
let value = parseDate(answer);
|
|
|
|
|
|
|
|
if (value === null) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite uma data no formato 12/12/2020.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
2023-04-05 12:29:23 -03:00
|
|
|
value = new Date(value);
|
|
|
|
result = value.toLocaleString('pt-BR', { day: '2-digit', month: '2-digit', year: 'numeric' });
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'hour') {
|
2023-01-25 10:49:00 -03:00
|
|
|
const extractEntity = (text: string) => {
|
2022-10-22 13:23:32 -03:00
|
|
|
return text.match(/^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$/gi);
|
|
|
|
};
|
|
|
|
|
2023-02-01 12:23:47 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
|
|
|
if (value === null || value.length != 1) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um horário no formato hh:ss.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:44:52 -03:00
|
|
|
result = answer;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'money') {
|
2023-01-25 10:49:00 -03:00
|
|
|
const extractEntity = (text: string) => {
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/307
|
2023-01-17 11:03:25 -03:00
|
|
|
if (user.locale === 'en') {
|
2022-12-26 07:51:41 -03:00
|
|
|
return text.match(/(?:\d{1,3},)*\d{1,3}(?:\.\d+)?/gi);
|
2023-01-31 11:46:37 -03:00
|
|
|
} else {
|
2022-12-26 07:51:41 -03:00
|
|
|
return text.match(/(?:\d{1,3}.)*\d{1,3}(?:\,\d+)?/gi);
|
|
|
|
}
|
2022-11-01 00:59:35 -03:00
|
|
|
return [];
|
2022-10-22 13:23:32 -03:00
|
|
|
};
|
|
|
|
|
2023-02-01 12:23:47 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
|
|
|
if (value === null || value.length != 1) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um valor monetário.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = value;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'mobile') {
|
2023-07-14 18:51:36 -03:00
|
|
|
let phoneNumber = answer;
|
|
|
|
let p = libphonenumber.PhoneNumberUtil.getInstance();
|
2022-10-22 13:23:32 -03:00
|
|
|
try {
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/307
|
2023-07-14 18:51:36 -03:00
|
|
|
phoneNumber = p.parse(phoneNumber);
|
2022-10-22 13:23:32 -03:00
|
|
|
} catch (error) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: Messages[locale].validation_enter_valid_mobile });
|
2022-10-22 13:23:32 -03:00
|
|
|
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
2023-07-14 18:51:36 -03:00
|
|
|
if (!p.isPossibleNumber(phoneNumber)) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um número de telefone válido.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = phoneNumber;
|
2023-03-06 20:36:27 -03:00
|
|
|
} else if (kind === 'qr-scanner') {
|
2023-03-02 17:46:45 -03:00
|
|
|
//https://github.com/GeneralBots/BotServer/issues/171
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `BASIC (${min.botId}): Upload done for ${answer.filename}.`);
|
2024-03-03 16:20:50 -03:00
|
|
|
const handle = WebAutomationServices.cyrb53({ pid, str: min.botId + answer.filename });
|
2023-03-02 17:46:45 -03:00
|
|
|
GBServer.globals.files[handle] = answer;
|
2023-03-06 20:36:27 -03:00
|
|
|
QrScanner.scanImage(GBServer.globals.files[handle])
|
|
|
|
.then(result => console.log(result))
|
|
|
|
.catch(error => console.log(error || 'no QR code found.'));
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'zipcode') {
|
2023-01-25 10:49:00 -03:00
|
|
|
const extractEntity = (text: string) => {
|
2022-10-22 13:23:32 -03:00
|
|
|
text = text.replace(/\-/gi, '');
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (user.locale === 'en') {
|
2022-12-26 07:51:41 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/307
|
2022-10-22 13:23:32 -03:00
|
|
|
return text.match(/\d{8}/gi);
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2022-10-22 13:23:32 -03:00
|
|
|
return text.match(/(?:\d{1,3}.)*\d{1,3}(?:\,\d+)?/gi);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-01 12:23:47 -03:00
|
|
|
const value = extractEntity(answer);
|
2022-10-22 13:23:32 -03:00
|
|
|
|
|
|
|
if (value === null || value.length != 1) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: 'Por favor, digite um CEP válido.' });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
|
2023-10-30 13:44:52 -03:00
|
|
|
result = answer;
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'menu') {
|
2022-10-22 13:23:32 -03:00
|
|
|
const list = args;
|
|
|
|
result = null;
|
|
|
|
await CollectionUtil.asyncForEach(list, async item => {
|
2023-02-01 12:23:47 -03:00
|
|
|
if (GBConversationalService.kmpSearch(answer, item) != -1) {
|
2022-10-22 13:23:32 -03:00
|
|
|
result = item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result === null) {
|
2023-12-19 18:19:36 -03:00
|
|
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos (${args.join(', ')}).` });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
} else if (kind === 'language') {
|
2022-10-22 13:23:32 -03:00
|
|
|
result = null;
|
|
|
|
|
|
|
|
const list = [
|
|
|
|
{ name: 'english', code: 'en' },
|
|
|
|
{ name: 'inglês', code: 'en' },
|
|
|
|
{ name: 'portuguese', code: 'pt' },
|
|
|
|
{ name: 'português', code: 'pt' },
|
|
|
|
{ name: 'français', code: 'fr' },
|
|
|
|
{ name: 'francês', code: 'fr' },
|
|
|
|
{ name: 'french', code: 'fr' },
|
|
|
|
{ name: 'spanish', code: 'es' },
|
|
|
|
{ name: 'espanõl', code: 'es' },
|
|
|
|
{ name: 'espanhol', code: 'es' },
|
|
|
|
{ name: 'german', code: 'de' },
|
|
|
|
{ name: 'deutsch', code: 'de' },
|
|
|
|
{ name: 'alemão', code: 'de' }
|
|
|
|
];
|
|
|
|
|
|
|
|
await CollectionUtil.asyncForEach(list, async item => {
|
2022-11-19 23:34:58 -03:00
|
|
|
if (
|
2023-02-01 12:23:47 -03:00
|
|
|
GBConversationalService.kmpSearch(answer.toLowerCase(), item.name.toLowerCase()) != -1 ||
|
|
|
|
GBConversationalService.kmpSearch(answer.toLowerCase(), item.code.toLowerCase()) != -1
|
2022-11-19 23:34:58 -03:00
|
|
|
) {
|
2022-10-22 13:23:32 -03:00
|
|
|
result = item.code;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result === null) {
|
2023-03-04 16:27:25 -03:00
|
|
|
await this.talk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
|
2023-03-06 20:36:27 -03:00
|
|
|
return await this.hear({ pid, kind, args });
|
2022-10-22 13:23:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
|
|
|
GBLog.error(`BASIC RUNTIME ERR HEAR ${error.message ? error.message : error}\n Stack:${error.stack}`);
|
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.
|
|
|
|
*/
|
2023-03-04 16:27:25 -03:00
|
|
|
public async gotoDialog({ pid, fromOrDialogName, dialogName }) {
|
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-06-21 17:46:46 -03:00
|
|
|
if (dialogName) {
|
|
|
|
if (dialogName.charAt(0) === '/') {
|
2022-12-26 13:37:11 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/308
|
|
|
|
// await step.beginDialog(fromOrDialogName);
|
2022-06-21 17:46:46 -03:00
|
|
|
} else {
|
|
|
|
let sec = new SecService();
|
|
|
|
let user = await sec.getUserFromSystemId(fromOrDialogName);
|
|
|
|
if (!user) {
|
2024-08-04 17:16:04 -03:00
|
|
|
user = await sec.ensureUser(min, fromOrDialogName, fromOrDialogName, null, 'whatsapp', 'from', null);
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
|
|
|
await sec.updateUserHearOnDialog(user.userId, dialogName);
|
2022-06-17 19:50:26 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2022-12-26 13:37:11 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/308
|
|
|
|
// await step.beginDialog(fromOrDialogName);
|
2021-09-13 18:36:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
public async messageBot({ pid, text }) {
|
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2024-04-02 17:52:17 -03:00
|
|
|
GBLogEx.info(min, `MESSAGE BOT: ${text}.`);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-04-02 17:52:17 -03:00
|
|
|
const { conversation, client } = min['apiConversations'][pid];
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-03-15 07:14:21 -03:00
|
|
|
await client.apis.Conversations.Conversations_PostActivity({
|
2024-03-03 16:20:50 -03:00
|
|
|
conversationId: conversation.conversationId,
|
|
|
|
activity: {
|
2024-04-17 15:07:53 -03:00
|
|
|
pid: pid,
|
2024-03-03 16:20:50 -03:00
|
|
|
textFormat: 'plain',
|
|
|
|
text: text,
|
|
|
|
type: 'message',
|
|
|
|
from: {
|
2024-03-15 07:14:21 -03:00
|
|
|
id: user.userSystemId,
|
|
|
|
name: user.userName
|
2024-03-03 16:20:50 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-04-02 17:52:17 -03:00
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
let messages = [];
|
2024-04-21 23:39:39 -03:00
|
|
|
GBLogEx.info(min, `MessageBot: Starting message polling ${conversation.conversationId}).`);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-04-17 16:07:19 -03:00
|
|
|
let count = API_RETRIES;
|
|
|
|
while (count--) {
|
|
|
|
await GBUtil.sleep(DEFAULT_HEAR_POLL_INTERVAL);
|
2024-04-30 15:05:31 -03:00
|
|
|
|
2024-03-15 07:14:21 -03:00
|
|
|
try {
|
|
|
|
const response = await client.apis.Conversations.Conversations_GetActivities({
|
|
|
|
conversationId: conversation.conversationId,
|
|
|
|
watermark: conversation.watermark
|
2024-03-03 16:20:50 -03:00
|
|
|
});
|
2024-04-17 16:38:26 -03:00
|
|
|
conversation.watermark = response.obj.watermark;
|
2024-04-17 16:32:48 -03:00
|
|
|
let activities = response.obj.activities;
|
2024-04-02 17:52:17 -03:00
|
|
|
|
2024-03-15 07:14:21 -03:00
|
|
|
if (activities && activities.length) {
|
|
|
|
activities = activities.filter(m => m.from.id === min.botId && m.type === 'message');
|
|
|
|
if (activities.length) {
|
|
|
|
activities.forEach(activity => {
|
2024-04-30 15:05:31 -03:00
|
|
|
messages.push(activity.text);
|
2024-03-15 07:14:21 -03:00
|
|
|
GBLogEx.info(min, `MESSAGE BOT answer from bot: ${activity.text}`);
|
|
|
|
});
|
|
|
|
}
|
2024-04-17 16:07:19 -03:00
|
|
|
return messages.join('\n');
|
2024-03-15 07:14:21 -03:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
GBLog.error(
|
2024-08-04 17:16:04 -03:00
|
|
|
`Error calling printMessages in messageBot API ${err.data === undefined ? err : err.data} ${
|
|
|
|
err.errObj ? err.errObj.message : ''
|
2024-03-15 07:14:21 -03:00
|
|
|
}`
|
|
|
|
);
|
2024-04-17 16:07:19 -03:00
|
|
|
return err;
|
2024-03-03 16:20:50 -03:00
|
|
|
}
|
2024-08-04 17:16:04 -03:00
|
|
|
}
|
2024-03-03 16:20:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public async start({ botId, botApiKey, userSystemId, text }) {
|
|
|
|
let min: GBMinInstance = GBServer.globals.minInstances.filter(p => p.instance.botId === botId)[0];
|
|
|
|
let sec = new SecService();
|
|
|
|
let user = await sec.getUserFromSystemId(userSystemId);
|
|
|
|
if (!user) {
|
2024-08-04 17:16:04 -03:00
|
|
|
user = await sec.ensureUser(min, userSystemId, userSystemId, null, 'api', 'API User', null);
|
2024-03-03 16:20:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const pid = GBVMService.createProcessInfo(user, min, 'api', null);
|
2024-04-02 17:52:17 -03:00
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
const conversation = min['apiConversations'][pid];
|
|
|
|
|
2024-08-19 16:12:23 -03:00
|
|
|
const client = await GBUtil.getDirectLineClient(min);
|
2024-03-03 16:20:50 -03:00
|
|
|
conversation.client = client;
|
|
|
|
const response = await client.apis.Conversations.Conversations_StartConversation();
|
|
|
|
conversation.conversationId = response.obj.conversationId;
|
|
|
|
|
2024-04-02 17:52:17 -03:00
|
|
|
return await GBVMService.callVM('start', min, null, pid);
|
2024-03-03 16:20:50 -03:00
|
|
|
}
|
|
|
|
|
2023-01-19 10:47:47 -03:00
|
|
|
public static async getProcessInfo(pid: number) {
|
2023-01-17 11:03:25 -03:00
|
|
|
const proc = GBServer.globals.processes[pid];
|
2024-03-16 21:36:03 -03:00
|
|
|
const step = proc.step;
|
2023-02-22 16:15:05 -03:00
|
|
|
const min = GBServer.globals.minInstances.filter(p => p.instance.instanceId == proc.instanceId)[0];
|
2023-01-17 11:03:25 -03:00
|
|
|
const sec = new SecService();
|
2024-09-05 01:23:49 -03:00
|
|
|
const user = GBServer.globals.users[proc.userId];
|
2023-08-13 17:42:16 -03:00
|
|
|
const params = user ? JSON.parse(user.params) : {};
|
2023-01-17 11:03:25 -03:00
|
|
|
return {
|
2023-01-31 11:46:37 -03:00
|
|
|
min,
|
|
|
|
user,
|
2023-03-26 19:33:58 -03:00
|
|
|
params,
|
2024-03-16 21:36:03 -03:00
|
|
|
proc,
|
|
|
|
step
|
2023-01-19 10:39:37 -03:00
|
|
|
};
|
2023-01-17 11:03:25 -03:00
|
|
|
}
|
|
|
|
|
2020-12-28 18:43:34 -03:00
|
|
|
/**
|
|
|
|
* Talks to the user by using the specified text.
|
|
|
|
*/
|
2023-03-04 16:27:25 -03:00
|
|
|
public async talk({ pid, text }) {
|
2024-03-16 21:36:03 -03:00
|
|
|
const { min, user, step } = await DialogKeywords.getProcessInfo(pid);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `TALK '${text} step:${step}'.`);
|
2023-02-23 17:41:29 -03:00
|
|
|
|
2023-01-17 11:03:25 -03:00
|
|
|
if (user) {
|
2023-03-05 11:09:36 -03:00
|
|
|
// TODO: const translate = user ? user.basicOptions.translatorOn : false;
|
2024-02-16 21:26:11 -03:00
|
|
|
text = await min.conversationalService.translate(
|
|
|
|
min,
|
|
|
|
text,
|
2024-08-04 17:16:04 -03:00
|
|
|
user.locale ? user.locale : min.core.getParam(min.instance, 'Locale', GBConfigService.get('LOCALE'))
|
2024-02-16 21:26:11 -03:00
|
|
|
);
|
|
|
|
GBLog.verbose(`Translated text(playMarkdown): ${text}.`);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-04-02 17:52:17 -03:00
|
|
|
if (step) {
|
2024-03-16 21:36:03 -03:00
|
|
|
await min.conversationalService.sendText(min, step, text);
|
2024-08-04 17:16:04 -03:00
|
|
|
} else {
|
2024-03-16 21:36:03 -03:00
|
|
|
await min.conversationalService['sendOnConversation'](min, user, text);
|
|
|
|
}
|
2022-11-01 00:59:35 -03:00
|
|
|
}
|
2023-03-03 19:10:31 -03:00
|
|
|
return { status: 0 };
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
|
2022-12-14 08:23:39 -03:00
|
|
|
private static getChannel(): string {
|
2022-11-01 00:59:35 -03:00
|
|
|
return 'whatsapp';
|
2022-12-26 13:37:11 -03:00
|
|
|
// https://github.com/GeneralBots/BotServer/issues/309
|
2021-06-01 10:05:52 -03:00
|
|
|
}
|
|
|
|
|
2020-12-31 15:36:19 -03:00
|
|
|
/**
|
|
|
|
* Processes the sending of the file.
|
|
|
|
*/
|
2023-03-26 19:33:58 -03:00
|
|
|
private async internalSendFile({ pid, channel, mobile, filename, caption }) {
|
2022-11-02 19:40:59 -03:00
|
|
|
// Handles SEND FILE TO mobile,element in Web Automation.
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2023-01-31 11:46:37 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-11-19 23:34:58 -03:00
|
|
|
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
|
2023-03-26 19:33:58 -03:00
|
|
|
let url;
|
2023-07-31 15:06:47 -03:00
|
|
|
let nameOnly;
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2023-07-31 15:06:47 -03:00
|
|
|
|
|
|
|
// Web automation.
|
2022-10-12 15:08:53 -03:00
|
|
|
|
|
|
|
if (element) {
|
2024-09-06 15:30:03 -03:00
|
|
|
const localName = path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);
|
|
|
|
nameOnly = path.basename(localName);
|
2022-10-12 15:08:53 -03:00
|
|
|
await element.screenshot({ path: localName, fullPage: true });
|
2022-06-17 19:50:26 -03:00
|
|
|
|
2024-09-06 15:30:03 -03:00
|
|
|
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
|
2022-06-01 19:45:43 -03:00
|
|
|
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `WebAutomation: Sending ${url} to ${mobile} (${channel}).`);
|
2023-08-13 17:42:16 -03:00
|
|
|
}
|
2023-07-31 15:06:47 -03:00
|
|
|
|
2023-08-13 17:42:16 -03:00
|
|
|
// GBFILE object.
|
2023-07-31 15:06:47 -03:00
|
|
|
else if (filename.url) {
|
2023-04-01 08:01:10 -03:00
|
|
|
url = filename.url;
|
2024-09-06 15:30:03 -03:00
|
|
|
nameOnly = path.basename(filename.localName);
|
2023-08-13 18:36:54 -03:00
|
|
|
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Sending the GBFILE ${url} to ${mobile} (${channel}).`);
|
2023-04-01 10:42:44 -03:00
|
|
|
}
|
2023-09-30 20:43:39 -03:00
|
|
|
|
2022-06-21 17:46:46 -03:00
|
|
|
// Handles Markdown.
|
2023-12-19 16:48:28 -03:00
|
|
|
else if (filename.indexOf('.md') !== -1) {
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Sending the contents of ${filename} markdown to mobile ${mobile}.`);
|
2023-01-25 10:49:00 -03:00
|
|
|
const md = await min.kbService.getAnswerTextByMediaName(min.instance.instanceId, filename);
|
2022-06-21 17:46:46 -03:00
|
|
|
if (!md) {
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Markdown file ${filename} not found on database for ${min.instance.botId}.`);
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2023-09-29 15:55:35 -03:00
|
|
|
await min.conversationalService['playMarkdown'](min, md, DialogKeywords.getChannel(), null, mobile);
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2023-09-29 17:23:02 -03:00
|
|
|
return;
|
2023-03-26 19:33:58 -03:00
|
|
|
}
|
|
|
|
|
2023-09-30 20:43:39 -03:00
|
|
|
// .gbdrive direct sending.
|
|
|
|
else {
|
2024-09-06 15:30:03 -03:00
|
|
|
const ext = path.extname(filename);
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2023-11-27 14:40:08 -03:00
|
|
|
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
|
2024-02-24 15:39:16 -03:00
|
|
|
const fileUrl = urlJoin('/', gbaiName, `${min.botId}.gbdrive`, filename);
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Direct send from .gbdrive: ${fileUrl} to ${mobile}.`);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2023-11-27 14:40:08 -03:00
|
|
|
const sys = new SystemKeywords();
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-02-24 15:39:16 -03:00
|
|
|
const pathOnly = fileUrl.substring(0, fileUrl.lastIndexOf('/'));
|
|
|
|
const fileOnly = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
|
2024-03-03 16:20:50 -03:00
|
|
|
|
2024-02-24 15:02:36 -03:00
|
|
|
let template = await sys.internalGetDocument(client, baseUrl, pathOnly, fileOnly);
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2023-12-19 16:52:23 -03:00
|
|
|
const driveUrl = template['@microsoft.graph.downloadUrl'];
|
|
|
|
const res = await fetch(driveUrl);
|
2023-11-27 14:40:08 -03:00
|
|
|
let buf: any = Buffer.from(await res.arrayBuffer());
|
2024-09-06 15:30:03 -03:00
|
|
|
let localName1 = path.join(
|
2024-08-04 17:16:04 -03:00
|
|
|
'work',
|
|
|
|
gbaiName,
|
|
|
|
'cache',
|
|
|
|
`${fileOnly.replace(/\s/gi, '')}-${GBAdminService.getNumberIdentifier()}.${ext}`
|
|
|
|
);
|
2024-09-06 15:30:03 -03:00
|
|
|
fs.writeFileSync(localName1, buf, { encoding: null });
|
2023-11-27 14:40:08 -03:00
|
|
|
|
2024-09-06 15:30:03 -03:00
|
|
|
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName1));
|
2023-09-30 20:43:39 -03:00
|
|
|
}
|
|
|
|
|
2023-08-13 17:42:16 -03:00
|
|
|
if (!url) {
|
2024-09-06 15:30:03 -03:00
|
|
|
const ext = path.extname(filename.localName);
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2023-08-10 22:28:59 -03:00
|
|
|
// Prepare a cache to be referenced by Bot Framework.
|
2024-01-11 14:42:00 -03:00
|
|
|
|
2024-09-06 15:30:03 -03:00
|
|
|
const buf = fs.readFileSync(filename);
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2024-09-06 15:30:03 -03:00
|
|
|
const localName = path.join('work', gbaiName, 'cache', `tmp${GBAdminService.getRndReadableIdentifier()}.${ext}`);
|
|
|
|
fs.writeFileSync(localName, buf, { encoding: null });
|
|
|
|
url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
|
2023-08-13 17:42:16 -03:00
|
|
|
}
|
2022-06-21 17:46:46 -03:00
|
|
|
|
2023-08-13 17:42:16 -03:00
|
|
|
const contentType = mime.lookup(url);
|
|
|
|
const reply = { type: ActivityTypes.Message, text: caption };
|
|
|
|
reply['attachments'] = [];
|
|
|
|
reply['attachments'].push({
|
|
|
|
name: nameOnly,
|
|
|
|
contentType: contentType,
|
|
|
|
contentUrl: url
|
|
|
|
});
|
|
|
|
|
2023-08-22 11:03:46 -03:00
|
|
|
if (channel === 'omnichannel' || !user) {
|
2023-08-13 17:42:16 -03:00
|
|
|
await min.conversationalService.sendFile(min, null, mobile, url, caption);
|
|
|
|
} else {
|
|
|
|
await min.conversationalService['sendOnConversation'](min, user, reply);
|
2022-06-21 17:46:46 -03:00
|
|
|
}
|
2020-12-31 15:36:19 -03:00
|
|
|
}
|
2023-03-06 20:36:27 -03:00
|
|
|
/**
|
|
|
|
* Generates a new QRCode.
|
|
|
|
*
|
|
|
|
* file = QRCODE "data"
|
|
|
|
*
|
|
|
|
*/
|
2023-01-13 16:09:45 -03:00
|
|
|
public async getQRCode({ pid, text }) {
|
2023-03-06 09:09:31 -03:00
|
|
|
const { min, user } = await DialogKeywords.getProcessInfo(pid);
|
2022-07-15 09:05:16 -03:00
|
|
|
const img = await qrcode.toDataURL(text);
|
2022-11-19 23:34:58 -03:00
|
|
|
const data = img.replace(/^data:image\/\w+;base64,/, '');
|
|
|
|
const buf = Buffer.from(data, 'base64');
|
2023-03-06 09:09:31 -03:00
|
|
|
|
2024-09-06 15:15:42 -03:00
|
|
|
const gbaiName = GBUtil.getGBAIPath(min.botId);
|
2024-09-06 15:30:03 -03:00
|
|
|
const localName = path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`);
|
|
|
|
fs.writeFileSync(localName, buf, { encoding: null });
|
|
|
|
const url = urlJoin(GBServer.globals.publicAddress, min.botId, 'cache', path.basename(localName));
|
2023-03-06 09:09:31 -03:00
|
|
|
|
2023-03-06 20:36:27 -03:00
|
|
|
return { data: data, localName: localName, url: url };
|
2022-07-15 09:05:16 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
}
|