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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
import urlJoin = require('url-join');
|
2019-03-08 17:05:58 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const Swagger = require('swagger-client');
|
|
|
|
const rp = require('request-promise');
|
2019-03-09 16:59:31 -03:00
|
|
|
import { GBLog, GBService } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import * as request from 'request-promise-native';
|
2019-05-15 12:41:04 -03:00
|
|
|
import { GBServer } from '../../../src/app';
|
2018-11-12 12:20:44 -02:00
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
/**
|
|
|
|
* Support for Whatsapp.
|
|
|
|
*/
|
2018-05-07 20:45:11 -03:00
|
|
|
export class WhatsappDirectLine extends GBService {
|
2019-06-22 07:06:21 -03:00
|
|
|
public pollInterval = 5000;
|
2019-03-08 06:37:13 -03:00
|
|
|
public directLineClientName = 'DirectLineClient';
|
|
|
|
|
|
|
|
public directLineClient: any;
|
|
|
|
public whatsappServiceKey: string;
|
|
|
|
public whatsappServiceNumber: string;
|
|
|
|
public whatsappServiceUrl: string;
|
|
|
|
public botId: string;
|
|
|
|
|
|
|
|
public conversationIds = {};
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
botId,
|
|
|
|
directLineSecret,
|
|
|
|
whatsappServiceKey,
|
|
|
|
whatsappServiceNumber,
|
2019-06-17 15:22:13 -03:00
|
|
|
whatsappServiceUrl
|
2019-03-08 06:37:13 -03:00
|
|
|
) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.botId = botId;
|
|
|
|
this.whatsappServiceKey = whatsappServiceKey;
|
|
|
|
this.whatsappServiceNumber = whatsappServiceNumber;
|
|
|
|
this.whatsappServiceUrl = whatsappServiceUrl;
|
2019-06-17 15:22:13 -03:00
|
|
|
const fs = require('fs');
|
2019-03-08 06:37:13 -03:00
|
|
|
|
2019-05-15 22:30:14 -03:00
|
|
|
this.directLineClient =
|
|
|
|
new Swagger({
|
|
|
|
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')),
|
|
|
|
usePromise: true
|
|
|
|
});
|
|
|
|
this.directLineClient
|
2019-03-08 06:37:13 -03:00
|
|
|
.then(async client => {
|
2019-05-15 22:30:14 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
client.clientAuthorizations.add(
|
|
|
|
'AuthorizationBotConnector',
|
2019-03-09 16:59:31 -03:00
|
|
|
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${directLineSecret}`, 'header')
|
2019-03-08 06:37:13 -03:00
|
|
|
);
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
2019-03-09 16:59:31 -03:00
|
|
|
url: urlJoin(this.whatsappServiceUrl, 'webhook'),
|
2019-03-08 06:37:13 -03:00
|
|
|
qs: {
|
|
|
|
token: this.whatsappServiceKey,
|
2019-08-22 17:28:11 -03:00
|
|
|
webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp`,
|
2019-03-08 06:37:13 -03:00
|
|
|
set: true
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'cache-control': 'no-cache'
|
|
|
|
}
|
|
|
|
};
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
try {
|
2019-03-09 16:59:31 -03:00
|
|
|
const result = request.post(options);
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(result);
|
2019-03-08 06:37:13 -03:00
|
|
|
} catch (error) {
|
2019-06-17 21:41:41 -03:00
|
|
|
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
2018-05-12 16:08:24 -03:00
|
|
|
}
|
2019-03-08 06:37:13 -03:00
|
|
|
});
|
|
|
|
}
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2019-06-22 07:51:04 -03:00
|
|
|
public static async asyncForEach(array, callback) {
|
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:17:41 -03:00
|
|
|
public async received(req, res) {
|
|
|
|
|
|
|
|
if (req.body.messages === undefined) {
|
|
|
|
res.end();
|
|
|
|
return; // Exit here.
|
|
|
|
}
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
const text = req.body.messages[0].body;
|
|
|
|
const from = req.body.messages[0].author.split('@')[0];
|
|
|
|
const fromName = req.body.messages[0].senderName;
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
if (req.body.messages[0].fromMe) {
|
2019-06-28 11:17:41 -03:00
|
|
|
res.end();
|
2019-03-08 06:37:13 -03:00
|
|
|
return; // Exit here.
|
2018-05-12 13:40:34 -03:00
|
|
|
}
|
|
|
|
|
2019-06-22 07:06:21 -03:00
|
|
|
GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);
|
2019-03-08 06:37:13 -03:00
|
|
|
|
|
|
|
const conversationId = this.conversationIds[from];
|
|
|
|
|
2019-06-28 11:17:41 -03:00
|
|
|
let client = await this.directLineClient;
|
|
|
|
|
|
|
|
if (this.conversationIds[from] === undefined) {
|
|
|
|
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
|
|
|
|
const response = await client.Conversations.Conversations_StartConversation()
|
|
|
|
const generatedConversationId = response.obj.conversationId;
|
|
|
|
|
|
|
|
this.conversationIds[from] = generatedConversationId;
|
|
|
|
|
|
|
|
this.pollMessages(client, generatedConversationId, from, fromName);
|
|
|
|
this.inputMessage(client, generatedConversationId, text, from, fromName);
|
|
|
|
} else {
|
|
|
|
this.inputMessage(client, conversationId, text, from, fromName);
|
|
|
|
}
|
|
|
|
res.end();
|
2019-06-17 15:22:13 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public inputMessage(client, conversationId, text, from, fromName) {
|
2019-06-28 11:17:41 -03:00
|
|
|
return client.Conversations.Conversations_PostActivity({
|
2019-03-08 06:37:13 -03:00
|
|
|
conversationId: conversationId,
|
|
|
|
activity: {
|
|
|
|
textFormat: 'plain',
|
|
|
|
text: text,
|
|
|
|
type: 'message',
|
|
|
|
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-03-08 06:37:13 -03:00
|
|
|
});
|
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) {
|
|
|
|
GBLog.error(`Error calling printMessages on Whatsapp channel ${err.data}`);
|
|
|
|
}
|
2019-06-22 07:06:21 -03:00
|
|
|
};
|
2019-06-23 07:25:59 -03:00
|
|
|
setInterval(worker, this.pollInterval);
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
2018-10-16 10:19:34 -03:00
|
|
|
|
2019-06-22 07:51:04 -03:00
|
|
|
public async printMessages(activities, conversationId, from, fromName) {
|
2019-03-08 06:37:13 -03:00
|
|
|
if (activities && activities.length) {
|
|
|
|
// Ignore own messages.
|
2018-11-12 12:20:44 -02:00
|
|
|
|
2019-05-15 22:30:14 -03:00
|
|
|
activities = activities.filter(m => m.from.id === this.botId && m.type === 'message');
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
if (activities.length) {
|
|
|
|
// Print other messages.
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2019-06-22 07:51:04 -03:00
|
|
|
await WhatsappDirectLine.asyncForEach(activities, async activity => {
|
|
|
|
await this.printMessage(activity, conversationId, from, fromName);
|
2019-03-08 06:37:13 -03:00
|
|
|
});
|
|
|
|
}
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2019-06-22 07:51:04 -03:00
|
|
|
public async printMessage(activity, conversationId, from, fromName) {
|
2019-03-08 06:37:13 -03:00
|
|
|
let output = '';
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
if (activity.text) {
|
2019-06-22 07:51:04 -03:00
|
|
|
GBLog.info(`GBWhatsapp: SND ${from}(${fromName}): ${activity.text}`);
|
2019-03-08 06:37:13 -03:00
|
|
|
output = activity.text;
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
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':
|
2019-03-09 16:59:31 -03:00
|
|
|
GBLog.info(`Opening the requested image ${attachment.contentUrl}`);
|
2019-03-08 06:37:13 -03:00
|
|
|
output += `\n${attachment.contentUrl}`;
|
|
|
|
break;
|
2019-03-09 16:59:31 -03:00
|
|
|
default:
|
|
|
|
GBLog.info(`Unknown content type: ${attachment.contentType}`);
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
|
|
|
});
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2018-05-11 22:18:38 -03:00
|
|
|
|
2019-06-22 07:51:04 -03:00
|
|
|
await this.sendToDevice(from, output);
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public renderHeroCard(attachment) {
|
|
|
|
return `${attachment.content.title} - ${attachment.content.text}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendToDevice(to, msg) {
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
2019-03-09 16:59:31 -03:00
|
|
|
url: urlJoin(this.whatsappServiceUrl, 'message'),
|
2019-03-08 06:37:13 -03:00
|
|
|
qs: {
|
|
|
|
token: this.whatsappServiceKey,
|
|
|
|
phone: to,
|
|
|
|
body: msg
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'cache-control': 'no-cache'
|
|
|
|
}
|
|
|
|
};
|
2019-05-15 22:30:14 -03:00
|
|
|
|
|
|
|
try {
|
2019-06-18 16:03:19 -03:00
|
|
|
// tslint:disable-next-line: await-promise
|
|
|
|
const result = await request.post(options);
|
2019-05-15 22:30:14 -03:00
|
|
|
GBLog.info(result);
|
|
|
|
} catch (error) {
|
2019-06-17 21:41:41 -03:00
|
|
|
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);
|
2019-05-15 22:30:14 -03:00
|
|
|
}
|
2019-03-08 06:37:13 -03:00
|
|
|
}
|
2018-10-16 10:19:34 -03:00
|
|
|
}
|