fix(basic.gblib): #307 - Fixed user context in API.

This commit is contained in:
rodrigorodriguez 2023-01-31 11:46:37 -03:00
parent 20911af3d7
commit cbce44f20d
3 changed files with 76 additions and 76 deletions

View file

@ -427,9 +427,7 @@ export class DialogKeywords {
*
*/
public async getToLst(pid, array, member) {
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
if (!array) {
return '<Empty>';
@ -456,9 +454,7 @@ export class DialogKeywords {
*
*/
public async getHourFromDate(pid, date) {
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
function addZero(i) {
if (i < 10) {
@ -565,7 +561,7 @@ export class DialogKeywords {
*
*/
public async sendFile({ pid, filename, caption }) {
const mobile = await this.userMobile();
const mobile = await this.userMobile({pid});
GBLog.info(`BASIC: SEND FILE (current: ${mobile},filename '${filename}'.`);
return await this.internalSendFile({ pid, mobile, filename, caption });
}
@ -592,18 +588,24 @@ export class DialogKeywords {
this['id'] = this.sys().getRandomId();
}
private async setOption({pid, name, value})
{
const process = GBServer.globals.processes[pid];
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
const sec = new SecService();
await sec.setParam(user.userId, name, value);
GBLog.info(`BASIC: ${name} = ${value} (botId: ${min.botId})`);
return { min, user, params };
}
/**
* Defines the maximum lines to scan in spreedsheets.
*
* @example SET MAX LINES 5000
*
*/
public async setMaxLines({ count }) {
if (this.user) {
// #307 user.basicOptions.maxLines = count;
} else {
this.maxLines = count;
}
public async setMaxLines({ pid, count }) {
await this.setOption({pid, name: "maxLines", value: count});
}
/**
@ -612,8 +614,8 @@ export class DialogKeywords {
* @example SET MAX COLUMNS 5000
*
*/
public async setMaxColumns({ count }) {
// #307 user.basicOptions.maxColumns = count;
public async setMaxColumns({ pid, count }) {
await this.setOption({pid, name: "setMaxColumns", value: count});
}
/**
@ -622,8 +624,9 @@ export class DialogKeywords {
* @example SET WHOLE WORD ON
*
*/
public async setWholeWord({ on }) {
// #307 user.basicOptions.wholeWord = (on.trim() === "on");
public async setWholeWord({ pid, on }) {
const value = (on.trim() === "on");
await this.setOption({pid, name: "wholeWord", value: value});
}
/**
@ -632,8 +635,9 @@ export class DialogKeywords {
* @example SET THEME "themename"
*
*/
public async setTheme({ theme }) {
// #307 user.basicOptions.theme = theme.trim();
public async setTheme({ pid, theme }) {
const value = theme.trim();
await this.setOption({pid, name: "theme", value: value});
}
/**
@ -642,24 +646,25 @@ export class DialogKeywords {
* @example SET TRANSLATOR ON | OFF
*
*/
public async setTranslatorOn({ on }) {
// #307 user.basicOptions.translatorOn = (on.trim() === "on");
public async setTranslatorOn({ pid, on }) {
const value = (on.trim() === "on");
await this.setOption({pid, name: "translatorOn", value: value});
}
/**
* Returns the name of the user acquired by WhatsApp API.
*/
public async userName() {
// #307 WhatsappDirectLine.usernames[await this.userMobile()] : 'N/A';
return this.sys().getRandomId();
public async userName({pid}) {
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
return user.userName;
}
/**
* Returns current mobile number from user in conversation.
*/
public async userMobile() {
// #307 return GBMinService.userMobile();
return this.sys().getRandomId();
public async userMobile({pid}) {
let { min, user, params } = await DialogKeywords.getProcessInfo(pid);
return user.userSystemId;
}
/**
@ -672,6 +677,7 @@ export class DialogKeywords {
// https://github.com/GeneralBots/BotServer/issues/237
// return await beginDialog('/menu');
}
private static async downloadAttachmentAndWrite(attachment) {
const url = attachment.contentUrl;
// https://github.com/GeneralBots/BotServer/issues/195 - '${botId}','uploads');
@ -730,11 +736,8 @@ export class DialogKeywords {
*
*/
public async getHear({ pid, kind, arg }) {
const process = GBServer.globals.processes[pid];
let {
min, user
} = await DialogKeywords.getProcessInfo(pid);
let { min, user } = await DialogKeywords.getProcessInfo(pid);
// Handles first arg as an array of args.
@ -765,8 +768,6 @@ export class DialogKeywords {
// https://github.com/GeneralBots/BotServer/issues/266
if (args && args.length > 1) {
// https://github.com/pedroslopez/whatsapp-web.js/issues/1811
//
// const list = new List(
@ -781,7 +782,6 @@ export class DialogKeywords {
// 'Please select a product'
// );
// let i = 0;
// await CollectionUtil.asyncForEach(args, async arg => {
// i++;
@ -792,7 +792,6 @@ export class DialogKeywords {
// const button = new wpp.Buttons(Messages[locale].choices, choices, ' ', ' ');
// await this.talk(button);
GBLog.info(`BASIC: HEAR with [${args.toString()}] (Asking for input).`);
} else {
GBLog.info('BASIC: HEAR (Asking for input).');
@ -916,8 +915,7 @@ export class DialogKeywords {
// https://github.com/GeneralBots/BotServer/issues/307
if (user.locale === 'en') {
return text.match(/(?:\d{1,3},)*\d{1,3}(?:\.\d+)?/gi);
}
else {
} else {
return text.match(/(?:\d{1,3}.)*\d{1,3}(?:\,\d+)?/gi);
}
return [];
@ -1053,24 +1051,23 @@ export class DialogKeywords {
public static async getProcessInfo(pid: number) {
const proc = GBServer.globals.processes[pid];
const min = GBServer.globals.minInstances.filter(p =>
p.instance.instanceId == proc.instanceId)[0];
const min = GBServer.globals.minInstances.filter(p => p.instance.instanceId == proc.instanceId)[0];
const sec = new SecService();
const user = await sec.getUserFromId(min.instance.instanceId, proc.userId);
const params = JSON.parse(user.params);
return {
min, user
min,
user,
params
};
}
/**
* Talks to the user by using the specified text.
*/
public async talk({ pid, text }) {
GBLog.info(`BASIC: TALK '${text}'.`);
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
if (user) {
// TODO: const translate = this.user ? this.user.basicOptions.translatorOn : false;
@ -1089,9 +1086,7 @@ export class DialogKeywords {
private async internalSendFile({ pid, mobile, filename, caption }) {
// Handles SEND FILE TO mobile,element in Web Automation.
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const element = filename._page ? filename._page : filename.screenshot ? filename : null;
if (element) {

View file

@ -1035,9 +1035,9 @@ export class GBVMService extends GBService {
};
sandbox['id'] = dk.sys().getRandomId();
sandbox['username'] = await dk.userName();
sandbox['mobile'] = await dk.userMobile();
sandbox['from'] = await dk.userMobile();
sandbox['username'] = await dk.userName({pid});
sandbox['mobile'] = await dk.userMobile({pid});
sandbox['from'] = await dk.userMobile({pid});
sandbox['ENTER'] = String.fromCharCode(13);
sandbox['headers'] = {};
sandbox['data'] = {};

View file

@ -449,6 +449,11 @@ export class SystemKeywords {
*
*/
public async set({ pid, file, address, value }): Promise<any> {
const {
min, user
} = await DialogKeywords.getProcessInfo(pid);
// Handles calls for HTML stuff
if (file._javascriptEnabled) {
@ -463,9 +468,9 @@ export class SystemKeywords {
GBLog.info(`BASIC: Defining '${address}' in '${file}' to '${value}' (SET). `);
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(this.min);
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
const botId = this.min.instance.botId;
const botId = min.instance.botId;
const path = `/${botId}.gbai/${botId}.gbdata`;
address = address.indexOf(':') !== -1 ? address : address + ':' + address;