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 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];
}
/**

View file

@ -191,6 +191,7 @@ export class GBMinService {
removeRoute(GBServer.globals.server, uiUrl);
GBServer.globals.minInstances = GBServer.globals.minInstances.filter(p => p.instance.botId !== botId);
}
/**
@ -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');
@ -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'];
}
}
@ -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

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();
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) {
@ -235,6 +241,9 @@ export class KBService implements IGBKBService {
return undefined;
}
public async addAnswer(obj: GuaribasAnswer): Promise<GuaribasAnswer> {
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(
filePath: string,
instanceId: number,
@ -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}`);
}

View file

@ -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.
@ -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;
@ -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() {
@ -162,21 +163,7 @@ 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;
@ -184,13 +171,72 @@ export class WhatsappDirectLine extends GBService {
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);
@ -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') {
@ -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);
}
}
@ -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)) {