new(whatsapp.gblib): New provider.
This commit is contained in:
parent
80ce234bf6
commit
c39618c182
3 changed files with 91 additions and 72 deletions
|
@ -731,6 +731,7 @@ export class GBVMService extends GBService {
|
||||||
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
GBConfigService.get('DEFAULT_CONTENT_LANGUAGE')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (step.context.activity['originalText']) {
|
||||||
const entities = await min["nerEngine"].findEntities(
|
const entities = await min["nerEngine"].findEntities(
|
||||||
step.context.activity['originalText'],
|
step.context.activity['originalText'],
|
||||||
contentLocale);
|
contentLocale);
|
||||||
|
@ -740,6 +741,7 @@ export class GBVMService extends GBService {
|
||||||
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.
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -189,24 +189,34 @@ 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 (this.chatapi) {
|
||||||
if (req.body.messages[0].fromMe) {
|
if (req.body.messages[0].fromMe) {
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
return; // Exit here.
|
return; // Exit here.
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (req.body.message.fromMe) {
|
||||||
|
res.end();
|
||||||
|
|
||||||
|
return; // Exit here.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.chatapi) {
|
||||||
if (message.chatName.charAt(0) !== '+') {
|
if (message.chatName.charAt(0) !== '+') {
|
||||||
group = message.chatName;
|
group = message.chatName;
|
||||||
|
|
||||||
|
@ -264,19 +274,16 @@ export class WhatsappDirectLine extends GBService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
|
|
Loading…
Add table
Reference in a new issue