new(basic.gblib): #256 coding for loading sheet file and HEAR param processing.

This commit is contained in:
rodrigorodriguez 2023-02-23 17:41:29 -03:00
parent cc15b7cc3a
commit a0211ff441
2 changed files with 93 additions and 29 deletions

View file

@ -58,6 +58,7 @@ import mammoth from 'mammoth';
import qrcode from 'qrcode';
import { json } from 'body-parser';
import { WebAutomationServices } from './WebAutomationServices.js';
import urljoin from 'url-join';
/**
* Default check interval for user replay
@ -606,9 +607,7 @@ export class DialogKeywords {
return names.indexOf(name) > -1;
}
private async setOption({pid, name, value})
{
private async setOption({ pid, name, value }) {
if (this.isUserSystemParam(name)) {
throw new Error(`Not possible to define ${name} as it is a reserved system param name.`);
}
@ -620,8 +619,7 @@ export class DialogKeywords {
return { min, user, params };
}
private async getOption({pid, name})
{
private async getOption({ pid, name }) {
if (this.isUserSystemParam(name)) {
throw new Error(`Not possible to retrieve ${name} system param.`);
}
@ -638,7 +636,7 @@ export class DialogKeywords {
*
*/
public async setMaxLines({ pid, count }) {
await this.setOption({pid, name: "maxLines", value: count});
await this.setOption({ pid, name: 'maxLines', value: count });
}
/**
@ -661,7 +659,6 @@ export class DialogKeywords {
await this.getOption({ pid, name });
}
/**
* Defines the maximum lines to scan in spreedsheets.
*
@ -669,7 +666,7 @@ export class DialogKeywords {
*
*/
public async setMaxColumns({ pid, count }) {
await this.setOption({pid, name: "setMaxColumns", value: count});
await this.setOption({ pid, name: 'setMaxColumns', value: count });
}
/**
@ -679,8 +676,8 @@ export class DialogKeywords {
*
*/
public async setWholeWord({ pid, on }) {
const value = (on.trim() === "on");
await this.setOption({pid, name: "wholeWord", value: value});
const value = on.trim() === 'on';
await this.setOption({ pid, name: 'wholeWord', value: value });
}
/**
@ -691,7 +688,7 @@ export class DialogKeywords {
*/
public async setTheme({ pid, theme }) {
const value = theme.trim();
await this.setOption({pid, name: "theme", value: value});
await this.setOption({ pid, name: 'theme', value: value });
}
/**
@ -701,8 +698,8 @@ export class DialogKeywords {
*
*/
public async setTranslatorOn({ pid, on }) {
const value = (on.trim() === "on");
await this.setOption({pid, name: "translatorOn", value: value});
const value = on.trim() === 'on';
await this.setOption({ pid, name: 'translatorOn', value: value });
}
/**
@ -732,7 +729,6 @@ export class DialogKeywords {
// return await beginDialog('/menu');
}
/**
* Performs the transfer of the conversation to a human agent.
*
@ -831,7 +827,68 @@ export class DialogKeywords {
const answer = min.cbMap[userId].promise;
if (kind === 'file') {
if (kind === 'sheet') {
// Retrieves the .xlsx file associated with the HEAR var AS file.xlsx.
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(this.min);
const botId = min.instance.botId;
const path = urljoin(`${botId}.gbai`, `${botId}.gbdata`);
let url = `${baseUrl}/drive/root:/${path}:/children`;
GBLog.info(`Loading HEAR AS .xlsx options from Sheet: ${url}`);
const res = await client.api(url).get();
// Finds .xlsx specified by arg.
const document = res.value.filter(m => {
return m.name === arg;
});
if (document === undefined || document.length === 0) {
GBLog.info(`${arg} not found on .gbdata folder, check the package.`);
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] !== '') {
list.push( results.text[index][0]);
}
else
{
break;
}
}
// 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) {
await this.getTalk({ pid, text: `Escolha por favor um dos itens sugeridos.` });
return await this.getHear({ pid, kind, arg });
}
} else if (kind === 'file') {
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
const handle = WebAutomationServices.cyrb53(this.min.botId + answer.filename);
GBServer.globals.files[handle] = answer;

View file

@ -170,6 +170,13 @@ export class KeywordsExpressions {
}
];
keywords[i++] = [
/^\s*hear (\w+) as (\w+( \w+)*.xlsx)/gim,
($0, $1, $2) => {
return `${$1} = await dk.getHear({pid: pid, kind:"sheet", arg: "${$2}"})`;
}
];
keywords[i++] = [
/^\s*hear (\w+) as\s*login/gim,
($0, $1) => {