2018-05-07 20:45:11 -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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
const Path = require("path");
|
|
|
|
const Fs = require("fs");
|
|
|
|
const _ = require("lodash");
|
|
|
|
const Parse = require("csv-parse");
|
|
|
|
const Async = require("async");
|
|
|
|
const UrlJoin = require("url-join");
|
|
|
|
const Walk = require("fs-walk");
|
|
|
|
const logger = require("../../../src/logger");
|
|
|
|
const Swagger = require('swagger-client');
|
|
|
|
const rp = require('request-promise');
|
2018-05-11 22:18:38 -03:00
|
|
|
import * as request from "request-promise-native";
|
|
|
|
|
2018-05-07 20:45:11 -03:00
|
|
|
import { GBServiceCallback, GBService, IGBInstance } from "botlib";
|
|
|
|
|
|
|
|
export class WhatsappDirectLine extends GBService {
|
|
|
|
|
|
|
|
pollInterval = 1000;
|
|
|
|
directLineClientName = 'DirectLineClient';
|
|
|
|
directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';
|
2018-05-12 16:08:24 -03:00
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
directLineClient: any;
|
|
|
|
whatsappServiceKey: string;
|
|
|
|
whatsappServiceNumber: string;
|
2018-05-12 16:08:24 -03:00
|
|
|
whatsappServiceUrl: string;
|
2018-05-12 13:40:34 -03:00
|
|
|
botId: string;
|
2018-05-13 18:28:24 -03:00
|
|
|
watermark: string = null;
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
conversationIds = {};
|
|
|
|
|
|
|
|
constructor(botId, directLineSecret, whatsappServiceKey, whatsappServiceNumber, whatsappServiceUrl) {
|
2018-05-07 20:45:11 -03:00
|
|
|
|
|
|
|
super();
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
this.botId = botId;
|
|
|
|
this.whatsappServiceKey = whatsappServiceKey;
|
|
|
|
this.whatsappServiceNumber = whatsappServiceNumber;
|
2018-05-12 16:08:24 -03:00
|
|
|
this.whatsappServiceUrl = whatsappServiceUrl;
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-05-11 22:18:38 -03:00
|
|
|
// TODO: Migrate to Swagger 3.
|
2018-05-12 13:40:34 -03:00
|
|
|
this.directLineClient = rp(this.directLineSpecUrl)
|
2018-05-12 16:08:24 -03:00
|
|
|
.then((spec) => {
|
2018-05-07 20:45:11 -03:00
|
|
|
return new Swagger({
|
|
|
|
spec: JSON.parse(spec.trim()),
|
|
|
|
usePromise: true
|
|
|
|
});
|
|
|
|
})
|
2018-05-12 16:08:24 -03:00
|
|
|
.then(async (client) => {
|
2018-05-07 20:45:11 -03:00
|
|
|
client.clientAuthorizations.add('AuthorizationBotConnector',
|
2018-05-12 13:40:34 -03:00
|
|
|
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' +
|
|
|
|
directLineSecret, 'header'));
|
2018-05-12 16:08:24 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
method: 'POST',
|
|
|
|
url: UrlJoin(this.whatsappServiceUrl, "webhook"),
|
|
|
|
qs:
|
|
|
|
{
|
|
|
|
token: this.whatsappServiceKey,
|
2018-05-13 18:28:24 -03:00
|
|
|
webhookUrl: `https://a2fc0900.ngrok.io/instances/${this.botId}/whatsapp`
|
2018-05-12 16:08:24 -03:00
|
|
|
},
|
|
|
|
headers:
|
|
|
|
{
|
|
|
|
'cache-control': 'no-cache'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await request.post(options);
|
2018-05-13 18:28:24 -03:00
|
|
|
logger.info(result);
|
2018-05-12 16:08:24 -03:00
|
|
|
} catch (error) {
|
|
|
|
logger.error('Error initializing DirectLine client', error);
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:45:11 -03:00
|
|
|
return client;
|
|
|
|
})
|
2018-05-12 16:08:24 -03:00
|
|
|
.catch((err) => {
|
2018-05-12 13:40:34 -03:00
|
|
|
logger.error('Error initializing DirectLine client', err);
|
2018-05-07 20:45:11 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
received(req, res) {
|
2018-05-12 16:08:24 -03:00
|
|
|
let text = req.body.messages[0].body;
|
|
|
|
let from = req.body.messages[0].author.split('@')[0];
|
|
|
|
let fromName = req.body.messages[0].senderName;
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2018-05-13 18:28:24 -03:00
|
|
|
if (req.body.messages[0].fromMe) {
|
2018-05-12 16:08:24 -03:00
|
|
|
return; // Exit here.
|
|
|
|
}
|
2018-05-13 18:28:24 -03:00
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
logger.info(`GBWhatsapp: Hook called. from: ${from}(${fromName}), text: ${text})`);
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
let conversationId = this.conversationIds[from];
|
2018-05-12 13:40:34 -03:00
|
|
|
|
|
|
|
this.directLineClient.then((client) => {
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
if (this.conversationIds[from] == null) {
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
logger.info(`GBWhatsapp: Starting new conversation on Bot.`);
|
|
|
|
client.Conversations.Conversations_StartConversation()
|
|
|
|
.then((response) => {
|
|
|
|
return response.obj.conversationId;
|
|
|
|
})
|
|
|
|
.then((conversationId) => {
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
this.conversationIds[from] = conversationId;
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
this.inputMessage(client, conversationId, text,
|
|
|
|
from, fromName);
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
this.pollMessages(client, conversationId, from, fromName);
|
2018-05-12 13:40:34 -03:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error('Error starting conversation', err);
|
2018-05-07 20:45:11 -03:00
|
|
|
});
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
} else {
|
|
|
|
this.inputMessage(client, conversationId, text,
|
|
|
|
from, fromName);
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2018-05-12 13:40:34 -03:00
|
|
|
res.end();
|
2018-05-07 20:45:11 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
|
|
|
|
inputMessage(client, conversationId, text, from, fromName) {
|
|
|
|
|
|
|
|
client.Conversations.Conversations_PostActivity(
|
|
|
|
{
|
|
|
|
conversationId: conversationId,
|
|
|
|
activity: {
|
|
|
|
textFormat: 'plain',
|
|
|
|
text: text,
|
|
|
|
type: 'message',
|
|
|
|
from: {
|
|
|
|
id: from,
|
|
|
|
name: fromName
|
|
|
|
},
|
|
|
|
replyToId: from
|
|
|
|
}
|
2018-05-12 16:08:24 -03:00
|
|
|
}).catch((err) => {
|
2018-05-12 13:40:34 -03:00
|
|
|
logger.error(`GBWhatsapp: Error receiving message: ${err}.`);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
pollMessages(client, conversationId, from, fromName) {
|
2018-05-12 13:40:34 -03:00
|
|
|
|
|
|
|
logger.info(`GBWhatsapp: Starting polling message for conversationId:
|
|
|
|
${conversationId}.`);
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
client.Conversations.Conversations_GetActivities({
|
|
|
|
conversationId:
|
2018-05-13 18:28:24 -03:00
|
|
|
conversationId, watermark: this.watermark
|
2018-05-12 13:40:34 -03:00
|
|
|
})
|
|
|
|
.then((response) => {
|
2018-05-13 18:28:24 -03:00
|
|
|
this.watermark = response.obj.watermark;
|
2018-05-07 20:45:11 -03:00
|
|
|
return response.obj.activities;
|
|
|
|
})
|
2018-05-12 13:40:34 -03:00
|
|
|
.then((activities) => {
|
2018-05-12 16:08:24 -03:00
|
|
|
this.printMessages(activities, conversationId, from, fromName);
|
2018-05-12 13:40:34 -03:00
|
|
|
});
|
2018-05-07 20:45:11 -03:00
|
|
|
}, this.pollInterval);
|
|
|
|
}
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
printMessages(activities, conversationId, from, fromName) {
|
2018-05-11 22:18:38 -03:00
|
|
|
|
2018-05-07 20:45:11 -03:00
|
|
|
if (activities && activities.length) {
|
2018-05-12 13:40:34 -03:00
|
|
|
|
|
|
|
// Ignore own messages.
|
|
|
|
|
2018-05-13 18:28:24 -03:00
|
|
|
activities = activities.filter((m) => { return m.from.id === this.botId && m.type === "message" });
|
2018-05-07 20:45:11 -03:00
|
|
|
|
|
|
|
if (activities.length) {
|
|
|
|
|
2018-05-12 13:40:34 -03:00
|
|
|
// Print other messages.
|
|
|
|
|
2018-05-13 18:28:24 -03:00
|
|
|
activities.forEach(activity => {
|
|
|
|
this.printMessage(activity, conversationId, from, fromName);
|
|
|
|
});
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
printMessage(activity, conversationId, from, fromName) {
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2018-05-13 18:28:24 -03:00
|
|
|
let output = "";
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2018-05-07 20:45:11 -03:00
|
|
|
if (activity.text) {
|
2018-05-12 13:40:34 -03:00
|
|
|
logger.info(`GBWhatsapp: MSG: ${activity.text}`);
|
|
|
|
output = activity.text;
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (activity.attachments) {
|
2018-05-12 16:08:24 -03:00
|
|
|
activity.attachments.forEach((attachment) => {
|
2018-05-07 20:45:11 -03:00
|
|
|
switch (attachment.contentType) {
|
|
|
|
case "application/vnd.microsoft.card.hero":
|
2018-05-13 18:28:24 -03:00
|
|
|
output += `\n${this.renderHeroCard(attachment)}`;
|
2018-05-07 20:45:11 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/png":
|
2018-05-12 13:40:34 -03:00
|
|
|
logger.info('Opening the requested image ' + attachment.contentUrl);
|
|
|
|
output += `\n${attachment.contentUrl}`;
|
2018-05-07 20:45:11 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-12 13:40:34 -03:00
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
this.sendToDevice(conversationId, from, fromName, output);
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderHeroCard(attachment) {
|
2018-05-13 18:28:24 -03:00
|
|
|
return `${attachment.content.title} - ${attachment.content.text}`;
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2018-05-11 22:18:38 -03:00
|
|
|
|
2018-05-12 16:08:24 -03:00
|
|
|
async sendToDevice(conversationId, to, toName, msg) {
|
2018-05-11 22:18:38 -03:00
|
|
|
var options = {
|
|
|
|
method: 'POST',
|
2018-05-12 16:08:24 -03:00
|
|
|
url: UrlJoin(this.whatsappServiceUrl, 'message'),
|
2018-05-11 22:18:38 -03:00
|
|
|
qs:
|
|
|
|
{
|
2018-05-12 13:40:34 -03:00
|
|
|
token: this.whatsappServiceKey,
|
2018-05-12 16:08:24 -03:00
|
|
|
phone: to,
|
|
|
|
body: msg
|
2018-05-11 22:18:38 -03:00
|
|
|
},
|
|
|
|
headers:
|
|
|
|
{
|
|
|
|
'cache-control': 'no-cache'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = await request.get(options);
|
|
|
|
}
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|