new(basic.gblib): Web Automation CAPTCHA processing.

This commit is contained in:
Rodrigo Rodriguez 2022-06-21 17:46:46 -03:00
parent 100cf31250
commit 696e867364
4 changed files with 316 additions and 318 deletions

View file

@ -244,7 +244,7 @@ export class DialogKeywords {
public async type(step, page, idOrName, text) { public async type(step, page, idOrName, text) {
const e = await this.getBySelector(page, idOrName); const e = await this.getBySelector(page, idOrName);
await e.type(text); await e.type(text);
} }
/** /**
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy. * Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
@ -261,7 +261,7 @@ export class DialogKeywords {
} }
return await tesseract.recognize(localFile, config); return await tesseract.recognize(localFile, config);
} }
/** /**
* Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy. * Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
@ -293,7 +293,7 @@ export class DialogKeywords {
default: default:
return [year, month, day].join('/'); return [year, month, day].join('/');
} }
} }
/** /**
* Quits the dialog, currently required to get out of VM context. * Quits the dialog, currently required to get out of VM context.
@ -305,7 +305,7 @@ export class DialogKeywords {
await this.browser.close(); await this.browser.close();
} }
await step.endDialog(); await step.endDialog();
} }
/** /**
* Get active tasks. * Get active tasks.
@ -315,7 +315,7 @@ export class DialogKeywords {
public async getActiveTasks() { public async getActiveTasks() {
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY); let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
return await s.getActiveTasks(); return await s.getActiveTasks();
} }
/** /**
* Creates a new deal. * Creates a new deal.
@ -326,7 +326,7 @@ export class DialogKeywords {
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY); let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
let deal = await s.createDeal(dealName, contact, company, amount); let deal = await s.createDeal(dealName, contact, company, amount);
return deal; return deal;
} }
/** /**
* Finds contacts in XRM. * Finds contacts in XRM.
@ -336,7 +336,7 @@ export class DialogKeywords {
public async fndContact(name) { public async fndContact(name) {
let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY); let s = new HubSpotServices(null, null, process.env.HUBSPOT_KEY);
return await s.searchContact(name); return await s.searchContact(name);
} }
public getContentLocaleWithCulture(contentLocale) { public getContentLocaleWithCulture(contentLocale) {
@ -351,7 +351,7 @@ export class DialogKeywords {
return 'en-us'; return 'en-us';
} }
} }
/** /**
* Returns specified date week day in format 'Mon'. * Returns specified date week day in format 'Mon'.
@ -380,7 +380,7 @@ export class DialogKeywords {
return week.substr(0, 3); return week.substr(0, 3);
} }
return 'NULL'; return 'NULL';
} }
/** /**
* Returns an object ready to get information about difference in several ways * Returns an object ready to get information about difference in several ways
@ -407,7 +407,7 @@ export class DialogKeywords {
case 'hour': return diff.hours(); case 'hour': return diff.hours();
case 'minute': return diff.minutes(); case 'minute': return diff.minutes();
} }
} }
/** /**
* Returns specified date week day in format 'Mon'. * Returns specified date week day in format 'Mon'.
@ -435,7 +435,7 @@ export class DialogKeywords {
default: ret = undefined; break; default: ret = undefined; break;
} }
return ret; return ret;
} }
@ -458,7 +458,7 @@ export class DialogKeywords {
array = array.join(", "); array = array.join(", ");
return array; return array;
} }
/** /**
* Returns the specified time in format hh:dd. * Returns the specified time in format hh:dd.
@ -489,7 +489,7 @@ export class DialogKeywords {
return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes()); return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
} }
return 'NULL'; return 'NULL';
} }
/** /**
* Returns current time in format hh:dd. * Returns current time in format hh:dd.
@ -513,7 +513,7 @@ export class DialogKeywords {
{ timeZone: process.env.DEFAULT_TIMEZONE }); { timeZone: process.env.DEFAULT_TIMEZONE });
return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0]; return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0];
} }
/** /**
* Sends a file to a given mobile. * Sends a file to a given mobile.
@ -524,7 +524,7 @@ export class DialogKeywords {
public async sendFileTo(step, mobile, filename, caption) { public async sendFileTo(step, mobile, filename, caption) {
GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`); GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`);
return await this.internalSendFile(null, mobile, filename, caption); return await this.internalSendFile(null, mobile, filename, caption);
} }
/** /**
* Sends a file to the current user. * Sends a file to the current user.
@ -536,7 +536,7 @@ export class DialogKeywords {
const mobile = await this.userMobile(step); const mobile = await this.userMobile(step);
GBLog.info(`BASIC: SEND FILE (current: ${mobile}, filename '${filename}'.`); GBLog.info(`BASIC: SEND FILE (current: ${mobile}, filename '${filename}'.`);
return await this.internalSendFile(step, mobile, filename, caption); return await this.internalSendFile(step, mobile, filename, caption);
} }
/** /**
* Defines the current language of the bot conversation. * Defines the current language of the bot conversation.
@ -552,7 +552,7 @@ export class DialogKeywords {
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines the maximum lines to scan in spreedsheets. * Defines the maximum lines to scan in spreedsheets.
@ -565,7 +565,7 @@ export class DialogKeywords {
user.basicOptions.maxLines = count; user.basicOptions.maxLines = count;
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines the FIND behaviour to consider whole words while searching. * Defines the FIND behaviour to consider whole words while searching.
@ -578,7 +578,7 @@ export class DialogKeywords {
user.basicOptions.wholeWord = (on.trim() === "on"); user.basicOptions.wholeWord = (on.trim() === "on");
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines the theme for assets generation. * Defines the theme for assets generation.
@ -591,7 +591,7 @@ export class DialogKeywords {
user.basicOptions.theme = theme.trim(); user.basicOptions.theme = theme.trim();
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
* Defines translator behaviour. * Defines translator behaviour.
@ -604,7 +604,7 @@ export class DialogKeywords {
user.basicOptions.translatorOn = (on.trim() === "on"); user.basicOptions.translatorOn = (on.trim() === "on");
await this.min.userProfile.set(step.context, user); await this.min.userProfile.set(step.context, user);
this.user = user; this.user = user;
} }
/** /**
@ -612,14 +612,14 @@ export class DialogKeywords {
*/ */
public async userName(step) { public async userName(step) {
return step ? WhatsappDirectLine.usernames[await this.userMobile(step)] : 'N/A'; return step ? WhatsappDirectLine.usernames[await this.userMobile(step)] : 'N/A';
} }
/** /**
* OBSOLETE. * OBSOLETE.
*/ */
public async getFrom(step) { public async getFrom(step) {
return step ? await this.userMobile(step) : 'N/A'; return step ? await this.userMobile(step) : 'N/A';
} }
/** /**
@ -630,7 +630,7 @@ export class DialogKeywords {
*/ */
public async userMobile(step) { public async userMobile(step) {
return GBMinService.userMobile(step); return GBMinService.userMobile(step);
} }
/** /**
* Shows the subject menu to the user * Shows the subject menu to the user
@ -640,7 +640,7 @@ export class DialogKeywords {
*/ */
public async showMenu(step) { public async showMenu(step) {
return await step.beginDialog('/menu'); return await step.beginDialog('/menu');
} }
/** /**
* Performs the transfer of the conversation to a human agent. * Performs the transfer of the conversation to a human agent.
@ -650,7 +650,7 @@ export class DialogKeywords {
*/ */
public async transferTo(step, to: string = null) { public async transferTo(step, to: string = null) {
return await step.beginDialog('/t', { to: to }); return await step.beginDialog('/t', { to: to });
} }
/** /**
* Hears something from user and put it in a variable * Hears something from user and put it in a variable
@ -670,27 +670,12 @@ export class DialogKeywords {
if (this.hrOn) { if (this.hrOn) {
let sleep = ms => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
};
// Waits for next message in HEAR delegated context. // Waits for next message in HEAR delegated context.
const mobile = await this.userMobile(step); const botId = this.min.botId;
while (true) { WhatsappDirectLine.state[botId + this.hrOn] = {
if (WhatsappDirectLine.state[mobile] === 3) { promise: promise, previousResolve: previousResolve
break; };
}
sleep(5000);
}
const result = WhatsappDirectLine.lastMessage[this.min.instance.botId + mobile];
opts = await promise(step, result);
if (previousResolve !== undefined) {
previousResolve(opts);
}
} }
else { else {
@ -700,7 +685,7 @@ export class DialogKeywords {
await step.beginDialog('/hear', opts); await step.beginDialog('/hear', opts);
} }
} }
} }
/** /**
* Prepares the next dialog to be shown to the specified user. * Prepares the next dialog to be shown to the specified user.
@ -722,16 +707,17 @@ export class DialogKeywords {
else { else {
await step.beginDialog(fromOrDialogName); await step.beginDialog(fromOrDialogName);
} }
} }
/** /**
* Talks to the user by using the specified text. * Talks to the user by using the specified text.
*/ */
public async talk(step, text: string) { public async talk(step, text: string) {
const translate = this.user ? this.user.basicOptions.translatorOn : false;
await this.min.conversationalService['sendTextWithOptions'](this.min, step, text, await this.min.conversationalService['sendTextWithOptions'](this.min, step, text,
this.user.basicOptions.translatorOn, null); translate, null);
} }
private static getChannel(step): string { private static getChannel(step): string {
if (!step) return 'whatsapp'; if (!step) return 'whatsapp';
@ -743,7 +729,7 @@ export class DialogKeywords {
} }
return 'webchat'; return 'webchat';
} }
} }
/** /**
@ -795,5 +781,5 @@ export class DialogKeywords {
await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption); await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
} }
} }
} }

View file

@ -175,8 +175,8 @@ export class GBVMService extends GBService {
code = `<%\n code = `<%\n
id = sys().getRandomId() id = sys().getRandomId()
username = this.userName(step); username = step ? this.userName(step) : sys().getRandomId();
mobile = this.userMobile(step); mobile = step ? this.userMobile(step) : sys().getRandomId();
from = mobile; from = mobile;
ubound = function(array){return array.length}; ubound = function(array){return array.length};
isarray = function(array){return Array.isArray(array) }; isarray = function(array){return Array.isArray(array) };
@ -728,7 +728,7 @@ export class GBVMService extends GBService {
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE') GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
); );
if (step.context.activity['originalText']) { if (step && step.context.activity['originalText']) {
const entities = await min["nerEngine"].findEntities( const entities = await min["nerEngine"].findEntities(
step.context.activity['originalText'], step.context.activity['originalText'],
contentLocale); contentLocale);

View file

@ -188,8 +188,6 @@ export class GBMinService {
}); });
GBLog.info(`Package deployment done.`); GBLog.info(`Package deployment done.`);
} }
@ -524,7 +522,7 @@ export class GBMinService {
} }
} catch (error) { } catch (error) {
GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error}`); GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error} ${error.stack}`);
} }
} }
@ -1249,6 +1247,8 @@ export class GBMinService {
} else if (cmdOrDialogName === '/call') { } else if (cmdOrDialogName === '/call') {
await GBVMService.callVM(args, min, step, this.deployer); await GBVMService.callVM(args, min, step, this.deployer);
} else if (cmdOrDialogName === '/callsch') {
await GBVMService.callVM(args, min, null, null);
} else { } else {
await step.beginDialog(cmdOrDialogName, { args: args }); await step.beginDialog(cmdOrDialogName, { args: args });
} }

View file

@ -278,6 +278,18 @@ export class WhatsappDirectLine extends GBService {
} }
} }
const botId = this.min.instance.botId;
const state = WhatsappDirectLine.state[botId + from];
if ( state) {
WhatsappDirectLine.state[botId + from] = null;
await state.promise(null, message.text);
return; // Exit here.
};
// Processes .gbapp message interception.
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);
@ -291,7 +303,9 @@ export class WhatsappDirectLine extends GBService {
if (answerText) { if (answerText) {
await this.sendToDeviceEx(user.userSystemId, answerText, locale, null); await this.sendToDeviceEx(user.userSystemId, answerText, locale, null);
return; // Exit here. return; // Exit here.
} }
if (message.type === 'ptt') { if (message.type === 'ptt') {
@ -315,14 +329,12 @@ export class WhatsappDirectLine extends GBService {
`No momento estou apenas conseguindo ler mensagens de texto.`, null); `No momento estou apenas conseguindo ler mensagens de texto.`, null);
} }
} }
const botId = this.min.instance.botId;
const conversationId = WhatsappDirectLine.conversationIds[botId + from + group]; const conversationId = WhatsappDirectLine.conversationIds[botId + from + group];
const client = await this.directLineClient; const client = await this.directLineClient;
WhatsappDirectLine.lastMessage[botId + from] = message; WhatsappDirectLine.lastMessage[botId + from] = message;
// Check if this message is from a Human Agent itself. // Check if this message is from a Human Agent itself.
if (user.agentMode === 'self') { if (user.agentMode === 'self') {