- NEW: Now each .gbapp has it own set of syspackages loaded.

- NEW: Added support for Whatsapp external service key on bot instance model.
This commit is contained in:
Rodrigo Rodriguez 2018-05-11 22:18:38 -03:00
parent 1d36f3d95e
commit 2a142e3afc
14 changed files with 182 additions and 56 deletions

5
.gitignore vendored
View file

@ -4,5 +4,10 @@ node_modules
/guaribas.sqlite /guaribas.sqlite
/guaribas.log /guaribas.log
/work /work
/tmp
/docs /docs
/.env /.env
/chart.html
/chart.png
/chart.svg
/gbtrace.log

View file

@ -40,6 +40,8 @@ import { Sequelize } from 'sequelize-typescript';
import { IGBCoreService } from 'botlib'; import { IGBCoreService } from 'botlib';
export class GBAdminPackage implements IGBPackage { export class GBAdminPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
} }
unloadPackage(core: IGBCoreService): void { unloadPackage(core: IGBCoreService): void {

View file

@ -42,6 +42,8 @@ import { Sequelize } from "sequelize-typescript";
export class GBAnalyticsPackage implements IGBPackage { export class GBAnalyticsPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
} }

View file

@ -43,6 +43,7 @@ import { Sequelize } from "sequelize-typescript";
import { GuaribasInstance, GuaribasException, GuaribasPackage, GuaribasChannel } from "./models/GBModel"; import { GuaribasInstance, GuaribasException, GuaribasPackage, GuaribasChannel } from "./models/GBModel";
export class GBCorePackage implements IGBPackage { export class GBCorePackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
core.sequelize.addModels([ core.sequelize.addModels([

View file

@ -91,9 +91,11 @@ export class GuaribasInstance extends Model<GuaribasInstance> implements IGBInst
@Column webchatKey: string; @Column webchatKey: string;
@Column whatsappKey: string; @Column whatsappBotKey: string;
@Column spellCheckerKey: string; @Column whatsappServiceKey: string;
@Column spellcheckerKey: string;
@Column theme: string; @Column theme: string;

View file

@ -57,6 +57,7 @@ import { GBDeployer } from './GBDeployer';
import { GBSecurityPackage } from '../../security.gblib'; import { GBSecurityPackage } from '../../security.gblib';
import { GBAdminPackage } from './../../admin.gbapp/index'; import { GBAdminPackage } from './../../admin.gbapp/index';
import { GBCustomerSatisfactionPackage } from "../../customer-satisfaction.gbapp"; import { GBCustomerSatisfactionPackage } from "../../customer-satisfaction.gbapp";
import { GBWhatsappPackage } from "../../whatsapp.gblib";
/** Minimal service layer for a bot. */ /** Minimal service layer for a bot. */
@ -155,7 +156,24 @@ export class GBMinService {
min.core = _this.core; min.core = _this.core;
min.conversationalService = _this.conversationalService; min.conversationalService = _this.conversationalService;
_this.core.loadInstance(min.botId, (data, err) => { _this.core.loadInstance(min.botId, (data, err) => {
min.instance = data; min.instance = data;
// Call the loadBot event for all packages.
appPackages.forEach(e => {
e.sysPackages = new Array<IGBPackage>();
[GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage,
GBKBPackage, GBCustomerSatisfactionPackage, GBWhatsappPackage].forEach(sysPackage => {
logger.trace(`Loading sys package: ${sysPackage.name}...`);
let p = Object.create(sysPackage.prototype) as IGBPackage;
p.loadBot(min);
e.sysPackages.push(p);
});
e.loadBot(min);
});
}); });
let connector = new gBuilder.ChatConnector({ let connector = new gBuilder.ChatConnector({
@ -189,9 +207,6 @@ export class GBMinService {
storage: inMemoryStorage storage: inMemoryStorage
}); });
// Call the loadBot event.
appPackages.forEach(e => e.loadBot(min));
// Setups handlers. // Setups handlers.
@ -215,7 +230,7 @@ export class GBMinService {
} }
appPackages.forEach(e => { appPackages.forEach(e => {
e.onNewSession(min, session) e.onNewSession(min, session);
}); });
next(); next();
@ -272,13 +287,6 @@ export class GBMinService {
} }
}); });
let generalPackages = [GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage, GBKBPackage, GBCustomerSatisfactionPackage];
generalPackages.forEach(e => {
logger.trace(`Loading package: ${e.name}...`);
let p = Object.create(e.prototype) as IGBPackage;
p.loadBot(min);
});
// Specialized load for each min instance. // Specialized load for each min instance.
@ -288,7 +296,7 @@ export class GBMinService {
} }
/** Performs package deployment in all .gbai or default. */ /** Performs package deployment in all .gbai or default. */
public deployPackages(core: IGBCoreService, server: any, appPackages: Array<IGBPackage>, sysPackages: Array<IGBPackage>) { public deployPackages(core: IGBCoreService, server: any, appPackages: Array<IGBPackage>) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
@ -356,7 +364,7 @@ export class GBMinService {
WaitUntil() WaitUntil()
.interval(1000) .interval(1000)
.times(5) .times(10)
.condition(function (cb) { .condition(function (cb) {
logger.trace(`Waiting for app package deployment...`); logger.trace(`Waiting for app package deployment...`);
cb(appPackagesProcessed == gbappPackages.length); cb(appPackagesProcessed == gbappPackages.length);

View file

@ -41,7 +41,7 @@ import { Session } from 'botbuilder';
import { Sequelize } from 'sequelize-typescript'; import { Sequelize } from 'sequelize-typescript';
export class GBCustomerSatisfactionPackage implements IGBPackage { export class GBCustomerSatisfactionPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
core.sequelize.addModels([ core.sequelize.addModels([
GuaribasQuestionAlternate GuaribasQuestionAlternate

View file

@ -69,7 +69,7 @@ export class AskDialog extends IGBDialog {
session.replaceDialog("/menu"); session.replaceDialog("/menu");
} else { } else {
AzureText.getSpelledText( AzureText.getSpelledText(
min.instance.spellCheckerKey, min.instance.spellcheckerKey,
text, text,
(data, err) => { (data, err) => {
if (data != text) { if (data != text) {

View file

@ -44,6 +44,9 @@ import { Sequelize } from 'sequelize-typescript';
import { IGBCoreService } from 'botlib'; import { IGBCoreService } from 'botlib';
export class GBKBPackage implements IGBPackage { export class GBKBPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
core.sequelize.addModels([ core.sequelize.addModels([
GuaribasAnswer, GuaribasAnswer,

View file

@ -41,7 +41,7 @@ import { Sequelize } from "sequelize-typescript";
import { GuaribasUser, GuaribasGroup, GuaribasUserGroup } from "./models"; import { GuaribasUser, GuaribasGroup, GuaribasUserGroup } from "./models";
export class GBSecurityPackage implements IGBPackage { export class GBSecurityPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void { loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
core.sequelize.addModels([ core.sequelize.addModels([
GuaribasGroup, GuaribasGroup,

View file

@ -0,0 +1,65 @@
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| 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. |
| |
\*****************************************************************************/
"use strict";
const UrlJoin = require("url-join");
import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib";
import { Session } from 'botbuilder';
import { Sequelize } from "sequelize-typescript";
import { WhatsappDirectLine } from "./services/directline";
export class GBWhatsappPackage implements IGBPackage {
sysPackages: IGBPackage[] = null;
channel: WhatsappDirectLine;
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
}
unloadPackage(core: IGBCoreService): void {
}
loadBot(min: GBMinInstance): void {
this.channel = new WhatsappDirectLine(min.instance.whatsappBotKey);
}
unloadBot(min: GBMinInstance): void {
}
onNewSession(min: GBMinInstance, session: Session): void {
}
}

View file

@ -39,8 +39,9 @@ const UrlJoin = require("url-join");
const Walk = require("fs-walk"); const Walk = require("fs-walk");
const logger = require("../../../src/logger"); const logger = require("../../../src/logger");
const Swagger = require('swagger-client'); const Swagger = require('swagger-client');
const open = require('open');
const rp = require('request-promise'); const rp = require('request-promise');
import * as request from "request-promise-native";
import { GBServiceCallback, GBService, IGBInstance } from "botlib"; import { GBServiceCallback, GBService, IGBInstance } from "botlib";
export class WhatsappDirectLine extends GBService { export class WhatsappDirectLine extends GBService {
@ -55,6 +56,8 @@ export class WhatsappDirectLine extends GBService {
this.directLineSecret = directLineSecret; this.directLineSecret = directLineSecret;
// TODO: Migrate to Swagger 3.
let directLineClient = rp(this.directLineSpecUrl) let directLineClient = rp(this.directLineSpecUrl)
.then(function (spec) { .then(function (spec) {
return new Swagger({ return new Swagger({
@ -64,21 +67,23 @@ export class WhatsappDirectLine extends GBService {
}) })
.then(function (client) { .then(function (client) {
client.clientAuthorizations.add('AuthorizationBotConnector', client.clientAuthorizations.add('AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + this.directLineSecret, 'header')); new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
return client; return client;
}) })
.catch(function (err) { .catch(function (err) {
console.error('Error initializing DirectLine client', err); console.error('Error initializing DirectLine client', err);
}); });
// TODO: Remove *this* issue.
let _this = this;
directLineClient.then(function (client) { directLineClient.then(function (client) {
client.Conversations.Conversations_StartConversation() // create conversation client.Conversations.Conversations_StartConversation()
.then(function (response) { .then(function (response) {
return response.obj.conversationId; return response.obj.conversationId;
}) // obtain id })
.then(function (conversationId) { .then(function (conversationId) {
this.sendMessagesFromConsole(client, conversationId); // start watching console input for sending new messages to bot _this.sendMessagesFromConsole(client, conversationId);
this.pollMessages(client, conversationId); // start polling messages from bot _this.pollMessages(client, conversationId);
}) })
.catch(function (err) { .catch(function (err) {
console.error('Error starting conversation', err); console.error('Error starting conversation', err);
@ -87,6 +92,7 @@ export class WhatsappDirectLine extends GBService {
} }
sendMessagesFromConsole(client, conversationId) { sendMessagesFromConsole(client, conversationId) {
let _this = this;
var stdin = process.openStdin(); var stdin = process.openStdin();
process.stdout.write('Command> '); process.stdout.write('Command> ');
stdin.addListener('data', function (e) { stdin.addListener('data', function (e) {
@ -97,7 +103,6 @@ export class WhatsappDirectLine extends GBService {
return process.exit(); return process.exit();
} }
// send message
client.Conversations.Conversations_PostActivity( client.Conversations.Conversations_PostActivity(
{ {
conversationId: conversationId, conversationId: conversationId,
@ -106,8 +111,8 @@ export class WhatsappDirectLine extends GBService {
text: input, text: input,
type: 'message', type: 'message',
from: { from: {
id: this.directLineClientName, id: _this.directLineClientName,
name: this.directLineClientName name: _this.directLineClientName
} }
} }
}).catch(function (err) { }).catch(function (err) {
@ -119,8 +124,9 @@ export class WhatsappDirectLine extends GBService {
}); });
} }
/** Poll Messages from conversation using DirectLine client */ /** TBD: Poll Messages from conversation using DirectLine client */
pollMessages(client, conversationId) { pollMessages(client, conversationId) {
let _this = this;
console.log('Starting polling message for conversationId: ' + conversationId); console.log('Starting polling message for conversationId: ' + conversationId);
var watermark = null; var watermark = null;
setInterval(function () { setInterval(function () {
@ -129,19 +135,22 @@ export class WhatsappDirectLine extends GBService {
watermark = response.obj.watermark; // use watermark so subsequent requests skip old messages watermark = response.obj.watermark; // use watermark so subsequent requests skip old messages
return response.obj.activities; return response.obj.activities;
}) })
.then(this.printMessages); .then(_this.printMessages, _this.directLineClientName);
}, this.pollInterval); }, this.pollInterval);
} }
printMessages(activities) { printMessages(activities, directLineClientName) {
if (activities && activities.length) { if (activities && activities.length) {
// ignore own messages // ignore own messages
activities = activities.filter(function (m) { return m.from.id !== this.directLineClientName }); activities = activities.filter(function (m) { return m.from.id !== directLineClientName });
if (activities.length) { if (activities.length) {
// print other messages // print other messages
activities.forEach(this.printMessage); activities.forEach(activity => {
console.log(activity.text);
});
process.stdout.write('Command> '); process.stdout.write('Command> ');
} }
@ -183,4 +192,29 @@ export class WhatsappDirectLine extends GBService {
console.log('*' + contentLine(attachment.content.text) + '*'); console.log('*' + contentLine(attachment.content.text) + '*');
console.log('*'.repeat(width + 1) + '/'); console.log('*'.repeat(width + 1) + '/');
} }
async sendToDevice(senderID, msg) {
var options = {
method: 'POST',
url: 'https://www.waboxapp.com/api/send/chat',
qs:
{
token: '',
uid: '55****388**',
to: senderID,
custom_uid: 'GBZAP' + (new Date()).toISOString,
text: msg
},
headers:
{
'cache-control': 'no-cache'
}
};
const result = await request.get(options);
}
} }

View file

@ -31,7 +31,8 @@
"@types/url-join": "^0.8.2", "@types/url-join": "^0.8.2",
"async": "^1.5.2", "async": "^1.5.2",
"botbuilder": "^3.14.0", "botbuilder": "^3.14.0",
"botlib": "0.0.12",
"botlib": "0.0.16",
"chai": "^4.1.2", "chai": "^4.1.2",
"chokidar": "^2.0.2", "chokidar": "^2.0.2",
"csv-parse": "^2.4.0", "csv-parse": "^2.4.0",
@ -48,12 +49,15 @@
"sequelize": "^4.37.6", "sequelize": "^4.37.6",
"sequelize-typescript": "^0.6.3", "sequelize-typescript": "^0.6.3",
"sqlite3": "^4.0.0", "sqlite3": "^4.0.0",
"tedious": "^2.1.1", "tedious": "^2.1.1",
"ts-node": "3.3.0", "ts-node": "3.3.0",
"typedoc": "^0.10.0", "typedoc": "^0.10.0",
"typescript": "2.7.2", "typescript": "2.7.2",
"url-join": "^4.0.0", "url-join": "^4.0.0",
"wait-until": "0.0.2", "wait-until": "0.0.2",
"winston": "^2.4.0" "winston": "^2.4.0",
"swagger-client": "^2.1.18"
} }
} }

View file

@ -42,6 +42,7 @@ import { GBConfigService } from "../deploy/core.gbapp/services/GBConfigService";
import { GBConversationalService } from "../deploy/core.gbapp/services/GBConversationalService"; import { GBConversationalService } from "../deploy/core.gbapp/services/GBConversationalService";
import { GBMinService } from "../deploy/core.gbapp/services/GBMinService"; import { GBMinService } from "../deploy/core.gbapp/services/GBMinService";
import { GBDeployer } from "../deploy/core.gbapp/services/GBDeployer"; import { GBDeployer } from "../deploy/core.gbapp/services/GBDeployer";
import { GBWhatsappPackage } from './../deploy/whatsapp.gblib/index';
import { GBCoreService } from "../deploy/core.gbapp/services/GBCoreService"; import { GBCoreService } from "../deploy/core.gbapp/services/GBCoreService";
import { GBImporter } from "../deploy/core.gbapp/services/GBImporter"; import { GBImporter } from "../deploy/core.gbapp/services/GBImporter";
import { GBAnalyticsPackage } from "../deploy/analytics.gblib"; import { GBAnalyticsPackage } from "../deploy/analytics.gblib";
@ -52,6 +53,9 @@ import { GBAdminPackage } from '../deploy/admin.gbapp/index';
import { GBCustomerSatisfactionPackage } from "../deploy/customer-satisfaction.gbapp"; import { GBCustomerSatisfactionPackage } from "../deploy/customer-satisfaction.gbapp";
import { IGBPackage } from 'botlib'; import { IGBPackage } from 'botlib';
let appPackages = new Array<IGBPackage>();
/** /**
* General Bots open-core entry point. * General Bots open-core entry point.
*/ */
@ -60,19 +64,17 @@ export class GBServer {
/** Program entry-point. */ /** Program entry-point. */
static run() { static run() {
logger.info("Starting General Bots Open Core (Guaribas)...");
// Creates a basic HTTP server that will serve several URL, one for each // Creates a basic HTTP server that will serve several URL, one for each
// bot instance. This allows the same server to attend multiple Bot on // bot instance. This allows the same server to attend multiple Bot on
// the Marketplace until GB get serverless. // the Marketplace until GB get serverless.
let port = process.env.port || process.env.PORT || 4242; let port = process.env.port || process.env.PORT || 4242;
logger.info(`Starting HTTP BotServer...`); logger.info(`The Bot Server is in STARTING mode...`);
let server = express(); let server = express();
server.listen(port, () => { server.listen(port, () => {
logger.info(`General Bot - RUNNING on ${port}...`); logger.info(`Accepting connections on ${port}...`);
logger.info(`Starting instances...`); logger.info(`Starting instances...`);
// Reads basic configuration, initialize minimal services. // Reads basic configuration, initialize minimal services.
@ -90,20 +92,18 @@ export class GBServer {
let conversationalService = new GBConversationalService(core); let conversationalService = new GBConversationalService(core);
let minService = new GBMinService(core, conversationalService, deployer); let minService = new GBMinService(core, conversationalService, deployer);
let sysPackages = new Array<IGBPackage>();
[GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage, [GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage,
GBKBPackage, GBCustomerSatisfactionPackage].forEach(e => { GBKBPackage, GBCustomerSatisfactionPackage, GBWhatsappPackage].forEach(e => {
logger.trace(`Loading sys package: ${e.name}...`); logger.trace(`Loading sys package: ${e.name}...`);
let p = Object.create(e.prototype) as IGBPackage; let p = Object.create(e.prototype) as IGBPackage;
p.loadPackage(core, core.sequelize); p.loadPackage(core, core.sequelize);
sysPackages.push(p);
}); });
(async () => { (async () => {
try { try {
let appPackages = new Array<IGBPackage>(); await minService.deployPackages(core, server, appPackages);
await minService.deployPackages(core, server, appPackages, sysPackages); logger.info(`The Bot Server is in RUNNING mode...`);
minService.buildMin(instance => { minService.buildMin(instance => {
logger.info(`Instance loaded: ${instance.botId}...`); logger.info(`Instance loaded: ${instance.botId}...`);