From 82dcfac4e5c514f059c650264d12cf3d9d3db5bc Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (pragmatismo.io)" Date: Fri, 28 Jun 2019 11:17:41 -0300 Subject: [PATCH] fix(whatsapp.gblib): Service latency due to res.end missing call. --- packages/core.gbapp/services/GBMinService.ts | 33 ++++++------ .../services/WhatsappDirectLine.ts | 50 ++++++++----------- src/app.ts | 4 +- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index dd085c47..b7f8ec22 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -116,10 +116,12 @@ export class GBMinService { deployer: GBDeployer, proxyAddress: string ) { - // Serves default UI on root address '/'. - const uiPackage = 'default.gbui'; - server.use('/', express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); + + // Serves default UI on root address '/' if web enabled. + if (process.env.DISABLE_WEB !== 'true') { + server.use('/', express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); + } await Promise.all( instances.map(async instance => { @@ -130,11 +132,13 @@ export class GBMinService { // Serves the bot information object via HTTP so clients can get // instance information stored on server. - server.get('/instances/:botId', (req, res) => { - (async () => { - await this.sendInstanceToClient(req, bootInstance, res, webchatToken); - })(); - }); + if (process.env.DISABLE_WEB !== 'true') { + server.get('/instances/:botId', (req, res) => { + (async () => { + await this.sendInstanceToClient(req, bootInstance, res, webchatToken); + })(); + }); + } // Build bot adapter. @@ -158,11 +162,12 @@ export class GBMinService { // Serves individual URL for each bot user interface. - const uiUrl = `/${instance.botId}`; - server.use(uiUrl, express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); - - GBLog.info(`Bot UI ${uiPackage} accessible at: ${uiUrl}.`); + if (process.env.DISABLE_WEB !== 'true') { + const uiUrl = `/${instance.botId}`; + server.use(uiUrl, express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); + GBLog.info(`Bot UI ${uiPackage} accessible at: ${uiUrl}.`); + } // Clients get redirected here in order to create an OAuth authorize url and redirect them to AAD. // There they will authenticate and give their consent to allow this app access to // some resource they own. @@ -359,8 +364,8 @@ export class GBMinService { e.loadBot(min); if (index === 6) { // TODO: Remove this magic number and use a map. const url = '/instances/:botId/whatsapp'; - server.post(url, (req, res) => { - (e as any).channel.received(req, res); + server.post(url, async (req, res) => { + await (e as any).channel.received(req, res); }); } index++; diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index f070977f..38215209 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -109,12 +109,19 @@ export class WhatsappDirectLine extends GBService { } } - public received(req, res) { + public async received(req, res) { + + if (req.body.messages === undefined) { + res.end(); + return; // Exit here. + } + const text = req.body.messages[0].body; const from = req.body.messages[0].author.split('@')[0]; const fromName = req.body.messages[0].senderName; if (req.body.messages[0].fromMe) { + res.end(); return; // Exit here. } @@ -122,37 +129,26 @@ export class WhatsappDirectLine extends GBService { const conversationId = this.conversationIds[from]; - this.directLineClient.then(client => { - if (this.conversationIds[from] === undefined) { - GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`); - client.Conversations.Conversations_StartConversation() - .then(response => { - return response.obj.conversationId; - }).catch(err => { - GBLog.error(`Error calling Conversations_StartConversation on Whatsapp channel ${err.data}`); - }) + let client = await this.directLineClient; - .then(generatedConversationId => { - this.conversationIds[from] = generatedConversationId; - this.inputMessage(client, generatedConversationId, text, from, fromName); + if (this.conversationIds[from] === undefined) { + GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`); + const response = await client.Conversations.Conversations_StartConversation() + const generatedConversationId = response.obj.conversationId; - this.pollMessages(client, generatedConversationId, from, fromName); - }) - .catch(err => { - GBLog.error(`Error starting conversation ${err.data}`); - }); - } else { - this.inputMessage(client, conversationId, text, from, fromName); - } - res.end(); - }).catch(err => { - GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err.data}`); - }); + this.conversationIds[from] = generatedConversationId; + + this.pollMessages(client, generatedConversationId, from, fromName); + this.inputMessage(client, generatedConversationId, text, from, fromName); + } else { + this.inputMessage(client, conversationId, text, from, fromName); + } + res.end(); } public inputMessage(client, conversationId, text, from, fromName) { - client.Conversations.Conversations_PostActivity({ + return client.Conversations.Conversations_PostActivity({ conversationId: conversationId, activity: { textFormat: 'plain', @@ -164,8 +160,6 @@ export class WhatsappDirectLine extends GBService { }, replyToId: from } - }).catch(err => { - GBLog.error(`GBWhatsapp: Error receiving message: ${err.data}.`); }); } diff --git a/src/app.ts b/src/app.ts index e5f5df1a..3a602926 100644 --- a/src/app.ts +++ b/src/app.ts @@ -74,6 +74,7 @@ export class GBServer { public static run() { GBLog.info(`The Bot Server is in STARTING mode...`); GBServer.globals = new RootData(); + GBConfigService.init(); const port = GBConfigService.getServerPort(); const server = express(); GBServer.globals.server = server; @@ -87,7 +88,6 @@ export class GBServer { // Reads basic configuration, initialize minimal services. - GBConfigService.init(); const core: IGBCoreService = new GBCoreService(); const importer: GBImporter = new GBImporter(core); @@ -168,7 +168,7 @@ export class GBServer { // Opens Navigator. - core.openBrowserInDevelopment(); + // TODO: Config: core.openBrowserInDevelopment(); } catch (err) { GBLog.error(`STOP: ${err} ${err.stack ? err.stack : ''}`); process.exit(1);