new(whatsapp.gblib): New provider.

This commit is contained in:
Rodrigo Rodriguez 2022-06-07 18:37:29 -03:00
parent 80ce234bf6
commit c39618c182
3 changed files with 91 additions and 72 deletions

View file

@ -731,14 +731,16 @@ export class GBVMService extends GBService {
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE') GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
); );
const entities = await min["nerEngine"].findEntities( if (step.context.activity['originalText']) {
step.context.activity['originalText'], const entities = await min["nerEngine"].findEntities(
contentLocale); step.context.activity['originalText'],
contentLocale);
for (let i = 0; i < entities.length; i++) { for (let i = 0; i < entities.length; i++) {
const v = entities[i]; const v = entities[i];
const variableName = `${v.entity}`; const variableName = `${v.entity}`;
sandbox[variableName] = v.option; sandbox[variableName] = v.option;
}
} }
// Injects the .gbdialog generated code into the VM. // Injects the .gbdialog generated code into the VM.

View file

@ -333,9 +333,19 @@ export class GBMinService {
} }
let chatapi = false; let chatapi = false;
if (!chatapi) {
if (req.body.type !== 'message') {
res.status(200);
res.end();
return;
}
}
// Detects if the message is echo from itself. // Detects if the message is echo from itself.
const id = chatapi ? req.body.messages[0].author.split('@')[0] : req.body.receiver; const id = chatapi ? req.body.messages[0].author.split('@')[0] : req.body.user.phone;
const senderName = chatapi ? req.body.messages[0].senderName : req.body.user.name; const senderName = chatapi ? req.body.messages[0].senderName : req.body.user.name;
const sec = new SecService(); const sec = new SecService();
let user = await sec.getUserFromSystemId(id); let user = await sec.getUserFromSystemId(id);

View file

