fix(whatsapp.gblib): Improved multibot behavior sharing Whatsapp config.
This commit is contained in:
parent
fac588827f
commit
fc4970760a
5 changed files with 19165 additions and 54 deletions
19102
package-lock.json
generated
Normal file
19102
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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}.`);
|
||||||
|
@ -386,30 +388,30 @@ STORAGE_SYNC=true
|
||||||
return instances;
|
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
|
// NOTE: if there is any code before this line a semicolon
|
||||||
// will be necessary before this line.
|
// will be necessary before this line.
|
||||||
// Loads all system packages.
|
// Loads all system packages.
|
||||||
const sysPackages: IGBPackage[] = [];
|
const sysPackages: IGBPackage[] = [];
|
||||||
|
|
||||||
await CollectionUtil.asyncForEach([
|
await CollectionUtil.asyncForEach([
|
||||||
GBAdminPackage,
|
GBAdminPackage,
|
||||||
GBCorePackage,
|
GBCorePackage,
|
||||||
GBSecurityPackage,
|
GBSecurityPackage,
|
||||||
GBKBPackage,
|
GBKBPackage,
|
||||||
GBCustomerSatisfactionPackage,
|
GBCustomerSatisfactionPackage,
|
||||||
GBAnalyticsPackage,
|
GBAnalyticsPackage,
|
||||||
GBWhatsappPackage,
|
GBWhatsappPackage,
|
||||||
GBAzureDeployerPackage,
|
GBAzureDeployerPackage,
|
||||||
GBSharePointPackage,
|
GBSharePointPackage,
|
||||||
], async e => {
|
], async e => {
|
||||||
GBLog.info(`Loading sys package: ${e.name}...`);
|
GBLog.info(`Loading sys package: ${e.name}...`);
|
||||||
|
|
||||||
const p = Object.create(e.prototype) as IGBPackage;
|
const p = Object.create(e.prototype) as IGBPackage;
|
||||||
sysPackages.push(p);
|
sysPackages.push(p);
|
||||||
|
|
||||||
await p.loadPackage(core, core.sequelize);
|
await p.loadPackage(core, core.sequelize);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return sysPackages;
|
return sysPackages;
|
||||||
|
|
|
@ -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,12 +416,22 @@ 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');
|
||||||
const dialogState = conversationState.createProperty('dialogState');
|
const dialogState = conversationState.createProperty('dialogState');
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -53,7 +53,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
public whatsappServiceUrl: string;
|
public whatsappServiceUrl: string;
|
||||||
public botId: string;
|
public botId: string;
|
||||||
private directLineSecret: string;
|
private directLineSecret: string;
|
||||||
|
|
||||||
|
|
||||||
public conversationIds = {};
|
public conversationIds = {};
|
||||||
min: GBMinInstance;
|
min: GBMinInstance;
|
||||||
|
@ -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,14 +103,15 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
if (setUrl) {
|
||||||
let res = await request.post(options);
|
try {
|
||||||
GBLog.info(res);
|
let res = await request.post(options);
|
||||||
} catch (error) {
|
GBLog.info(res);
|
||||||
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
} catch (error) {
|
||||||
|
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async asyncForEach(array, callback) {
|
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) {
|
public async received(req, res) {
|
||||||
|
|
||||||
if (req.body.messages === undefined) {
|
if (req.body.messages === undefined) {
|
||||||
|
@ -146,12 +151,12 @@ export class WhatsappDirectLine extends GBService {
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await request(options);
|
const res = await request(options);
|
||||||
let buf = Buffer.from(res, 'binary');
|
let buf = Buffer.from(res, 'binary');
|
||||||
text = await GBConversationalService.getTextFromAudioBuffer(
|
text = await GBConversationalService.getTextFromAudioBuffer(
|
||||||
this.min.instance.speechKey,
|
this.min.instance.speechKey,
|
||||||
this.min.instance.cloudLocation,
|
this.min.instance.cloudLocation,
|
||||||
buf, 'pt-br'
|
buf, 'pt-br'
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue