Skip to content

Commit

Permalink
fix(basic.gblib): #227 - HEAR AS FILE and GET/SET PARAM.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Feb 1, 2023
1 parent 82a9ba9 commit 2521117
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 76 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"adm-zip": "0.5.9",
"alasql": "2.1.6",
"any-shell-escape": "0.1.1",
"arraybuffer-to-buffer": "^0.0.7",
"async-promises": "0.2.3",
"basic-auth": "2.0.1",
"billboard.js": "3.6.3",
Expand Down
5 changes: 3 additions & 2 deletions packages/analytics.gblib/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ export class AnalyticsService {

public async createMessage (
instanceId: number,
conversation: GuaribasConversation,
conversationId: number,
userId: number,
content: string
): Promise<GuaribasConversationMessage> {

const message = GuaribasConversationMessage.build();
message.content = typeof content === 'object' ? JSON.stringify(content) : content;
message.instanceId = instanceId;
message.userId = userId;
message.conversationId = conversation.conversationId;
message.conversationId = conversationId;

return await message.save();
}
Expand Down
81 changes: 68 additions & 13 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,29 @@ export class DialogKeywords {
this['id'] = this.sys().getRandomId();
}

private isUserSystemParam(name: string): Boolean {
const names = [
'welcomed',
'loaded',
'subjects',
'cb',
'welcomed',
'maxLines',
'translatorOn',
'wholeWord',
'theme',
'maxColumns'
];

return names.indexOf(name) > -1;
}


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.`);
}
const process = GBServer.globals.processes[pid];
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const sec = new SecService();
Expand All @@ -598,6 +619,17 @@ export class DialogKeywords {
return { min, user, params };
}

private async getOption({pid, name})
{
if (this.isUserSystemParam(name)){
throw new Error(`Not possible to retrieve ${name} system param.`);
}
const process = GBServer.globals.processes[pid];
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const sec = new SecService();
return await sec.getParam(user, name);
}

/**
* Defines the maximum lines to scan in spreedsheets.
*
Expand All @@ -608,6 +640,27 @@ export class DialogKeywords {
await this.setOption({pid, name: "maxLines", value: count});
}

/**
* Defines a custom user param to be persisted to storage.
*
* @example SET PARAM name AS value
*
*/
public async setUserParam({ pid, name, value }) {
await this.setOption({pid, name, value});
}

/**
* Returns a custom user param persisted on storage.
*
* @example GET PARAM name
*
*/
public async getUserParam({ pid, name }) {
await this.getOption({pid, name});
}


/**
* Defines the maximum lines to scan in spreedsheets.
*
Expand Down Expand Up @@ -772,12 +825,14 @@ export class DialogKeywords {
await sleep(DEFAULT_HEAR_POLL_INTERVAL);
}

const text = min.cbMap[userId].promise;
const answer = min.cbMap[userId].promise;

if (kind === 'file') {
GBLog.info(`BASIC (${min.botId}): Upload done for ${answer.filename}.`);
// TODO: answer.filename, answer.data.

} else if (kind === 'boolean') {
if (isIntentYes('pt-BR', text)) {
if (isIntentYes('pt-BR', answer)) {
result = true;
} else {
result = false;
Expand All @@ -787,7 +842,7 @@ export class DialogKeywords {
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null) {
await this.talk({ pid, text: 'Por favor, digite um e-mail válido.' });
Expand All @@ -800,7 +855,7 @@ export class DialogKeywords {
return text.match(/[_a-zA-Z][_a-zA-Z0-9]{0,16}/gi);
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite um nome válido.' });
Expand All @@ -813,7 +868,7 @@ export class DialogKeywords {
return text.match(/\d+/gi);
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite um número válido.' });
Expand All @@ -828,7 +883,7 @@ export class DialogKeywords {
);
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite uma data no formato 12/12/2020.' });
Expand All @@ -841,7 +896,7 @@ export class DialogKeywords {
return text.match(/^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$/gi);
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite um horário no formato hh:ss.' });
Expand All @@ -860,7 +915,7 @@ export class DialogKeywords {
return [];
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite um valor monetário.' });
Expand All @@ -872,7 +927,7 @@ export class DialogKeywords {
let phoneNumber;
try {
// https://github.com/GeneralBots/BotServer/issues/307
phoneNumber = phone(text, { country: 'BRA' })[0];
phoneNumber = phone(answer, { country: 'BRA' })[0];
phoneNumber = phoneUtil.parse(phoneNumber);
} catch (error) {
await this.talk({ pid, text: Messages[locale].validation_enter_valid_mobile });
Expand All @@ -897,7 +952,7 @@ export class DialogKeywords {
}
};

const value = extractEntity(text);
const value = extractEntity(answer);

if (value === null || value.length != 1) {
await this.talk({ pid, text: 'Por favor, digite um CEP válido.' });
Expand All @@ -909,7 +964,7 @@ export class DialogKeywords {
const list = args;
result = null;
await CollectionUtil.asyncForEach(list, async item => {
if (GBConversationalService.kmpSearch(text, item) != -1) {
if (GBConversationalService.kmpSearch(answer, item) != -1) {
result = item;
}
});
Expand Down Expand Up @@ -939,8 +994,8 @@ export class DialogKeywords {

await CollectionUtil.asyncForEach(list, async item => {
if (
GBConversationalService.kmpSearch(text.toLowerCase(), item.name.toLowerCase()) != -1 ||
GBConversationalService.kmpSearch(text.toLowerCase(), item.code.toLowerCase()) != -1
GBConversationalService.kmpSearch(answer.toLowerCase(), item.name.toLowerCase()) != -1 ||
GBConversationalService.kmpSearch(answer.toLowerCase(), item.code.toLowerCase()) != -1
) {
result = item.code;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,20 @@ export class GBVMService extends GBService {
}
];

keywords[i++] = [
/^\s*set param \s*(.*)\s*as\s*(.*)/gim,
($0, $1, $2) => {
return `await dk.setUserParam ({pid: pid, ${$1}}, ${$2})`;
}
];

keywords[i++] = [
/^\s*get param \s*(.*)/gim,
($0, $1, $2) => {
return `await dk.getUserParam ({pid: pid, ${$1}})`;
}
];

keywords[i++] = [
/^\s*set header\s*(.*)\s*as\s*(.*)/gim,
($0, $1, $2) => {
Expand Down
Loading

0 comments on commit 2521117

Please sign in to comment.