@ -189,94 +189,101 @@ export class WhatsappDirectLine extends GBService {
const message = this.chatapi ? req.body.messages[0] : req.body.message; const message = this.chatapi ? req.body.messages[0] : req.body.message;
let group = ""; let group = "";
const to = req.body.to;
let answerText = null; let answerText = null;
let text = message.body; let text = this.chatapi? message.body : message.text;
text = text.replace(/\@\d+ /gi, ''); text = text.replace(/\@\d+ /gi, '');
const from = message.author.split('@')[0]; const from = this.chatapi ? req.body.messages[0].author.split('@')[0] : req.body.user.phone;
const fromName = message.senderName; const fromName = this.chatapi ? req.body.messages[0].senderName : req.body.user.name;
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`); GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);
if (req.body.messages[0].fromMe) { if (this.chatapi) {
res.end(); if (req.body.messages[0].fromMe) {
res.end();
return; // Exit here. return; // Exit here.
}
} else {
if (req.body.message.fromMe) {
res.end();
return; // Exit here.
}
} }
if (message.chatName.charAt(0) !== '+') { if (this.chatapi) {
group = message.chatName; if (message.chatName.charAt(0) !== '+') {
group = message.chatName;
let botGroupName = this.min.core.getParam<string>(this.min.instance, 'WhatsApp Group Name', null); 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); let botShortcuts = this.min.core.getParam<string>(this.min.instance, 'WhatsApp Group Shortcuts', null);
if (!botShortcuts) { if (!botShortcuts) {
botShortcuts = new Array() botShortcuts = new Array()
} }
else { else {
botShortcuts = botShortcuts.split(' '); botShortcuts = botShortcuts.split(' ');
} }
const parts = text.split(' '); const parts = text.split(' ');
// Bot name must be specified on config. // Bot name must be specified on config.
if (botGroupName === group) { if (botGroupName === group) {
// Shortcut has been mentioned? // Shortcut has been mentioned?
let found = false; let found = false;
parts.forEach(e1 => { parts.forEach(e1 => {
botShortcuts.forEach(e2 => { botShortcuts.forEach(e2 => {
if (e1 === e2 && !found) { if (e1 === e2 && !found) {
found = true; found = true;
text = text.replace(e2, ''); text = text.replace(e2, '');
}
});
// 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. // Verify if it is a group cache answer.
let smsServiceNumber = this.min.core.getParam<string>(this.min.instance, 'whatsappServiceNumber', null); const questions = this.min['groupCache'];
if (smsServiceNumber && !answerText) { if (questions && questions.length > 0) {
smsServiceNumber = smsServiceNumber.replace('+', ''); questions.forEach(q => {
if (!message.body.startsWith('@' + smsServiceNumber)) { if (q.content === e1 && !found) {
return; 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);
}); });
const id = req.body.messages[0].chatId.split('@')[0].replace('true_', '').replace('false_', '');
const senderName = req.body.messages[0].senderName;
const sec = new SecService(); const sec = new SecService();
const user = await sec.ensureUser(this.min.instance.instanceId, id, const user = await sec.ensureUser(this.min.instance.instanceId, from,
senderName, '', 'whatsapp', senderName, null); fromName, '', 'whatsapp', fromName, null);
const locale = user.locale ? user.locale : 'pt'; const locale = user.locale ? user.locale : 'pt';
@ -285,12 +292,11 @@ export class WhatsappDirectLine extends GBService {
return; // Exit here. return; // Exit here.
} }
if (message.type === 'ptt') { if (message.type === 'ptt') {
if (process.env.AUDIO_DISABLED !== 'true') { if (process.env.AUDIO_DISABLED !== 'true') {
const options = { const options = {
url: message.body, url: this.chatapi?message.body : message.text,
method: 'GET', method: 'GET',
encoding: 'binary' encoding: 'binary'
}; };
@ -321,10 +327,10 @@ export class WhatsappDirectLine extends GBService {
// Check if there is someone being handled by this Human Agent. // Check if there is someone being handled by this Human Agent.
const manualUser = await sec.getUserFromAgentSystemId(id); const manualUser = await sec.getUserFromAgentSystemId(from);
if (manualUser === null) { if (manualUser === null) {
await sec.updateHumanAgent(id, this.min.instance.instanceId, null); await sec.updateHumanAgent(from, this.min.instance.instanceId, null);
} else { } else {
const agent = await sec.getUserFromSystemId(user.agentSystemId); const agent = await sec.getUserFromSystemId(user.agentSystemId);
@ -369,15 +375,15 @@ export class WhatsappDirectLine extends GBService {
await this.sendToDeviceEx(user.userSystemId, `Você já está sendo atendido por ${agent.userSystemId}.`, locale, null); await this.sendToDeviceEx(user.userSystemId, `Você já está sendo atendido por ${agent.userSystemId}.`, locale, null);
} else if (text === '/qt' || text === 'Sair' || text === 'Fechar') { } else if (text === '/qt' || text === 'Sair' || text === 'Fechar') {
// TODO: Transfers only in pt-br for now. // TODO: Transfers only in pt-br for now.
await this.endTransfer(id, locale, user, agent, sec); await this.endTransfer(from, locale, user, agent, sec);
} else { } else {
GBLog.info(`USER (${id}) TO AGENT ${agent.userSystemId}: ${text}`); GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`);
if (user.agentSystemId.charAt(2) === ":" || agent.userSystemId.indexOf("@") > -1) { // Agent is from Teams or Google Chat. if (user.agentSystemId.charAt(2) === ":" || agent.userSystemId.indexOf("@") > -1) { // Agent is from Teams or Google Chat.
await this.min.conversationalService['sendOnConversation'](this.min, agent, text); await this.min.conversationalService['sendOnConversation'](this.min, agent, text);
} }
else { else {
await this.sendToDeviceEx(user.agentSystemId, `Bot: ${this.min.instance.botId}\n${id}: ${text}`, locale, null); await this.sendToDeviceEx(user.agentSystemId, `Bot: ${this.min.instance.botId}\n${from}: ${text}`, locale, null);
} }
} }
@ -638,9 +644,10 @@ export class WhatsappDirectLine extends GBService {
options = { options = {
url: url,
method: 'post', method: 'post',
json: true, json: true,
body: msg, body: { type: 'text', message: msg, to_number: to },
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'x-maytapi-key': this.whatsappServiceKey, 'x-maytapi-key': this.whatsappServiceKey,