Skip to content

Commit

Permalink
fix(whatsapp.gblib): Service latency due to res.end missing call.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Rodriguez (pragmatismo.io) committed Jun 28, 2019
1 parent 46261d7 commit 82dcfac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
33 changes: 19 additions & 14 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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++;
Expand Down
54 changes: 24 additions & 30 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,50 +109,46 @@ 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.
}

GBLog.info(`GBWhatsapp: RCV ${from}(${fromName}): ${text})`);

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}`);
})

.then(generatedConversationId => {
this.conversationIds[from] = generatedConversationId;
this.inputMessage(client, generatedConversationId, text, from, fromName);

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}`);
});
let client = await this.directLineClient;

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.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',
Expand All @@ -164,8 +160,6 @@ export class WhatsappDirectLine extends GBService {
},
replyToId: from
}
}).catch(err => {
GBLog.error(`GBWhatsapp: Error receiving message: ${err.data}.`);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 82dcfac

Please sign in to comment.