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;
}
public setWWWRoot(localPath: string)
{
public setWWWRoot(localPath: string) {
GBServer.globals.wwwroot = localPath;
}
@ -331,20 +329,24 @@ STORAGE_SYNC=true
let instances: IGBInstance[];
try {
instances = await core.loadInstances();
const instance = instances[0];
if (process.env.NODE_ENV === 'development' &&
GBConfigService.get('REVERSE_PROXY') === undefined) {
GBLog.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
await CollectionUtil.asyncForEach(instances, async instance => {
GBLog.info(`Updating bot endpoint for ${instance.botId}...`);
try {
await installationDeployer.updateBotProxy(
instance.botId,
instance.botId,
GBConfigService.get("CLOUD_GROUP"),
`${proxyAddress}/api/messages/${instance.botId}`
);
} 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) {
if (error.parent === undefined) {
throw new Error(`Cannot connect to operating storage: ${error.message}.`);
@ -386,30 +388,30 @@ STORAGE_SYNC=true
return instances;
}
public async loadSysPackages(core: GBCoreService) : Promise<IGBPackage[]>{
public async loadSysPackages(core: GBCoreService): Promise<IGBPackage[]> {
// NOTE: if there is any code before this line a semicolon
// will be necessary before this line.
// Loads all system packages.
const sysPackages: IGBPackage[] = [];
await CollectionUtil.asyncForEach([
GBAdminPackage,
GBCorePackage,
GBSecurityPackage,
GBKBPackage,
GBCustomerSatisfactionPackage,
GBAnalyticsPackage,
GBWhatsappPackage,
GBAzureDeployerPackage,
GBSharePointPackage,
], async e => {
await CollectionUtil.asyncForEach([
GBAdminPackage,
GBCorePackage,
GBSecurityPackage,
GBKBPackage,
GBCustomerSatisfactionPackage,
GBAnalyticsPackage,
GBWhatsappPackage,
GBAzureDeployerPackage,
GBSharePointPackage,
], async e => {
GBLog.info(`Loading sys package: ${e.name}...`);
const p = Object.create(e.prototype) as IGBPackage;
sysPackages.push(p);
await p.loadPackage(core, core.sequelize);
});
return sysPackages;

View file

@ -157,17 +157,18 @@ export class GBMinService {
// User wants to switch bots.
if (toSwitchMin !== undefined) {
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}...`);
res.end();
}
else {
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 {
(GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res);
await (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res);
}
} catch (error) {
GBLog.error(`Error on Whatsapp callback: ${error.message}`);
@ -198,10 +199,6 @@ export class GBMinService {
// Build bot adapter.
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);
// Install default BASIC module.
@ -391,6 +388,11 @@ export class GBMinService {
// The minimal bot is built here.
const min = new GBMinInstance();
if (GBServer.globals.minBoot === undefined) {
GBServer.globals.minBoot = min;
}
min.botId = instance.botId;
min.bot = adapter;
min.userState = userState;
@ -404,6 +406,7 @@ export class GBMinService {
min.scriptMap = {};
min.sandBoxMap = {};
min.packages = sysPackages;
if (min.instance.whatsappServiceKey !== null) {
min.whatsAppDirectLine = new WhatsappDirectLine(
min,
@ -413,12 +416,22 @@ export class GBMinService {
min.instance.whatsappServiceNumber,
min.instance.whatsappServiceUrl
);
await min.whatsAppDirectLine.setup(true);
}
else {
const minBoot = GBServer.globals.minBoot as any;
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');
const dialogState = conversationState.createProperty('dialogState');

View file

@ -47,18 +47,7 @@ export class GBWhatsappPackage implements IGBPackage {
public sysPackages: IGBPackage[];
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) {

View file

@ -53,7 +53,7 @@ export class WhatsappDirectLine extends GBService {
public whatsappServiceUrl: string;
public botId: string;
private directLineSecret: string;
public conversationIds = {};
min: GBMinInstance;
@ -77,7 +77,7 @@ export class WhatsappDirectLine extends GBService {
}
public async setup() {
public async setup(setUrl) {
this.directLineClient =
new Swagger({
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')),
@ -103,14 +103,15 @@ export class WhatsappDirectLine extends GBService {
}
};
try {
let res = await request.post(options);
GBLog.info(res);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
if (setUrl) {
try {
let res = await request.post(options);
GBLog.info(res);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
}
}
public static async asyncForEach(array, callback) {
@ -119,6 +120,10 @@ export class WhatsappDirectLine extends GBService {
}
}
public resetConversationId(number) {
this.conversationIds[number] = undefined;
}
public async received(req, res) {
if (req.body.messages === undefined) {
@ -146,12 +151,12 @@ export class WhatsappDirectLine extends GBService {
};
const res = await request(options);
let buf = Buffer.from(res, 'binary');
let buf = Buffer.from(res, 'binary');
text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
buf, 'pt-br'
);
);
}