diff --git a/packages/basic.gblib/services/DialogKeywords.ts b/packages/basic.gblib/services/DialogKeywords.ts index 9e333db2..2ea0b90f 100644 --- a/packages/basic.gblib/services/DialogKeywords.ts +++ b/packages/basic.gblib/services/DialogKeywords.ts @@ -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]; } /** diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 78225fa6..2b4ead23 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -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 diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index f9465fe2..c8b019e9 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -197,18 +197,24 @@ export class KBService implements IGBKBService { } - public async getAnswerByText(instanceId: number, text: string): Promise { + public async getAnswerByText(instanceId: number, text: string, from: string = null): Promise { 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 { return await GuaribasAnswer.create(obj); } @@ -382,6 +391,12 @@ export class KBService implements IGBKBService { } } + public static async getGroupReplies(instanceId: number): Promise { + 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}`); } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 9a45f3bb..4e475b91 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -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(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(this.min.instance, 'WhatsApp Group Name', null); + let botShortcuts = this.min.core.getParam(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(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)) {