Skip to content

Commit

Permalink
fix(all): Group improvements, group type in from column.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Dec 26, 2021
1 parent 0fe8b8c commit 55fdcbb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 39 deletions.
12 changes: 7 additions & 5 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,14 @@ export class DialogKeywords {
);

const nowUTC = new Date();
const now = new Date((typeof nowUTC === 'string' ?
const now= typeof nowUTC === 'string' ?
new Date(nowUTC) :
nowUTC).toLocaleString(this.getContentLocaleWithCulture(contentLocale),
{ timeZone: process.env.DEFAULT_TIMEZONE }));

return now.getHours().toString().padStart(2, "0") + ':' + now.getMinutes().toString().padStart(2, "0");
nowUTC;

const nowText = now.toLocaleString(this.getContentLocaleWithCulture(contentLocale),
{ 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];
}

/**
Expand Down
14 changes: 5 additions & 9 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export class GBMinService {
removeRoute(GBServer.globals.server, uiUrl);

GBServer.globals.minInstances = GBServer.globals.minInstances.filter(p => p.instance.botId !== botId);

}

/**
Expand All @@ -207,6 +208,7 @@ export class GBMinService {
GBServer.globals.sysPackages,
GBServer.globals.appPackages
);
min['groupCache'] = await KBService.getGroupReplies(instance.instanceId);
GBServer.globals.minInstances.push(min);

await this.deployer.deployPackage(min, 'packages/default.gbtheme');
Expand Down Expand Up @@ -789,14 +791,6 @@ export class GBMinService {
let mobile = WhatsappDirectLine.mobiles[step.context.activity.conversation.id]
return mobile;

if (isNaN(step.context.activity['mobile'])) {
if (step.context.activity.from && !isNaN(step.context.activity.from.id)) {
return step.context.activity.from.id;
}
return null;
} else {
return step.context.activity['mobile'];
}
}


Expand All @@ -823,7 +817,9 @@ export class GBMinService {

await adapter['processActivity'](req, res, async context => {

context.activity.text = context.activity.text.replace(/\@General Bots Online /gi, '');
if (context.activity.text){
context.activity.text = context.activity.text.replace(/\@General Bots Online /gi, '');
}


// Get loaded user state
Expand Down
28 changes: 23 additions & 5 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,24 @@ export class KBService implements IGBKBService {

}

public async getAnswerByText(instanceId: number, text: string): Promise<any> {
public async getAnswerByText(instanceId: number, text: string, from: string = null): Promise<any> {
text = text.trim();

const service = new CSService();
let question = await service.getQuestionFromAlternateText(instanceId, text);

if (!question) {
const where={
instanceId: instanceId,
content: { [Op.like]: `%[^a-z]${text}[^a-z]%` }
};

if (from)
{
where['from']= from;
}
question = await GuaribasQuestion.findOne({
where: {
instanceId: instanceId,
content: { [Op.like]: `%[^a-z]${text}[^a-z]%` }
}
where: where
});
}
if (!question) {
Expand All @@ -235,6 +241,9 @@ export class KBService implements IGBKBService {
return undefined;
}




public async addAnswer(obj: GuaribasAnswer): Promise<GuaribasAnswer> {
return await GuaribasAnswer.create(obj);
}
Expand Down Expand Up @@ -382,6 +391,12 @@ export class KBService implements IGBKBService {
}
}

public static async getGroupReplies(instanceId: number): Promise<GuaribasQuestion[]> {
return await GuaribasQuestion.findAll({
where: { from: 'group', instanceId: instanceId }
});
}

public async importKbTabularFile(
filePath: string,
instanceId: number,
Expand Down Expand Up @@ -713,6 +728,9 @@ export class KBService implements IGBKBService {
GBDeployer.mountGBKBAssets(packageName, min.botId, localPath);

await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));

min['groupCache'] = await KBService.getGroupReplies(instance.instanceId);

GBLog.info(`[GBDeployer] Finished import of ${localPath}`);
}

Expand Down
93 changes: 73 additions & 20 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { GBServer } from '../../../src/app';
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService';
import { SecService } from '../../security.gbapp/services/SecService';
import { Messages } from '../strings';
import { KBService } from '../../kb.gbapp/services/KBService';

/**
* Support for Whatsapp.
Expand All @@ -51,7 +52,7 @@ export class WhatsappDirectLine extends GBService {
public static mobiles = {};
public static chatIds = {};

public pollInterval = 5000;
public pollInterval = 3000;
public directLineClientName = 'DirectLineClient';

public directLineClient: any;
Expand Down Expand Up @@ -133,7 +134,7 @@ export class WhatsappDirectLine extends GBService {
}

public async resetConversationId(number, group) {
WhatsappDirectLine.conversationIds[number+group] = undefined;
WhatsappDirectLine.conversationIds[number + group] = undefined;
}

public async check() {
Expand Down Expand Up @@ -162,35 +163,80 @@ export class WhatsappDirectLine extends GBService {

const message = req.body.messages[0];
let group = "";


// Ignore group messages without the mention to Bot.

if (message.chatName.charAt(0) !== '+') {
group = message.chatName;

let smsServiceNumber = this.min.core.getParam<string>(this.min.instance, 'whatsappServiceNumber', null);;
if (smsServiceNumber) {
smsServiceNumber = smsServiceNumber.replace('+', '');
if (!message.body.startsWith('@' + smsServiceNumber)) {
return;
}
}
}
let answerText = null;


let text = message.body;
text = text.replace(/\@\d+ /gi, '');

const from = message.author.split('@')[0];
const fromName = message.senderName;
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);


if (req.body.messages[0].fromMe) {
res.end();

return; // Exit here.
}
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);

if (message.chatName.charAt(0) !== '+') {
group = message.chatName;

let botGroupName = this.min.core.getParam<string>(this.min.instance, 'WhatsApp Group Name', null);
let botShortcuts = this.min.core.getParam<string>(this.min.instance, 'WhatsApp Group Shortcuts', null);
if (!botShortcuts) {
botShortcuts = new Array()
}
else {
botShortcuts = botShortcuts.split(' ');
}

const parts = text.split(' ');

// Bot name must be specified on config.

if (botGroupName === group) {

// Shortcut has been mentioned?

let found = false;
parts.forEach(e1 => {
botShortcuts.forEach(e2 => {
if (e1 === e2 && !found) {
found = true;
}
});


// Verify if it is a group cache answer.

const questions = this.min['groupCache'];
if (questions && questions.length > 0) {
questions.forEach(q => {
if (q.content === e1 && !found) {
const answer = this.min.kbService['getAnswerById'](this.min.instance.instanceId,
q.answerId);
answerText = answer.content;
}
});
}


// Ignore group messages without the mention to Bot.

let smsServiceNumber = this.min.core.getParam<string>(this.min.instance, 'whatsappServiceNumber', null);
if (smsServiceNumber && !answerText) {
smsServiceNumber = smsServiceNumber.replace('+', '');
if (!message.body.startsWith('@' + smsServiceNumber)) {
return;
}
}

});
}
}


await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
await e.onExchangeData(this.min, 'whatsappMessage', message);
Expand All @@ -204,6 +250,13 @@ export class WhatsappDirectLine extends GBService {
senderName, '', 'whatsapp', senderName, null);

const locale = user.locale ? user.locale : 'pt';

if (answerText) {
await this.sendToDeviceEx(user.userSystemId, answerText, locale, null);
return; // Exit here.
}


if (message.type === 'ptt') {

if (process.env.AUDIO_DISABLED !== 'true') {
Expand All @@ -221,7 +274,7 @@ export class WhatsappDirectLine extends GBService {
buf, locale
);
} else {
await this.sendToDevice(user.userSystemId,
await this.sendToDevice(user.userSystemId,
`No momento estou apenas conseguindo ler mensagens de texto.`, null);
}
}
Expand Down Expand Up @@ -478,7 +531,7 @@ export class WhatsappDirectLine extends GBService {
await this.sendFileToDevice(to, url, 'Audio', msg);
}

public async sendToDevice(to: string, msg: string, conversationId ) {
public async sendToDevice(to: string, msg: string, conversationId) {

const cmd = '/audio ';
if (msg.startsWith(cmd)) {
Expand Down

0 comments on commit 55fdcbb

Please sign in to comment.