fix(all): Group improvements, group type in from column.

This commit is contained in:
Rodrigo Rodriguez 2021-12-25 22:13:52 -03:00
parent 0fe8b8c1b6
commit 55fdcbb9b5
4 changed files with 108 additions and 39 deletions

View file

@ -322,12 +322,14 @@ export class DialogKeywords {
); );
const nowUTC = new Date(); const nowUTC = new Date();
const now = new Date((typeof nowUTC === 'string' ? const now= typeof nowUTC === 'string' ?
new Date(nowUTC) : new Date(nowUTC) :
nowUTC).toLocaleString(this.getContentLocaleWithCulture(contentLocale), nowUTC;
{ timeZone: process.env.DEFAULT_TIMEZONE }));
const nowText = now.toLocaleString(this.getContentLocaleWithCulture(contentLocale),
return now.getHours().toString().padStart(2, "0") + ':' + now.getMinutes().toString().padStart(2, "0"); { 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];
} }
/** /**

View file

@ -191,6 +191,7 @@ export class GBMinService {
removeRoute(GBServer.globals.server, uiUrl); removeRoute(GBServer.globals.server, uiUrl);
GBServer.globals.minInstances = GBServer.globals.minInstances.filter(p => p.instance.botId !== botId); GBServer.globals.minInstances = GBServer.globals.minInstances.filter(p => p.instance.botId !== botId);
} }
/** /**
@ -207,6 +208,7 @@ export class GBMinService {
GBServer.globals.sysPackages, GBServer.globals.sysPackages,
GBServer.globals.appPackages GBServer.globals.appPackages
); );
min['groupCache'] = await KBService.getGroupReplies(instance.instanceId);
GBServer.globals.minInstances.push(min); GBServer.globals.minInstances.push(min);
await this.deployer.deployPackage(min, 'packages/default.gbtheme'); await this.deployer.deployPackage(min, 'packages/default.gbtheme');
@ -789,14 +791,6 @@ export class GBMinService {
let mobile = WhatsappDirectLine.mobiles[step.context.activity.conversation.id] let mobile = WhatsappDirectLine.mobiles[step.context.activity.conversation.id]
return mobile; 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'];
}
} }
@ -823,7 +817,9 @@ export class GBMinService {
await adapter['processActivity'](req, res, async context => { 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 // Get loaded user state

View file

@ -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(); text = text.trim();
const service = new CSService(); const service = new CSService();
let question = await service.getQuestionFromAlternateText(instanceId, text); let question = await service.getQuestionFromAlternateText(instanceId, text);
if (!question) { if (!question) {
const where={
instanceId: instanceId,
content: { [Op.like]: `%[^a-z]${text}[^a-z]%` }
};
if (from)
{
where['from']= from;
}
question = await GuaribasQuestion.findOne({ question = await GuaribasQuestion.findOne({
where: { where: where
instanceId: instanceId,
content: { [Op.like]: `%[^a-z]${text}[^a-z]%` }
}
}); });
} }
if (!question) { if (!question) {
@ -235,6 +241,9 @@ export class KBService implements IGBKBService {
return undefined; return undefined;
} }
public async addAnswer(obj: GuaribasAnswer): Promise<GuaribasAnswer> { public async addAnswer(obj: GuaribasAnswer): Promise<GuaribasAnswer> {
return await GuaribasAnswer.create(obj); return await GuaribasAnswer.create(obj);
} }
@ -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( public async importKbTabularFile(
filePath: string, filePath: string,
instanceId: number, instanceId: number,
@ -713,6 +728,9 @@ export class KBService implements IGBKBService {
GBDeployer.mountGBKBAssets(packageName, min.botId, localPath); GBDeployer.mountGBKBAssets(packageName, min.botId, localPath);
await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex)); await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
min['groupCache'] = await KBService.getGroupReplies(instance.instanceId);
GBLog.info(`[GBDeployer] Finished import of ${localPath}`); GBLog.info(`[GBDeployer] Finished import of ${localPath}`);
} }

View file

@ -41,6 +41,7 @@ import { GBServer } from '../../../src/app';
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService'; import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService';
import { SecService } from '../../security.gbapp/services/SecService'; import { SecService } from '../../security.gbapp/services/SecService';
import { Messages } from '../strings'; import { Messages } from '../strings';
import { KBService } from '../../kb.gbapp/services/KBService';
/** /**
* Support for Whatsapp. * Support for Whatsapp.
@ -51,7 +52,7 @@ export class WhatsappDirectLine extends GBService {
public static mobiles = {}; public static mobiles = {};
public static chatIds = {}; public static chatIds = {};
public pollInterval = 5000; public pollInterval = 3000;
public directLineClientName = 'DirectLineClient'; public directLineClientName = 'DirectLineClient';
public directLineClient: any; public directLineClient: any;
@ -133,7 +134,7 @@ export class WhatsappDirectLine extends GBService {
} }
public async resetConversationId(number, group) { public async resetConversationId(number, group) {
WhatsappDirectLine.conversationIds[number+group] = undefined; WhatsappDirectLine.conversationIds[number + group] = undefined;
} }
public async check() { public async check() {
@ -162,21 +163,7 @@ export class WhatsappDirectLine extends GBService {
const message = req.body.messages[0]; const message = req.body.messages[0];
let group = ""; let group = "";
let answerText = null;
// 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 text = message.body; let text = message.body;
@ -184,13 +171,72 @@ export class WhatsappDirectLine extends GBService {
const from = message.author.split('@')[0]; const from = message.author.split('@')[0];
const fromName = message.senderName; const fromName = message.senderName;
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);
if (req.body.messages[0].fromMe) { if (req.body.messages[0].fromMe) {
res.end(); res.end();
return; // Exit here. 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 CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
await e.onExchangeData(this.min, 'whatsappMessage', message); await e.onExchangeData(this.min, 'whatsappMessage', message);
@ -204,6 +250,13 @@ export class WhatsappDirectLine extends GBService {
senderName, '', 'whatsapp', senderName, null); senderName, '', 'whatsapp', senderName, null);
const locale = user.locale ? user.locale : 'pt'; 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 (message.type === 'ptt') {
if (process.env.AUDIO_DISABLED !== 'true') { if (process.env.AUDIO_DISABLED !== 'true') {
@ -221,7 +274,7 @@ export class WhatsappDirectLine extends GBService {
buf, locale buf, locale
); );
} else { } else {
await this.sendToDevice(user.userSystemId, await this.sendToDevice(user.userSystemId,
`No momento estou apenas conseguindo ler mensagens de texto.`, null); `No momento estou apenas conseguindo ler mensagens de texto.`, null);
} }
} }
@ -478,7 +531,7 @@ export class WhatsappDirectLine extends GBService {
await this.sendFileToDevice(to, url, 'Audio', msg); 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 '; const cmd = '/audio ';
if (msg.startsWith(cmd)) { if (msg.startsWith(cmd)) {