fix(core.gbapp): Text improvements in messages.

This commit is contained in:
Rodrigo Rodriguez 2020-12-14 09:28:12 -03:00
parent 46f4cac29b
commit f739c15431
3 changed files with 175 additions and 167 deletions

View file

@ -70,8 +70,8 @@ class SysClass {
encoding: 'binary' encoding: 'binary'
}; };
const res = await request(options); const res = await request(options);
return Buffer.from(res, 'binary').toString(); return Buffer.from(res, 'binary').toString();
} }
@ -115,88 +115,96 @@ class SysClass {
public async set(file: string, address: string, value: any): Promise<any> { public async set(file: string, address: string, value: any): Promise<any> {
GBLog.info(`BASIC: Defining '${address}' in '${file}' to '${value}' (SET). `); GBLog.info(`BASIC: Defining '${address}' in '${file}' to '${value}' (SET). `);
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId); let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
let siteId = process.env.STORAGE_SITE_ID; let siteId = process.env.STORAGE_SITE_ID;
let libraryId = process.env.STORAGE_LIBRARY; let libraryId = process.env.STORAGE_LIBRARY;
let client = MicrosoftGraph.Client.init({ let client = MicrosoftGraph.Client.init({
authProvider: done => { authProvider: done => {
done(null, token); done(null, token);
}
});
const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`;
let res = await client
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get();
let document = res.value.filter(m => {
return m.name.toLowerCase() === file.toLowerCase();
});
if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command SET not found. Check the file extension (.xlsx) and the associated .gbdata/.gbdialog.`;
} }
});
const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`;
let body = { values: [[]] }; address = address.indexOf(':') !== -1 ? address : address + ":" + address;
body.values[0][0] = value;
await client let res = await client
.api( .api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='${address}')` .get();
)
.patch(body); let document = res.value.filter(m => {
return m.name.toLowerCase() === file.toLowerCase();
});
if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command SET not found. Check the file extension (.xlsx) and the associated .gbdata/.gbdialog.`;
}
let body = { values: [[]] };
body.values[0][0] = value;
let sheets = await client
.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets`
)
.get();
await client
.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('${sheets.value[0].name}')/range(address='${address}')`
)
.patch(body);
} }
public async save(file: string, ...args): Promise<any> { public async save(file: string, ...args): Promise<any> {
GBLog.info(`BASIC: Saving '${file}' (SAVE). Args: ${args.join(',')}.`); GBLog.info(`BASIC: Saving '${file}' (SAVE). Args: ${args.join(',')}.`);
let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId); let token = await this.min.adminService.acquireElevatedToken(this.min.instance.instanceId);
let siteId = process.env.STORAGE_SITE_ID; let siteId = process.env.STORAGE_SITE_ID;
let libraryId = process.env.STORAGE_LIBRARY; let libraryId = process.env.STORAGE_LIBRARY;
let client = MicrosoftGraph.Client.init({ let client = MicrosoftGraph.Client.init({
authProvider: done => { authProvider: done => {
done(null, token); done(null, token);
}
});
const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`;
let res = await client
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get();
let document = res.value.filter(m => {
return m.name.toLowerCase() === file.toLowerCase();
});
await client
.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:Z2')/insert`
)
.post({});
if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command SAVE not found. Check the .gbdata or the .gbdialog associated.`;
}
if (args.length > 128) {
throw `File '${file}' has a SAVE call with more than 128 arguments. Check the .gbdialog associated.`;
} }
});
const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`;
let body = { values: [[]] }; let res = await client
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get();
for (let index = 0; index < 128; index++) { let document = res.value.filter(m => {
body.values[0][index] = args[index]; return m.name.toLowerCase() === file.toLowerCase();
} });
let res2 = await client await client
.api( .api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:DX2')` `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:Z2')/insert`
) )
.patch(body); .post({});
if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command SAVE not found. Check the .gbdata or the .gbdialog associated.`;
}
if (args.length > 128) {
throw `File '${file}' has a SAVE call with more than 128 arguments. Check the .gbdialog associated.`;
}
let body = { values: [[]] };
for (let index = 0; index < 128; index++) {
body.values[0][index] = args[index];
}
let res2 = await client
.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A2:DX2')`
)
.patch(body);
} }
public async get(file: string, address: string): Promise<any> { public async get(file: string, address: string): Promise<any> {
@ -212,31 +220,31 @@ class SysClass {
const botId = this.min.instance.botId; const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`; const path = `/${botId}.gbai/${botId}.gbdata`;
let res = await client let res = await client
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`) .api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get(); .get();
// Performs validation. // Performs validation.
let document = res.value.filter(m => { let document = res.value.filter(m => {
return m.name.toLowerCase() === file.toLowerCase(); return m.name.toLowerCase() === file.toLowerCase();
}); });
if (!document || document.length === 0) { if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command GET not found. Check the .gbdata or the .gbdialog associated.`; throw `File '${file}' specified on save GBasic command GET not found. Check the .gbdata or the .gbdialog associated.`;
} }
// Creates workbook session that will be discarded. // Creates workbook session that will be discarded.
let results = await client let results = await client
.api( .api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='${address}')` `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='${address}')`
) )
.get(); .get();
let val = results.text[0][0]; let val = results.text[0][0];
GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`); GBLog.info(`BASIC: Getting '${file}' (GET). Value= ${val}.`);
return val; return val;
} }
@ -253,72 +261,72 @@ class SysClass {
const botId = this.min.instance.botId; const botId = this.min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`; const path = `/${botId}.gbai/${botId}.gbdata`;
let res = await client let res = await client
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`) .api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
.get(); .get();
// Performs validation. // Performs validation.
let document = res.value.filter(m => { let document = res.value.filter(m => {
return m.name.toLowerCase() === file.toLowerCase(); return m.name.toLowerCase() === file.toLowerCase();
}); });
if (!document || document.length === 0) { if (!document || document.length === 0) {
throw `File '${file}' specified on save GBasic command FIND not found. Check the .gbdata or the .gbdialog associated.`; throw `File '${file}' specified on save GBasic command FIND not found. Check the .gbdata or the .gbdialog associated.`;
} }
if (args.length > 1) { if (args.length > 1) {
throw `File '${file}' has a FIND call with more than 1 arguments. Check the .gbdialog associated.`; throw `File '${file}' has a FIND call with more than 1 arguments. Check the .gbdialog associated.`;
}
// Creates workbook session that will be discarded.
const filter = args[0].split('=');
const columnName = filter[0];
const value = filter[1];
let results = await client
.api(
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A1:Z100')`
)
.get();
let columnIndex = 0;
const header = results.text[0];
for (; columnIndex < header.length; columnIndex++) {
if (header[columnIndex].toLowerCase() === columnName.toLowerCase()) {
break;
} }
}
// Creates workbook session that will be discarded. // As BASIC uses arrays starting with 1 (one) as index,
// a ghost element is added at 0 (zero) position.
const filter = args[0].split('='); let array = [];
const columnName = filter[0]; array.push({ 'this is a base 1': 'array' });
const value = filter[1]; let foundIndex = 0;
let results = await client for (; foundIndex < results.text.length; foundIndex++) {
.api( // Filter results action.
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('Sheet1')/range(address='A1:Z100')`
)
.get();
let columnIndex = 0; if (results.text[foundIndex][columnIndex].toLowerCase() === value.toLowerCase()) {
const header = results.text[0]; let output = {};
for (; columnIndex < header.length; columnIndex++) { const row = results.text[foundIndex];
if (header[columnIndex].toLowerCase() === columnName.toLowerCase()) { for (let colIndex = 0; colIndex < row.length; colIndex++) {
break; output[header[colIndex]] = row[colIndex];
} }
output['line'] = foundIndex + 1;
array.push(output);
} }
}
// As BASIC uses arrays starting with 1 (one) as index, if (array.length === 1) {
// a ghost element is added at 0 (zero) position. GBLog.info(`BASIC: FIND the data set is empty.`);
return null;
let array = []; } else if (array.length === 2) {
array.push({ 'this is a base 1': 'array' }); GBLog.info(`BASIC: FIND single result: ${array[0]}.`);
let foundIndex = 0; return array[1];
for (; foundIndex < results.text.length; foundIndex++) { } else {
// Filter results action. GBLog.info(`BASIC: FIND multiple result count: ${array.length}.`);
return array;
if (results.text[foundIndex][columnIndex].toLowerCase() === value.toLowerCase()) { }
let output = {};
const row = results.text[foundIndex];
for (let colIndex = 0; colIndex < row.length; colIndex++) {
output[header[colIndex]] = row[colIndex];
}
output['line'] = foundIndex + 1;
array.push(output);
}
}
if (array.length === 1) {
GBLog.info(`BASIC: FIND the data set is empty.`);
return null;
} else if (array.length === 2) {
GBLog.info(`BASIC: FIND single result: ${array[0]}.`);
return array[1];
} else {
GBLog.info(`BASIC: FIND multiple result count: ${array.length}.`);
return array;
}
} }
/** /**
@ -339,26 +347,26 @@ class SysClass {
const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name); const path = urlJoin(`/${botId}.gbai/${botId}.gbdata`, name);
return new Promise<any>((resolve, reject) => { return new Promise<any>((resolve, reject) => {
let client = MicrosoftGraph.Client.init({ let client = MicrosoftGraph.Client.init({
authProvider: done => { authProvider: done => {
done(null, token); done(null, token);
}
});
const body = {
"name": name,
"folder": {},
"@microsoft.graph.conflictBehavior": "rename"
}
client.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${path}:/children`)
.post(body, (err, res) => {
if (err) {
reject(err)
}
else {
resolve(res);
} }
}); });
const body = { });
"name": name,
"folder": {},
"@microsoft.graph.conflictBehavior": "rename"
}
client.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:/${path}:/children`)
.post(body, (err, res) => {
if (err) {
reject(err)
}
else {
resolve(res);
}
});
});
} }
/** /**

View file

@ -21,7 +21,7 @@ export const Messages = {
wider_answer: subjectText => wider_answer: subjectText =>
`Vou te responder de modo mais abrangente... Não apenas sobre ${subjectText}`, `Vou te responder de modo mais abrangente... Não apenas sobre ${subjectText}`,
which_question: 'Qual a pergunta?', which_question: 'Qual a pergunta?',
anything_else: 'Então, posso ajudar em algo mais?', anything_else: 'Posso ajudar em algo mais?',
here_is_subjects: 'Aqui estão algumas categorias de assuntos...', here_is_subjects: 'Aqui estão algumas categorias de assuntos...',
menu_select: 'Selecionar', menu_select: 'Selecionar',
lets_search: query => lets_search: query =>

View file

@ -166,7 +166,7 @@ export class WhatsappDirectLine extends GBService {
} }
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`); GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);
await CollectionUtil.asyncForEach(this.min.appPackages, async (e:IGBPackage) => { await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
await e.onExchangeData(this.min, "whatsappMessage", message); await e.onExchangeData(this.min, "whatsappMessage", message);
}); });
@ -252,7 +252,7 @@ export class WhatsappDirectLine extends GBService {
} }
} }
else if (user.agentMode === "bot" || user.agentMode === null) { else if (user.agentMode === "bot" || user.agentMode === null || user.agentMode === undefined) {
if (WhatsappDirectLine.conversationIds[from] === undefined) { if (WhatsappDirectLine.conversationIds[from] === undefined) {
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`); GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);