new(whatsapp.gblib): New provider.

This commit is contained in:
Rodrigo Rodriguez 2022-06-07 17:27:03 -03:00
parent 9e4ebba84e
commit 80ce234bf6
3 changed files with 181 additions and 76 deletions

View file

@ -301,7 +301,6 @@ ENDPOINT_UPDATE=true
try {
if (fs.existsSync('node_modules/ngrok/bin/ngrok.exe') || fs.existsSync('node_modules/ngrok/bin/ngrok')) {
const ngrok = require('ngrok');
return await ngrok.connect({ port: port }, 10);
} else {
GBLog.warn('ngrok executable not found (only tested on Windows). Check installation or node_modules folder.');

View file

@ -325,18 +325,34 @@ export class GBMinService {
private async WhatsAppCallback(req, res) {
try {
if (req.body && req.body.webhook) {
res.status(200);
res.end();
return;
}
let chatapi = false;
// Detects if the message is echo from itself.
const id = req.body.messages[0].author.split('@')[0];
const senderName = req.body.messages[0].senderName;
const id = chatapi ? req.body.messages[0].author.split('@')[0] : req.body.receiver;
const senderName = chatapi ? req.body.messages[0].senderName : req.body.user.name;
const sec = new SecService();
let user = await sec.getUserFromSystemId(id);
if (chatapi) {
if (req.body.messages[0].fromMe) {
res.end();
return; // Exit here.
}
} else {
if (req.body.message.fromMe) {
res.end();
return; // Exit here.
}
}
let activeMin;
let botId = req.params.botId;
@ -349,9 +365,10 @@ export class GBMinService {
// Processes group behaviour.
let text = req.body.messages[0].body;
let text = chatapi ? req.body.messages[0].body : req.body.message.text;
text = text.replace(/\@\d+ /gi, '');
if (chatapi) {
// Ensures that the bot group is the active bot for the user (like switching).
const message = req.body.messages[0];
@ -367,6 +384,7 @@ export class GBMinService {
await (activeMin as any).whatsAppDirectLine.received(req, res);
return; // EXIT HERE.
}
}
// Detects if the welcome message is enabled.
@ -400,7 +418,12 @@ export class GBMinService {
if (startDialog) {
GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`);
if (chatapi) {
req.body.messages[0].body = `/start`;
}
else {
req.body.message = `/start`;
}
// Resets HEAR ON DIALOG value to none and passes
// current dialog to the direct line.
@ -431,7 +454,14 @@ export class GBMinService {
if (startDialog) {
GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`);
if (chatapi) {
req.body.messages[0].body = `/start`;
}
else {
req.body.message = `/start`;
}
await (activeMin as any).whatsAppDirectLine.received(req, res);
} else {
await (activeMin as any).whatsAppDirectLine.sendToDevice(

View file

@ -66,6 +66,8 @@ export class WhatsappDirectLine extends GBService {
public min: GBMinInstance;
private directLineSecret: string;
private locale: string = 'pt-BR';
chatapi: any;
INSTANCE_URL = 'https://api.maytapi.com/api';
constructor(
min: GBMinInstance,
@ -105,8 +107,10 @@ export class WhatsappDirectLine extends GBService {
'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
);
let options;
const options = {
if (this.chatapi) {
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
timeout: 10000,
@ -120,6 +124,26 @@ export class WhatsappDirectLine extends GBService {
}
};
}
else {
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
let url = `${this.INSTANCE_URL}/${productId}/setWebhook`;
let webhook = `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`;
options = {
url: url,
method: 'POST',
body: { webhook: webhook, },
headers: {
'x-maytapi-key': this.whatsappServiceKey,
'Content-Type': 'application/json',
},
json: true,
};
}
if (setUrl) {
const express = require('express');
GBServer.globals.server.use(`/audios`, express.static('work'));
@ -127,7 +151,6 @@ export class WhatsappDirectLine extends GBService {
if (process.env.ENDPOINT_UPDATE === 'true') {
try {
const res = await request.post(options);
GBLog.info(res);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
@ -158,13 +181,13 @@ export class WhatsappDirectLine extends GBService {
}
public async received(req, res) {
if (req.body.messages === undefined) {
if (this.chatapi && req.body.messages === undefined) {
res.end();
return; // Exit here.
}
const message = req.body.messages[0];
const message = this.chatapi ? req.body.messages[0] : req.body.message;
let group = "";
const to = req.body.to;
let answerText = null;
@ -487,7 +510,11 @@ export class WhatsappDirectLine extends GBService {
}
public async sendFileToDevice(to, url, filename, caption, chatId) {
const options = {
let options;
if (this.chatapi) {
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'sendFile'),
qs: {
@ -503,6 +530,33 @@ export class WhatsappDirectLine extends GBService {
}
};
}
else {
// TODO: Attach.
let contents = 0;
let body = {
type: 'image',
text: 'Base64 Image Response',
message: `data:image/jpeg;base64,${contents}`,
};
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
options = {
method: 'post',
json: true,
body,
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': this.whatsappServiceKey,
},
};
}
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
@ -557,9 +611,10 @@ export class WhatsappDirectLine extends GBService {
return await this.sendTextAsAudioToDevice(to, msg, chatId);
} else {
let options;
if (this.chatapi) {
const options = {
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'message'),
qs: {
@ -572,6 +627,27 @@ export class WhatsappDirectLine extends GBService {
'cache-control': 'no-cache'
}
};
}
else {
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
options = {
method: 'post',
json: true,
body: msg,
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': this.whatsappServiceKey,
},
};
}
try {
// tslint:disable-next-line: await-promise