new(whatsapp.gblib): New provider.
This commit is contained in:
parent
9e4ebba84e
commit
80ce234bf6
3 changed files with 181 additions and 76 deletions
|
@ -301,7 +301,6 @@ ENDPOINT_UPDATE=true
|
||||||
try {
|
try {
|
||||||
if (fs.existsSync('node_modules/ngrok/bin/ngrok.exe') || fs.existsSync('node_modules/ngrok/bin/ngrok')) {
|
if (fs.existsSync('node_modules/ngrok/bin/ngrok.exe') || fs.existsSync('node_modules/ngrok/bin/ngrok')) {
|
||||||
const ngrok = require('ngrok');
|
const ngrok = require('ngrok');
|
||||||
|
|
||||||
return await ngrok.connect({ port: port }, 10);
|
return await ngrok.connect({ port: port }, 10);
|
||||||
} else {
|
} else {
|
||||||
GBLog.warn('ngrok executable not found (only tested on Windows). Check installation or node_modules folder.');
|
GBLog.warn('ngrok executable not found (only tested on Windows). Check installation or node_modules folder.');
|
||||||
|
|
|
@ -325,18 +325,34 @@ export class GBMinService {
|
||||||
private async WhatsAppCallback(req, res) {
|
private async WhatsAppCallback(req, res) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
if (req.body && req.body.webhook) {
|
||||||
|
res.status(200);
|
||||||
|
res.end();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let chatapi = false;
|
||||||
// Detects if the message is echo from itself.
|
// Detects if the message is echo from itself.
|
||||||
|
|
||||||
const id = req.body.messages[0].author.split('@')[0];
|
const id = chatapi ? req.body.messages[0].author.split('@')[0] : req.body.receiver;
|
||||||
const senderName = req.body.messages[0].senderName;
|
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);
|
||||||
|
|
||||||
|
if (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.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let activeMin;
|
let activeMin;
|
||||||
let botId = req.params.botId;
|
let botId = req.params.botId;
|
||||||
|
@ -349,9 +365,10 @@ export class GBMinService {
|
||||||
|
|
||||||
// Processes group behaviour.
|
// 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, '');
|
text = text.replace(/\@\d+ /gi, '');
|
||||||
|
|
||||||
|
if (chatapi) {
|
||||||
// Ensures that the bot group is the active bot for the user (like switching).
|
// Ensures that the bot group is the active bot for the user (like switching).
|
||||||
|
|
||||||
const message = req.body.messages[0];
|
const message = req.body.messages[0];
|
||||||
|
@ -367,6 +384,7 @@ export class GBMinService {
|
||||||
await (activeMin as any).whatsAppDirectLine.received(req, res);
|
await (activeMin as any).whatsAppDirectLine.received(req, res);
|
||||||
return; // EXIT HERE.
|
return; // EXIT HERE.
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Detects if the welcome message is enabled.
|
// Detects if the welcome message is enabled.
|
||||||
|
@ -400,7 +418,12 @@ export class GBMinService {
|
||||||
|
|
||||||
if (startDialog) {
|
if (startDialog) {
|
||||||
GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`);
|
GBLog.info(`Calling /start to Auto start ${startDialog} for ${activeMin.instance.instanceId}...`);
|
||||||
|
if (chatapi) {
|
||||||
req.body.messages[0].body = `/start`;
|
req.body.messages[0].body = `/start`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
req.body.message = `/start`;
|
||||||
|
}
|
||||||
|
|
||||||
// Resets HEAR ON DIALOG value to none and passes
|
// Resets HEAR ON DIALOG value to none and passes
|
||||||
// current dialog to the direct line.
|
// current dialog to the direct line.
|
||||||
|
@ -431,7 +454,14 @@ export class GBMinService {
|
||||||
|
|
||||||
if (startDialog) {
|
if (startDialog) {
|
||||||
GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`);
|
GBLog.info(`Calling /start for Auto start : ${startDialog} for ${activeMin.instance.botId}...`);
|
||||||
|
if (chatapi) {
|
||||||
req.body.messages[0].body = `/start`;
|
req.body.messages[0].body = `/start`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
req.body.message = `/start`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
await (activeMin as any).whatsAppDirectLine.received(req, res);
|
await (activeMin as any).whatsAppDirectLine.received(req, res);
|
||||||
} else {
|
} else {
|
||||||
await (activeMin as any).whatsAppDirectLine.sendToDevice(
|
await (activeMin as any).whatsAppDirectLine.sendToDevice(
|
||||||
|
|
|
@ -66,6 +66,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
public min: GBMinInstance;
|
public min: GBMinInstance;
|
||||||
private directLineSecret: string;
|
private directLineSecret: string;
|
||||||
private locale: string = 'pt-BR';
|
private locale: string = 'pt-BR';
|
||||||
|
chatapi: any;
|
||||||
|
INSTANCE_URL = 'https://api.maytapi.com/api';
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
min: GBMinInstance,
|
min: GBMinInstance,
|
||||||
|
@ -105,8 +107,10 @@ export class WhatsappDirectLine extends GBService {
|
||||||
'AuthorizationBotConnector',
|
'AuthorizationBotConnector',
|
||||||
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
|
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
|
||||||
);
|
);
|
||||||
|
let options;
|
||||||
|
|
||||||
const options = {
|
if (this.chatapi) {
|
||||||
|
options = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
|
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
|
||||||
timeout: 10000,
|
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) {
|
if (setUrl) {
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
GBServer.globals.server.use(`/audios`, express.static('work'));
|
GBServer.globals.server.use(`/audios`, express.static('work'));
|
||||||
|
@ -127,7 +151,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
if (process.env.ENDPOINT_UPDATE === 'true') {
|
if (process.env.ENDPOINT_UPDATE === 'true') {
|
||||||
try {
|
try {
|
||||||
const res = await request.post(options);
|
const res = await request.post(options);
|
||||||
GBLog.info(res);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
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) {
|
public async received(req, res) {
|
||||||
if (req.body.messages === undefined) {
|
if (this.chatapi && req.body.messages === undefined) {
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
return; // Exit here.
|
return; // Exit here.
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = req.body.messages[0];
|
const message = this.chatapi ? req.body.messages[0] : req.body.message;
|
||||||
let group = "";
|
let group = "";
|
||||||
const to = req.body.to;
|
const to = req.body.to;
|
||||||
let answerText = null;
|
let answerText = null;
|
||||||
|
@ -487,7 +510,11 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendFileToDevice(to, url, filename, caption, chatId) {
|
public async sendFileToDevice(to, url, filename, caption, chatId) {
|
||||||
const options = {
|
|
||||||
|
let options;
|
||||||
|
if (this.chatapi) {
|
||||||
|
|
||||||
|
options = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'sendFile'),
|
url: urlJoin(this.whatsappServiceUrl, 'sendFile'),
|
||||||
qs: {
|
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 {
|
try {
|
||||||
// tslint:disable-next-line: await-promise
|
// tslint:disable-next-line: await-promise
|
||||||
const result = await request.post(options);
|
const result = await request.post(options);
|
||||||
|
@ -557,9 +611,10 @@ export class WhatsappDirectLine extends GBService {
|
||||||
return await this.sendTextAsAudioToDevice(to, msg, chatId);
|
return await this.sendTextAsAudioToDevice(to, msg, chatId);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
let options;
|
||||||
|
if (this.chatapi) {
|
||||||
|
|
||||||
|
options = {
|
||||||
const options = {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: urlJoin(this.whatsappServiceUrl, 'message'),
|
url: urlJoin(this.whatsappServiceUrl, 'message'),
|
||||||
qs: {
|
qs: {
|
||||||
|
@ -572,6 +627,27 @@ export class WhatsappDirectLine extends GBService {
|
||||||
'cache-control': 'no-cache'
|
'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 {
|
try {
|
||||||
// tslint:disable-next-line: await-promise
|
// tslint:disable-next-line: await-promise
|
||||||
|
|
Loading…
Add table
Reference in a new issue