botserver/packages/whatsapp.gblib/services/WhatsappDirectLine.ts

842 lines
27 KiB
TypeScript
Raw Normal View History

2019-06-22 07:06:21 -03:00
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
2019-06-22 07:06:21 -03:00
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
| Licensed under the AGPL-3.0. |
| |
| According to our dual licensing model, this program can be used either |
| under the terms of the GNU Affero General Public License, version 3, |
| or under a proprietary license. |
| |
| The texts of the GNU Affero General Public License with an additional |
| permission and of our proprietary license can be found at and |
| in the LICENSE file you have received along with this program. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU Affero General Public License for more details. |
| |
| "General Bots" is a registered trademark of Pragmatismo.io. |
| The licensing of the program under the AGPLv3 does not imply a |
| trademark license. Therefore any rights, title and interest in |
| our trademarks remain entirely with us. |
| |
\*****************************************************************************/
2022-01-03 13:11:21 -03:00
const urlJoin = require('url-join');
const Swagger = require('swagger-client');
const fs = require('fs');
const Path = require('path');
2020-12-31 15:36:19 -03:00
import { GBLog, GBMinInstance, GBService, IGBPackage } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework';
import * as request from 'request-promise-native';
2019-05-15 12:41:04 -03:00
import { GBServer } from '../../../src/app';
import { GBConversationalService } from '../../core.gbapp/services/GBConversationalService';
import { SecService } from '../../security.gbapp/services/SecService';
import { Messages } from '../strings';
import { GuaribasUser } from '../../security.gbapp/models';
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService';
const { MessageMedia, Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
/**
* Support for Whatsapp.
*/
export class WhatsappDirectLine extends GBService {
2020-12-31 15:36:19 -03:00
public static conversationIds = {};
public static mobiles = {};
2022-06-09 13:31:24 -03:00
public static phones = {};
2021-12-18 21:31:49 -03:00
public static chatIds = {};
public static usernames = {};
2022-05-03 17:05:57 -03:00
public static state = {}; // 2: Waiting, 3: MessageArrived.
public static lastMessage = {}; // 2: Waiting, 3: MessageArrived.
2021-12-18 21:31:49 -03:00
public pollInterval = 3000;
public directLineClientName = 'DirectLineClient';
public directLineClient: any;
public whatsappServiceKey: string;
public whatsappServiceNumber: string;
public whatsappServiceUrl: string;
public botId: string;
2020-12-31 15:36:19 -03:00
public min: GBMinInstance;
2020-04-08 21:33:27 -03:00
private directLineSecret: string;
private locale: string = 'pt-BR';
provider: any;
2022-06-07 17:27:03 -03:00
INSTANCE_URL = 'https://api.maytapi.com/api';
private customClient;
constructor(
min: GBMinInstance,
botId,
directLineSecret,
whatsappServiceKey,
whatsappServiceNumber,
whatsappServiceUrl
) {
super();
this.min = min;
this.botId = botId;
2020-04-08 21:33:27 -03:00
this.directLineSecret = directLineSecret;
this.whatsappServiceKey = whatsappServiceKey;
this.whatsappServiceNumber = whatsappServiceNumber;
this.whatsappServiceUrl = whatsappServiceUrl;
2022-07-15 09:05:16 -03:00
this.provider = whatsappServiceKey === "internal" ?
'GeneralBots' : whatsappServiceNumber.indexOf(';') > -1 ? 'maytapi' : 'chatapi';
2020-04-08 21:33:27 -03:00
}
2020-12-31 15:36:19 -03:00
public static async asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
public async setup(setUrl) {
2021-12-18 21:31:49 -03:00
2020-04-08 21:33:27 -03:00
this.directLineClient =
new Swagger({
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), usePromise: true
});
2020-12-31 15:36:19 -03:00
const client = await this.directLineClient;
2020-04-08 21:33:27 -03:00
client.clientAuthorizations.add(
'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
);
2022-06-07 17:27:03 -03:00
let options;
2020-04-08 21:33:27 -03:00
switch (this.provider) {
case 'GeneralBots':
2022-06-07 17:27:03 -03:00
// Initialize the browser using a local profile for each bot.
const gbaiName = `${this.min.botId}.gbai`;
let localName = Path.join('work', gbaiName, 'profile');
let client = this.customClient = new Client({
authStrategy: new LocalAuth({
clientId: this.min.botId,
dataPath: localName
}),
puppeteer: {
headless: false, args: ['--disable-features=site-per-process',
`--user-data-dir=${localName}`]
}
});
client.initialize();
2020-04-08 21:33:27 -03:00
// Dispatches messages to the received method.
client.on('message', (async message => {
await this.received(message, null);
}).bind(this));
2022-07-15 09:05:16 -03:00
client.on('qr', (async (qr) => {
const adminNumber = this.min.core.getParam(this.min.instance, 'Bot Admin Number', null);
const adminEmail = this.min.core.getParam(this.min.instance, 'Bot Admin E-mail', null);
// Sends QR Code to boot bot admin.
2022-07-15 09:05:16 -03:00
const msg = `Please, scan QR Code with for bot ${this.botId}.`;
GBLog.info(msg);
qrcode.generate(qr, { small: true, scale: 0.5 });
// While handling other bots uses boot instance of this class to send QR Codes.
2022-07-15 09:05:16 -03:00
if (this.botId !== GBServer.globals.minBoot.botId) {
const s = new DialogKeywords(null, null, null, null);
2022-07-15 09:05:16 -03:00
const qrBuf = await s.getQRCode(qr);
const gbaiName = `${this.min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`);
fs.writeFileSync(localName, qrBuf);
const url = urlJoin(
GBServer.globals.publicAddress,
this.min.botId,
'cache',
Path.basename(localName)
);
GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg);
s.sendEmail(adminEmail, `Check your WhatsApp for bot ${this.botId}`, msg);
}
}).bind(this));
client.on('authenticated', () => {
GBLog.info(`WhatsApp QR Code authenticated for ${this.botId}.`);
});
setUrl = false;
break;
case 'chatapi':
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
timeout: 10000,
qs: {
token: this.whatsappServiceKey,
webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`,
set: true
},
headers: {
'cache-control': 'no-cache'
}
};
break;
case 'maytapi':
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
WhatsappDirectLine.phones[phoneId] = this.botId;
options = {
url: url,
method: 'POST',
body: {
webhook: `${GBServer.globals.publicAddress}/webhooks/whatsapp/${this.botId}`,
"ack_delivery": false
},
headers: {
'x-maytapi-key': this.whatsappServiceKey,
'Content-Type': 'application/json',
},
json: true,
};
break;
2022-06-07 17:27:03 -03:00
}
if (setUrl) {
2020-04-28 20:54:04 -03:00
const express = require('express');
GBServer.globals.server.use(`/audios`, express.static('work'));
try {
const res = await request.post(options);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
2020-04-08 21:33:27 -03:00
}
}
2022-06-09 14:19:09 -03:00
public async resetConversationId(botId, number, group = '') {
WhatsappDirectLine.conversationIds[botId + number + group] = undefined;
}
public async check() {
GBLog.info(`GBWhatsapp: Checking server...`);
const options = {
2020-05-16 16:40:44 -03:00
url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}`,
method: 'GET'
};
const res = await request(options);
const json = JSON.parse(res);
2020-12-31 15:36:19 -03:00
return json.accountStatus === 'authenticated';
}
public static providerFromRequest(req) {
return req.body.messages ? 'chatapi' :
req.body.message ? 'maytapi' : 'GeneralBots';
}
public async received(req, res) {
2020-12-31 15:36:19 -03:00
const provider = WhatsappDirectLine.providerFromRequest(req);
let message, from, fromName, text;
2021-12-18 21:31:49 -03:00
let group = "";
let answerText = null;
let attachments = null;
2021-12-18 21:31:49 -03:00
switch (provider) {
case 'GeneralBots':
message = req;
2022-07-15 09:05:16 -03:00
text = message.body;
from = message.from.split('@')[0];
fromName = message._data.notifyName;
if (message.hasMedia) {
const base64Image = await message.downloadMedia();
attachments = [];
attachments.push(
{
name: 'uploaded.png',
contentType: base64Image.mimetype,
contentUrl: `data:${base64Image.mimetype};base64,${base64Image.data}`
});
}
break;
2021-12-18 21:31:49 -03:00
case 'chatapi':
message = req.body.messages[0];
text = message.body;
from = req.body.messages[0].author.split('@')[0];
fromName = req.body.messages[0].senderName;
2021-12-18 21:31:49 -03:00
if (message.type !== 'chat') {
attachments = [];
attachments.push(
{
name: 'uploaded',
contentType: 'application/octet-stream',
contentUrl: message.body
});
}
if (req.body.messages[0].fromMe) {
res.end();
return; // Exit here.
}
2020-12-31 15:36:19 -03:00
break;
case 'maytapi':
message = req.body.message;
text = message.text;
from = req.body.user.phone;
fromName = req.body.user.name;
if (req.body.message.fromMe) {
res.end();
return; // Exit here.
}
break;
2022-06-07 18:37:29 -03:00
}
text = text.replace(/\@\d+ /gi, '');
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);
if (provider === "chatapi") {
2022-06-07 18:37:29 -03:00
if (message.chatName.charAt(0) !== '+') {
group = message.chatName;
2022-06-07 18:37:29 -03:00
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);
if (!botShortcuts) {
botShortcuts = new Array()
}
else {
botShortcuts = botShortcuts.split(' ');
}
2022-06-07 18:37:29 -03:00
const parts = text.split(' ');
2022-06-07 18:37:29 -03:00
// Bot name must be specified on config.
2022-06-07 18:37:29 -03:00
if (botGroupName === group) {
2022-06-07 18:37:29 -03:00
// Shortcut has been mentioned?
2022-06-07 18:37:29 -03:00
let found = false;
parts.forEach(e1 => {
botShortcuts.forEach(e2 => {
if (e1 === e2 && !found) {
found = true;
text = text.replace(e2, '');
}
});
2022-06-07 18:37:29 -03:00
// Verify if it is a group cache answer.
2022-06-07 18:37:29 -03:00
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;
}
});
}
2022-06-07 18:37:29 -03:00
// 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;
}
}
});
}
}
}
const botId = this.min.instance.botId;
const state = WhatsappDirectLine.state[botId + from];
if (state) {
WhatsappDirectLine.state[botId + from] = null;
await state.promise(null, message.text);
return; // Exit here.
};
// Processes .gbapp message interception.
2022-06-07 17:27:03 -03:00
await CollectionUtil.asyncForEach(this.min.appPackages, async (e: IGBPackage) => {
await e.onExchangeData(this.min, 'whatsappMessage', { from, fromName });
});
const sec = new SecService();
2022-06-07 18:37:29 -03:00
const user = await sec.ensureUser(this.min.instance.instanceId, from,
fromName, '', 'whatsapp', fromName, null);
const locale = user.locale ? user.locale : 'pt';
if (answerText) {
await this.sendToDeviceEx(user.userSystemId, answerText, locale, null);
return; // Exit here.
}
2020-12-31 15:36:19 -03:00
if (message.type === 'ptt') {
2020-12-31 15:36:19 -03:00
if (process.env.AUDIO_DISABLED !== 'true') {
const options = {
url: provider ? message.body : message.text,
method: 'GET',
encoding: 'binary'
};
const res = await request(options);
2020-12-31 15:36:19 -03:00
const buf = Buffer.from(res, 'binary');
text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
buf, locale
);
2020-12-31 15:36:19 -03:00
} else {
await this.sendToDevice(user.userSystemId,
2021-12-18 21:31:49 -03:00
`No momento estou apenas conseguindo ler mensagens de texto.`, null);
}
}
const conversationId = WhatsappDirectLine.conversationIds[botId + from + group];
2020-12-31 15:36:19 -03:00
const client = await this.directLineClient;
2022-06-09 14:19:09 -03:00
WhatsappDirectLine.lastMessage[botId + from] = message;
2022-05-03 17:05:57 -03:00
// Check if this message is from a Human Agent itself.
2020-12-31 15:36:19 -03:00
if (user.agentMode === 'self') {
// Check if there is someone being handled by this Human Agent.
2022-06-07 18:37:29 -03:00
const manualUser = await sec.getUserFromAgentSystemId(from);
2020-05-16 16:40:44 -03:00
if (manualUser === null) {
2022-06-07 18:37:29 -03:00
await sec.updateHumanAgent(from, this.min.instance.instanceId, null);
2021-12-18 21:31:49 -03:00
2020-12-31 15:36:19 -03:00
} else {
const agent = await sec.getUserFromSystemId(user.agentSystemId);
2020-05-16 16:40:44 -03:00
const cmd = '/reply ';
if (text.startsWith(cmd)) {
2020-12-31 15:36:19 -03:00
const filename = text.substr(cmd.length);
const message = await this.min.kbService.getAnswerTextByMediaName(this.min.instance.instanceId, filename);
2020-05-16 16:40:44 -03:00
if (message === null) {
await this.sendToDeviceEx(user.userSystemId, `File ${filename} not found in any .gbkb published. Check the name or publish again the associated .gbkb.`,
2021-12-18 21:31:49 -03:00
locale, null);
2020-05-16 16:40:44 -03:00
} else {
await this.min.conversationalService.sendMarkdownToMobile(this.min, null, user.userSystemId, message);
}
} else if (text === '/qt') {
// TODO: Transfers only in pt-br for now.
2020-12-31 15:36:19 -03:00
await this.sendToDeviceEx(manualUser.userSystemId,
2021-12-18 21:31:49 -03:00
Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
2020-05-16 16:40:44 -03:00
if (user.agentSystemId.charAt(2) === ":") { // Agent is from Teams.
await this.min.conversationalService['sendOnConversation'](this.min, agent, Messages[this.locale].notify_end_transfer(this.min.instance.botId));
}
else {
await this.sendToDeviceEx(user.agentSystemId,
2021-12-18 21:31:49 -03:00
Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
}
await sec.updateHumanAgent(manualUser.userSystemId, this.min.instance.instanceId, null);
await sec.updateHumanAgent(user.agentSystemId, this.min.instance.instanceId, null);
2020-12-31 15:36:19 -03:00
} else {
GBLog.info(`HUMAN AGENT (${manualUser.agentSystemId}) TO USER ${manualUser.userSystemId}: ${text}`);
2021-12-18 21:31:49 -03:00
await this.sendToDeviceEx(manualUser.userSystemId, `AGENTE: *${text}*`, locale, null);
2020-05-16 16:40:44 -03:00
}
}
2020-12-31 15:36:19 -03:00
} else if (user.agentMode === 'human') {
2020-12-31 15:36:19 -03:00
const agent = await sec.getUserFromSystemId(user.agentSystemId);
if (text === '/t') {
2021-12-18 21:31:49 -03:00
await this.sendToDeviceEx(user.userSystemId, `Você já está sendo atendido por ${agent.userSystemId}.`, locale, null);
2020-12-31 15:36:19 -03:00
} else if (text === '/qt' || text === 'Sair' || text === 'Fechar') {
// TODO: Transfers only in pt-br for now.
2022-06-07 18:37:29 -03:00
await this.endTransfer(from, locale, user, agent, sec);
2020-12-31 15:36:19 -03:00
} else {
2022-06-07 18:37:29 -03:00
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.
await this.min.conversationalService['sendOnConversation'](this.min, agent, text);
}
else {
2022-06-07 18:37:29 -03:00
await this.sendToDeviceEx(user.agentSystemId, `Bot: ${this.min.instance.botId}\n${from}: ${text}`, locale, null);
}
}
2020-12-31 15:36:19 -03:00
} else if (user.agentMode === 'bot' || user.agentMode === null || user.agentMode === undefined) {
2022-06-09 14:19:09 -03:00
if (WhatsappDirectLine.conversationIds[botId + from + group] === undefined) {
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
const response = await client.Conversations.Conversations_StartConversation();
const generatedConversationId = response.obj.conversationId;
2022-06-09 14:19:09 -03:00
WhatsappDirectLine.conversationIds[botId + from + group] = generatedConversationId;
if (provider === "GeneralBots") {
2022-07-15 09:05:16 -03:00
WhatsappDirectLine.chatIds[generatedConversationId] = message.from;
}
WhatsappDirectLine.mobiles[generatedConversationId] = from;
WhatsappDirectLine.usernames[from] = fromName;
2021-12-18 21:31:49 -03:00
WhatsappDirectLine.chatIds[generatedConversationId] = message.chatId;
2022-06-07 17:27:03 -03:00
this.pollMessages(client, generatedConversationId, from, fromName);
this.inputMessage(client, generatedConversationId, text, from, fromName, group, attachments);
} else {
this.inputMessage(client, conversationId, text, from, fromName, group, attachments);
}
} else {
GBLog.warn(`Inconsistencty found: Invalid agentMode on User Table: ${user.agentMode}`);
}
if (res) {
res.end();
}
}
private async endTransfer(id: any, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) {
await this.sendToDeviceEx(id,
Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
if (user.agentSystemId.charAt(2) === ":") { // Agent is from Teams.
await this.min.conversationalService['sendOnConversation'](this.min, agent, Messages[this.locale].notify_end_transfer(this.min.instance.botId));
}
else {
await this.sendToDeviceEx(user.agentSystemId, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
}
await sec.updateHumanAgent(id, this.min.instance.instanceId, null);
}
public inputMessage(client, conversationId, text, from, fromName, group, attachments) {
return client.Conversations.Conversations_PostActivity({
conversationId: conversationId,
activity: {
textFormat: 'plain',
text: text,
type: 'message',
mobile: from,
group: group,
attachments: attachments,
from: {
id: from,
name: fromName
},
replyToId: from
}
});
}
public pollMessages(client, conversationId, from, fromName) {
2019-06-23 07:03:32 -03:00
GBLog.info(`GBWhatsapp: Starting message polling(${from}, ${conversationId}).`);
2019-06-23 07:24:35 -03:00
let watermark: any;
2019-06-23 07:03:32 -03:00
const worker = async () => {
try {
2019-06-23 07:24:35 -03:00
const response = await client.Conversations.Conversations_GetActivities({
2019-06-23 07:03:32 -03:00
conversationId: conversationId,
2019-06-23 07:24:35 -03:00
watermark: watermark
});
2019-06-23 07:24:35 -03:00
watermark = response.obj.watermark;
2019-06-23 07:03:32 -03:00
await this.printMessages(response.obj.activities, conversationId, from, fromName);
} catch (err) {
2020-04-28 20:54:04 -03:00
GBLog.error(`Error calling printMessages on Whatsapp channel ${err.data === undefined ? err : err.data}`);
2019-06-23 07:03:32 -03:00
}
2019-06-22 07:06:21 -03:00
};
2019-06-23 07:25:59 -03:00
setInterval(worker, this.pollInterval);
}
2018-10-16 10:19:34 -03:00
2019-06-22 07:51:04 -03:00
public async printMessages(activities, conversationId, from, fromName) {
if (activities && activities.length) {
// Ignore own messages.
activities = activities.filter(m => m.from.id === this.botId && m.type === 'message');
if (activities.length) {
// Print other messages.
2019-06-22 07:51:04 -03:00
await WhatsappDirectLine.asyncForEach(activities, async activity => {
await this.printMessage(activity, conversationId, from, fromName);
});
}
}
}
2019-06-22 07:51:04 -03:00
public async printMessage(activity, conversationId, from, fromName) {
let output = '';
if (activity.text) {
2019-06-22 07:51:04 -03:00
GBLog.info(`GBWhatsapp: SND ${from}(${fromName}): ${activity.text}`);
output = activity.text;
}
if (activity.attachments) {
activity.attachments.forEach(attachment => {
switch (attachment.contentType) {
case 'application/vnd.microsoft.card.hero':
output += `\n${this.renderHeroCard(attachment)}`;
break;
case 'image/png':
GBLog.info(`Opening the requested image ${attachment.contentUrl}`);
output += `\n${attachment.contentUrl}`;
break;
default:
GBLog.info(`Unknown content type: ${attachment.contentType}`);
}
});
}
2021-12-18 21:31:49 -03:00
await this.sendToDevice(from, output, conversationId);
}
public renderHeroCard(attachment) {
return `${attachment.content.title} - ${attachment.content.text}`;
}
public async sendFileToDevice(to, url, filename, caption, chatId) {
2022-06-07 17:27:03 -03:00
let options;
switch (this.provider) {
case 'GeneralBots':
const attachment = MessageMedia.fromUrl(url);
await this.customClient.sendMessage(to += '@c.us', attachment, { caption: caption });
break;
2022-06-07 17:27:03 -03:00
case 'chatapi':
2022-06-10 14:43:13 -03:00
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'sendFile'),
qs: {
token: this.whatsappServiceKey
},
json: true,
body: {
phone: to,
body: url,
filename: filename,
caption: caption
},
headers: {
'cache-control': 'no-cache'
}
};
2022-06-07 17:27:03 -03:00
break;
case 'maytapi':
let contents = 0;
let body = {
to_number: to,
type: "media",
message: url,
text: caption
};
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
options = {
url: `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`,
method: 'post',
json: true,
body,
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': this.whatsappServiceKey,
},
};
break;
}
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
GBLog.info(`File ${url} sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending file to Whatsapp provider ${error.message}`);
}
}
public async sendAudioToDevice(to, url, chatId) {
let options;
switch (this.provider) {
case 'GeneralBots':
const attachment = MessageMedia.fromUrl(url);
await this.customClient.sendMessage(to, attachment);
break;
case 'chatapi':
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'sendPTT'),
qs: {
token: this.whatsappServiceKey,
phone: chatId ? null : to,
chatId: chatId,
body: url
},
headers: {
'cache-control': 'no-cache'
}
};
break;
case 'maytapi':
options = {}; // TODO: Code Maytapi.
break;
}
try {
// tslint:disable-next-line: await-promise
const result = await request.post(options);
GBLog.info(`Audio ${url} sent to ${to}: ${result}`);
} catch (error) {
GBLog.error(`Error sending audio message to Whatsapp provider ${error.message}`);
}
}
public async sendTextAsAudioToDevice(to, msg, chatId) {
2020-04-28 20:54:04 -03:00
2022-06-07 17:27:03 -03:00
const url = await GBConversationalService.getAudioBufferFromText(
msg
);
await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
2020-04-28 20:54:04 -03:00
}
public async sendToDevice(to: string, msg: string, conversationId) {
2020-04-28 20:54:04 -03:00
const cmd = '/audio ';
let chatId = WhatsappDirectLine.chatIds[conversationId];
if (msg.startsWith(cmd)) {
2020-04-28 20:54:04 -03:00
msg = msg.substr(cmd.length);
return await this.sendTextAsAudioToDevice(to, msg, chatId);
} else {
2020-04-28 20:54:04 -03:00
2022-06-07 17:27:03 -03:00
let options;
switch (this.provider) {
case 'GeneralBots':
2022-06-07 17:27:03 -03:00
this.customClient.sendMessage(to + '@c.us', msg);
2022-06-07 17:27:03 -03:00
break;
2022-06-07 17:27:03 -03:00
case 'chatapi':
2022-06-07 17:27:03 -03:00
options = {
method: 'POST',
url: urlJoin(this.whatsappServiceUrl, 'message'),
qs: {
token: this.whatsappServiceKey,
phone: chatId ? null : to,
chatId: chatId,
body: msg
},
headers: {
'cache-control': 'no-cache'
}
};
break;
case 'maytapi':
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1]
let url = `${this.INSTANCE_URL}/${productId}/${phoneId}/sendMessage`;
options = {
url: url,
method: 'post',
json: true,
body: { type: 'text', message: msg, to_number: to },
headers: {
'Content-Type': 'application/json',
'x-maytapi-key': this.whatsappServiceKey,
},
};
break;
2022-06-07 17:27:03 -03:00
}
2021-12-18 21:31:49 -03:00
2020-04-28 20:54:04 -03:00
try {
GBLog.info(`Message [${msg}] is being sent to ${to}...`);
2020-04-28 20:54:04 -03:00
// tslint:disable-next-line: await-promise
if (this.provider !== "GeneralBots") {
await request.post(options);
}
2020-04-28 20:54:04 -03:00
} catch (error) {
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);
// TODO: Handle Error: socket hang up and retry.
}
}
}
2020-12-31 15:36:19 -03:00
2021-12-18 21:31:49 -03:00
public async sendToDeviceEx(to, text, locale, conversationId) {
text = await this.min.conversationalService.translate(
this.min,
2020-12-31 15:36:19 -03:00
text,
locale
);
2021-12-18 21:31:49 -03:00
await this.sendToDevice(to, text, conversationId);
2020-12-31 15:36:19 -03:00
}
2018-10-16 10:19:34 -03:00
}