- 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:
parent
1d36f3d95e
commit
2a142e3afc
14 changed files with 182 additions and 56 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -4,5 +4,10 @@ node_modules
|
|||
/guaribas.sqlite
|
||||
/guaribas.log
|
||||
/work
|
||||
/tmp
|
||||
/docs
|
||||
/.env
|
||||
/chart.html
|
||||
/chart.png
|
||||
/chart.svg
|
||||
/gbtrace.log
|
||||
|
|
|
@ -40,6 +40,8 @@ import { Sequelize } from 'sequelize-typescript';
|
|||
import { IGBCoreService } from 'botlib';
|
||||
|
||||
export class GBAdminPackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
}
|
||||
unloadPackage(core: IGBCoreService): void {
|
||||
|
|
|
@ -42,6 +42,8 @@ import { Sequelize } from "sequelize-typescript";
|
|||
|
||||
export class GBAnalyticsPackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
|
||||
}
|
||||
|
|
|
@ -43,7 +43,8 @@ import { Sequelize } from "sequelize-typescript";
|
|||
import { GuaribasInstance, GuaribasException, GuaribasPackage, GuaribasChannel } from "./models/GBModel";
|
||||
|
||||
export class GBCorePackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
core.sequelize.addModels([
|
||||
GuaribasInstance,
|
||||
|
|
|
@ -91,9 +91,11 @@ export class GuaribasInstance extends Model<GuaribasInstance> implements IGBInst
|
|||
|
||||
@Column webchatKey: string;
|
||||
|
||||
@Column whatsappKey: string;
|
||||
@Column whatsappBotKey: string;
|
||||
|
||||
@Column whatsappServiceKey: string;
|
||||
|
||||
@Column spellCheckerKey: string;
|
||||
@Column spellcheckerKey: string;
|
||||
|
||||
@Column theme: string;
|
||||
|
||||
|
|
|
@ -57,6 +57,7 @@ import { GBDeployer } from './GBDeployer';
|
|||
import { GBSecurityPackage } from '../../security.gblib';
|
||||
import { GBAdminPackage } from './../../admin.gbapp/index';
|
||||
import { GBCustomerSatisfactionPackage } from "../../customer-satisfaction.gbapp";
|
||||
import { GBWhatsappPackage } from "../../whatsapp.gblib";
|
||||
|
||||
/** Minimal service layer for a bot. */
|
||||
|
||||
|
@ -155,7 +156,24 @@ export class GBMinService {
|
|||
min.core = _this.core;
|
||||
min.conversationalService = _this.conversationalService;
|
||||
_this.core.loadInstance(min.botId, (data, err) => {
|
||||
|
||||
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({
|
||||
|
@ -189,9 +207,6 @@ export class GBMinService {
|
|||
storage: inMemoryStorage
|
||||
});
|
||||
|
||||
// Call the loadBot event.
|
||||
|
||||
appPackages.forEach(e => e.loadBot(min));
|
||||
|
||||
// Setups handlers.
|
||||
|
||||
|
@ -215,7 +230,7 @@ export class GBMinService {
|
|||
}
|
||||
|
||||
appPackages.forEach(e => {
|
||||
e.onNewSession(min, session)
|
||||
e.onNewSession(min, session);
|
||||
});
|
||||
|
||||
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.
|
||||
|
||||
|
@ -288,7 +296,7 @@ export class GBMinService {
|
|||
}
|
||||
|
||||
/** 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) => {
|
||||
try {
|
||||
|
@ -356,7 +364,7 @@ export class GBMinService {
|
|||
|
||||
WaitUntil()
|
||||
.interval(1000)
|
||||
.times(5)
|
||||
.times(10)
|
||||
.condition(function (cb) {
|
||||
logger.trace(`Waiting for app package deployment...`);
|
||||
cb(appPackagesProcessed == gbappPackages.length);
|
||||
|
|
|
@ -41,7 +41,7 @@ import { Session } from 'botbuilder';
|
|||
import { Sequelize } from 'sequelize-typescript';
|
||||
|
||||
export class GBCustomerSatisfactionPackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
core.sequelize.addModels([
|
||||
GuaribasQuestionAlternate
|
||||
|
|
|
@ -69,7 +69,7 @@ export class AskDialog extends IGBDialog {
|
|||
session.replaceDialog("/menu");
|
||||
} else {
|
||||
AzureText.getSpelledText(
|
||||
min.instance.spellCheckerKey,
|
||||
min.instance.spellcheckerKey,
|
||||
text,
|
||||
(data, err) => {
|
||||
if (data != text) {
|
||||
|
|
|
@ -44,6 +44,9 @@ import { Sequelize } from 'sequelize-typescript';
|
|||
import { IGBCoreService } from 'botlib';
|
||||
|
||||
export class GBKBPackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
core.sequelize.addModels([
|
||||
GuaribasAnswer,
|
||||
|
|
|
@ -41,7 +41,7 @@ import { Sequelize } from "sequelize-typescript";
|
|||
import { GuaribasUser, GuaribasGroup, GuaribasUserGroup } from "./models";
|
||||
|
||||
export class GBSecurityPackage implements IGBPackage {
|
||||
|
||||
sysPackages: IGBPackage[] = null;
|
||||
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
|
||||
core.sequelize.addModels([
|
||||
GuaribasGroup,
|
||||
|
|
65
deploy/whatsapp.gblib/index.ts
Normal file
65
deploy/whatsapp.gblib/index.ts
Normal 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 {
|
||||
|
||||
}
|
||||
}
|
|
@ -39,8 +39,9 @@ const UrlJoin = require("url-join");
|
|||
const Walk = require("fs-walk");
|
||||
const logger = require("../../../src/logger");
|
||||
const Swagger = require('swagger-client');
|
||||
const open = require('open');
|
||||
const rp = require('request-promise');
|
||||
import * as request from "request-promise-native";
|
||||
|
||||
import { GBServiceCallback, GBService, IGBInstance } from "botlib";
|
||||
|
||||
export class WhatsappDirectLine extends GBService {
|
||||
|
@ -55,6 +56,8 @@ export class WhatsappDirectLine extends GBService {
|
|||
|
||||
this.directLineSecret = directLineSecret;
|
||||
|
||||
|
||||
// TODO: Migrate to Swagger 3.
|
||||
let directLineClient = rp(this.directLineSpecUrl)
|
||||
.then(function (spec) {
|
||||
return new Swagger({
|
||||
|
@ -64,21 +67,23 @@ export class WhatsappDirectLine extends GBService {
|
|||
})
|
||||
.then(function (client) {
|
||||
client.clientAuthorizations.add('AuthorizationBotConnector',
|
||||
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + this.directLineSecret, 'header'));
|
||||
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
|
||||
return client;
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.error('Error initializing DirectLine client', err);
|
||||
});
|
||||
|
||||
// TODO: Remove *this* issue.
|
||||
let _this = this;
|
||||
directLineClient.then(function (client) {
|
||||
client.Conversations.Conversations_StartConversation() // create conversation
|
||||
client.Conversations.Conversations_StartConversation()
|
||||
.then(function (response) {
|
||||
return response.obj.conversationId;
|
||||
}) // obtain id
|
||||
})
|
||||
.then(function (conversationId) {
|
||||
this.sendMessagesFromConsole(client, conversationId); // start watching console input for sending new messages to bot
|
||||
this.pollMessages(client, conversationId); // start polling messages from bot
|
||||
_this.sendMessagesFromConsole(client, conversationId);
|
||||
_this.pollMessages(client, conversationId);
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.error('Error starting conversation', err);
|
||||
|
@ -87,6 +92,7 @@ export class WhatsappDirectLine extends GBService {
|
|||
}
|
||||
|
||||
sendMessagesFromConsole(client, conversationId) {
|
||||
let _this = this;
|
||||
var stdin = process.openStdin();
|
||||
process.stdout.write('Command> ');
|
||||
stdin.addListener('data', function (e) {
|
||||
|
@ -97,7 +103,6 @@ export class WhatsappDirectLine extends GBService {
|
|||
return process.exit();
|
||||
}
|
||||
|
||||
// send message
|
||||
client.Conversations.Conversations_PostActivity(
|
||||
{
|
||||
conversationId: conversationId,
|
||||
|
@ -106,8 +111,8 @@ export class WhatsappDirectLine extends GBService {
|
|||
text: input,
|
||||
type: 'message',
|
||||
from: {
|
||||
id: this.directLineClientName,
|
||||
name: this.directLineClientName
|
||||
id: _this.directLineClientName,
|
||||
name: _this.directLineClientName
|
||||
}
|
||||
}
|
||||
}).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) {
|
||||
let _this = this;
|
||||
console.log('Starting polling message for conversationId: ' + conversationId);
|
||||
var watermark = null;
|
||||
setInterval(function () {
|
||||
|
@ -129,19 +135,22 @@ export class WhatsappDirectLine extends GBService {
|
|||
watermark = response.obj.watermark; // use watermark so subsequent requests skip old messages
|
||||
return response.obj.activities;
|
||||
})
|
||||
.then(this.printMessages);
|
||||
.then(_this.printMessages, _this.directLineClientName);
|
||||
}, this.pollInterval);
|
||||
}
|
||||
|
||||
printMessages(activities) {
|
||||
printMessages(activities, directLineClientName) {
|
||||
|
||||
if (activities && activities.length) {
|
||||
// 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) {
|
||||
|
||||
// print other messages
|
||||
activities.forEach(this.printMessage);
|
||||
activities.forEach(activity => {
|
||||
console.log(activity.text);
|
||||
});
|
||||
|
||||
process.stdout.write('Command> ');
|
||||
}
|
||||
|
@ -183,4 +192,29 @@ export class WhatsappDirectLine extends GBService {
|
|||
console.log('*' + contentLine(attachment.content.text) + '*');
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -31,7 +31,8 @@
|
|||
"@types/url-join": "^0.8.2",
|
||||
"async": "^1.5.2",
|
||||
"botbuilder": "^3.14.0",
|
||||
"botlib": "0.0.12",
|
||||
|
||||
"botlib": "0.0.16",
|
||||
"chai": "^4.1.2",
|
||||
"chokidar": "^2.0.2",
|
||||
"csv-parse": "^2.4.0",
|
||||
|
@ -48,12 +49,15 @@
|
|||
"sequelize": "^4.37.6",
|
||||
"sequelize-typescript": "^0.6.3",
|
||||
"sqlite3": "^4.0.0",
|
||||
|
||||
"tedious": "^2.1.1",
|
||||
"ts-node": "3.3.0",
|
||||
"typedoc": "^0.10.0",
|
||||
"typescript": "2.7.2",
|
||||
"url-join": "^4.0.0",
|
||||
"wait-until": "0.0.2",
|
||||
"winston": "^2.4.0"
|
||||
"winston": "^2.4.0",
|
||||
|
||||
"swagger-client": "^2.1.18"
|
||||
}
|
||||
}
|
||||
|
|
42
src/app.ts
42
src/app.ts
|
@ -1,4 +1,4 @@
|
|||
#! /usr/bin/env node
|
||||
#! /usr/bin / env node
|
||||
/*****************************************************************************\
|
||||
| ( )_ _ |
|
||||
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
||||
|
@ -42,6 +42,7 @@ import { GBConfigService } from "../deploy/core.gbapp/services/GBConfigService";
|
|||
import { GBConversationalService } from "../deploy/core.gbapp/services/GBConversationalService";
|
||||
import { GBMinService } from "../deploy/core.gbapp/services/GBMinService";
|
||||
import { GBDeployer } from "../deploy/core.gbapp/services/GBDeployer";
|
||||
import { GBWhatsappPackage } from './../deploy/whatsapp.gblib/index';
|
||||
import { GBCoreService } from "../deploy/core.gbapp/services/GBCoreService";
|
||||
import { GBImporter } from "../deploy/core.gbapp/services/GBImporter";
|
||||
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 { IGBPackage } from 'botlib';
|
||||
|
||||
let appPackages = new Array<IGBPackage>();
|
||||
|
||||
|
||||
/**
|
||||
* General Bots open-core entry point.
|
||||
*/
|
||||
|
@ -60,19 +64,17 @@ export class GBServer {
|
|||
/** Program entry-point. */
|
||||
static run() {
|
||||
|
||||
logger.info("Starting General Bots Open Core (Guaribas)...");
|
||||
|
||||
// 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
|
||||
// the Marketplace until GB get serverless.
|
||||
|
||||
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();
|
||||
|
||||
server.listen(port, () => {
|
||||
|
||||
logger.info(`General Bot - RUNNING on ${port}...`);
|
||||
logger.info(`Accepting connections on ${port}...`);
|
||||
logger.info(`Starting instances...`);
|
||||
|
||||
// Reads basic configuration, initialize minimal services.
|
||||
|
@ -90,25 +92,23 @@ export class GBServer {
|
|||
let conversationalService = new GBConversationalService(core);
|
||||
let minService = new GBMinService(core, conversationalService, deployer);
|
||||
|
||||
let sysPackages = new Array<IGBPackage>();
|
||||
|
||||
[GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage,
|
||||
GBKBPackage, GBCustomerSatisfactionPackage].forEach(e => {
|
||||
logger.trace(`Loading sys package: ${e.name}...`);
|
||||
let p = Object.create(e.prototype) as IGBPackage;
|
||||
p.loadPackage(core, core.sequelize);
|
||||
sysPackages.push(p);
|
||||
});
|
||||
[GBAdminPackage, GBAnalyticsPackage, GBCorePackage, GBSecurityPackage,
|
||||
GBKBPackage, GBCustomerSatisfactionPackage, GBWhatsappPackage].forEach(e => {
|
||||
logger.trace(`Loading sys package: ${e.name}...`);
|
||||
let p = Object.create(e.prototype) as IGBPackage;
|
||||
p.loadPackage(core, core.sequelize);
|
||||
|
||||
});
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
let appPackages = new Array<IGBPackage>();
|
||||
await minService.deployPackages(core, server, appPackages, sysPackages);
|
||||
|
||||
minService.buildMin(instance => {
|
||||
logger.info(`Instance loaded: ${instance.botId}...`);
|
||||
}, server, appPackages);
|
||||
|
||||
await minService.deployPackages(core, server, appPackages);
|
||||
logger.info(`The Bot Server is in RUNNING mode...`);
|
||||
|
||||
minService.buildMin(instance => {
|
||||
logger.info(`Instance loaded: ${instance.botId}...`);
|
||||
}, server, appPackages);
|
||||
|
||||
|
||||
} catch (err) {
|
||||
logger.log(err)
|
||||
|
|
Loading…
Add table
Reference in a new issue