fix(whatsapp.gblib): Improved multibot behavior sharing Whatsapp config.

This commit is contained in:
Rodrigo Rodriguez 2020-04-15 01:42:54 +00:00
parent fac588827f
commit fc4970760a
5 changed files with 19165 additions and 54 deletions

19102
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -289,13 +289,11 @@ STORAGE_SYNC=true
} }
} }
public setEntryPointDialog(dialogName: string) public setEntryPointDialog(dialogName: string) {
{
GBServer.globals.entryPointDialog = dialogName; GBServer.globals.entryPointDialog = dialogName;
} }
public setWWWRoot(localPath: string) public setWWWRoot(localPath: string) {
{
GBServer.globals.wwwroot = localPath; GBServer.globals.wwwroot = localPath;
} }
@ -331,20 +329,24 @@ STORAGE_SYNC=true
let instances: IGBInstance[]; let instances: IGBInstance[];
try { try {
instances = await core.loadInstances(); instances = await core.loadInstances();
const instance = instances[0]; await CollectionUtil.asyncForEach(instances, async instance => {
if (process.env.NODE_ENV === 'development' && GBLog.info(`Updating bot endpoint for ${instance.botId}...`);
GBConfigService.get('REVERSE_PROXY') === undefined) {
GBLog.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
try { try {
await installationDeployer.updateBotProxy( await installationDeployer.updateBotProxy(
instance.botId, instance.botId,
instance.botId, GBConfigService.get("CLOUD_GROUP"),
`${proxyAddress}/api/messages/${instance.botId}` `${proxyAddress}/api/messages/${instance.botId}`
); );
} catch (error) { } catch (error) {
throw new Error(`Error updating bot proxy with proxy address${error.message}.`); if (error.code === "ResourceNotFound") {
GBLog.warn(`Bot ${instance.botId} not found on resource group ${GBConfigService.get("CLOUD_GROUP")}.`);
}
else {
throw new Error(`Error updating bot proxy, details: ${error.message}.`);
} }
} }
});
} catch (error) { } catch (error) {
if (error.parent === undefined) { if (error.parent === undefined) {
throw new Error(`Cannot connect to operating storage: ${error.message}.`); throw new Error(`Cannot connect to operating storage: ${error.message}.`);

View file

@ -157,17 +157,18 @@ export class GBMinService {
// User wants to switch bots. // User wants to switch bots.
if (toSwitchMin !== undefined) { if (toSwitchMin !== undefined) {
await sec.updateCurrentBotId(id, text); await sec.updateCurrentBotId(id, text);
await (activeMin as any).whatsAppDirectLine.resetConversationId(id);
await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Agora falando com ${activeMin.instance.title}...`); await (activeMin as any).whatsAppDirectLine.sendToDevice(id, `Agora falando com ${activeMin.instance.title}...`);
res.end(); res.end();
} }
else { else {
activeMin = GBServer.globals.minInstances.filter(p => p.botId === user.currentBotId)[0];; activeMin = GBServer.globals.minInstances.filter(p => p.botId === user.currentBotId)[0];;
(activeMin as any).whatsAppDirectLine.received(req, res); await (activeMin as any).whatsAppDirectLine.received(req, res);
} }
} }
} }
else { else {
(GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res); await (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res);
} }
} catch (error) { } catch (error) {
GBLog.error(`Error on Whatsapp callback: ${error.message}`); GBLog.error(`Error on Whatsapp callback: ${error.message}`);
@ -198,10 +199,6 @@ export class GBMinService {
// Build bot adapter. // Build bot adapter.
const { min, adapter, conversationState } = await this.buildBotAdapter(instance, GBServer.globals.sysPackages); const { min, adapter, conversationState } = await this.buildBotAdapter(instance, GBServer.globals.sysPackages);
if (GBServer.globals.minInstances.length === 0) {
GBServer.globals.minBoot = min;
}
GBServer.globals.minInstances.push(min); GBServer.globals.minInstances.push(min);
// Install default BASIC module. // Install default BASIC module.
@ -391,6 +388,11 @@ export class GBMinService {
// The minimal bot is built here. // The minimal bot is built here.
const min = new GBMinInstance(); const min = new GBMinInstance();
if (GBServer.globals.minBoot === undefined) {
GBServer.globals.minBoot = min;
}
min.botId = instance.botId; min.botId = instance.botId;
min.bot = adapter; min.bot = adapter;
min.userState = userState; min.userState = userState;
@ -404,6 +406,7 @@ export class GBMinService {
min.scriptMap = {}; min.scriptMap = {};
min.sandBoxMap = {}; min.sandBoxMap = {};
min.packages = sysPackages; min.packages = sysPackages;
if (min.instance.whatsappServiceKey !== null) { if (min.instance.whatsappServiceKey !== null) {
min.whatsAppDirectLine = new WhatsappDirectLine( min.whatsAppDirectLine = new WhatsappDirectLine(
min, min,
@ -413,10 +416,20 @@ export class GBMinService {
min.instance.whatsappServiceNumber, min.instance.whatsappServiceNumber,
min.instance.whatsappServiceUrl min.instance.whatsappServiceUrl
); );
await min.whatsAppDirectLine.setup(true);
} }
else { else {
const minBoot = GBServer.globals.minBoot as any;
min.whatsAppDirectLine = min.whatsAppDirectLine =
(GBServer.globals.minBoot as any).whatsAppDirectLine; new WhatsappDirectLine(
min,
min.botId,
min.instance.webchatKey,
minBoot.instance.whatsappServiceKey,
minBoot.instance.whatsappServiceNumber,
minBoot.instance.whatsappServiceUrl
);
await min.whatsAppDirectLine.setup(false);
} }
min.userProfile = conversationState.createProperty('userProfile'); min.userProfile = conversationState.createProperty('userProfile');

View file

@ -47,18 +47,7 @@ export class GBWhatsappPackage implements IGBPackage {
public sysPackages: IGBPackage[]; public sysPackages: IGBPackage[];
public async loadBot(min: GBMinInstance): Promise<void> { public async loadBot(min: GBMinInstance): Promise<void> {
// Only loads engine if it is defined on services.json.
if (min.instance.whatsappServiceKey !== null) {
min.whatsAppDirectLine = new WhatsappDirectLine(min,
min.botId,
min.instance.whatsappBotKey,
min.instance.whatsappServiceKey,
min.instance.whatsappServiceNumber,
min.instance.whatsappServiceUrl
);
await min.whatsAppDirectLine.setup();
}
} }
public async getDialogs(min: GBMinInstance) { public async getDialogs(min: GBMinInstance) {

View file

@ -77,7 +77,7 @@ export class WhatsappDirectLine extends GBService {
} }
public async setup() { public async setup(setUrl) {
this.directLineClient = this.directLineClient =
new Swagger({ new Swagger({
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')),
@ -103,13 +103,14 @@ export class WhatsappDirectLine extends GBService {
} }
}; };
if (setUrl) {
try { try {
let res = await request.post(options); let res = await request.post(options);
GBLog.info(res); GBLog.info(res);
} catch (error) { } catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`); GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
} }
}
} }
@ -119,6 +120,10 @@ export class WhatsappDirectLine extends GBService {
} }
} }
public resetConversationId(number) {
this.conversationIds[number] = undefined;
}
public async received(req, res) { public async received(req, res) {
if (req.body.messages === undefined) { if (req.body.messages === undefined) {