fix(whatsapp.gblib): Service latency due to res.end missing call.

This commit is contained in:
Rodrigo Rodriguez (pragmatismo.io) 2019-06-28 11:17:41 -03:00
parent 46261d7b1f
commit 82dcfac4e5
3 changed files with 43 additions and 44 deletions

View file

@ -116,10 +116,12 @@ export class GBMinService {
deployer: GBDeployer, deployer: GBDeployer,
proxyAddress: string proxyAddress: string
) { ) {
// Serves default UI on root address '/'.
const uiPackage = 'default.gbui'; const uiPackage = 'default.gbui';
// Serves default UI on root address '/' if web enabled.
if (process.env.DISABLE_WEB !== 'true') {
server.use('/', express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); server.use('/', express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build')));
}
await Promise.all( await Promise.all(
instances.map(async instance => { instances.map(async instance => {
@ -130,11 +132,13 @@ export class GBMinService {
// Serves the bot information object via HTTP so clients can get // Serves the bot information object via HTTP so clients can get
// instance information stored on server. // instance information stored on server.
if (process.env.DISABLE_WEB !== 'true') {
server.get('/instances/:botId', (req, res) => { server.get('/instances/:botId', (req, res) => {
(async () => { (async () => {
await this.sendInstanceToClient(req, bootInstance, res, webchatToken); await this.sendInstanceToClient(req, bootInstance, res, webchatToken);
})(); })();
}); });
}
// Build bot adapter. // Build bot adapter.
@ -158,11 +162,12 @@ export class GBMinService {
// Serves individual URL for each bot user interface. // Serves individual URL for each bot user interface.
if (process.env.DISABLE_WEB !== 'true') {
const uiUrl = `/${instance.botId}`; const uiUrl = `/${instance.botId}`;
server.use(uiUrl, express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build'))); server.use(uiUrl, express.static(urlJoin(GBDeployer.deployFolder, uiPackage, 'build')));
GBLog.info(`Bot UI ${uiPackage} accessible at: ${uiUrl}.`); 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. // 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 // There they will authenticate and give their consent to allow this app access to
// some resource they own. // some resource they own.
@ -359,8 +364,8 @@ export class GBMinService {
e.loadBot(min); e.loadBot(min);
if (index === 6) { // TODO: Remove this magic number and use a map. if (index === 6) { // TODO: Remove this magic number and use a map.
const url = '/instances/:botId/whatsapp'; const url = '/instances/:botId/whatsapp';
server.post(url, (req, res) => { server.post(url, async (req, res) => {
(e as any).channel.received(req, res); await (e as any).channel.received(req, res);
}); });
} }
index++; index++;

View file

@ -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 text = req.body.messages[0].body;
const from = req.body.messages[0].author.split('@')[0]; const from = req.body.messages[0].author.split('@')[0];
const fromName = req.body.messages[0].senderName; const fromName = req.body.messages[0].senderName;
if (req.body.messages[0].fromMe) { if (req.body.messages[0].fromMe) {
res.end();
return; // Exit here. return; // Exit here.
} }
@ -122,37 +129,26 @@ export class WhatsappDirectLine extends GBService {
const conversationId = this.conversationIds[from]; const conversationId = this.conversationIds[from];
this.directLineClient.then(client => { let client = await this.directLineClient;
if (this.conversationIds[from] === undefined) { if (this.conversationIds[from] === undefined) {
GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`); GBLog.info(`GBWhatsapp: Starting new conversation on Bot.`);
client.Conversations.Conversations_StartConversation() const response = await client.Conversations.Conversations_StartConversation()
.then(response => { const generatedConversationId = response.obj.conversationId;
return response.obj.conversationId;
}).catch(err => {
GBLog.error(`Error calling Conversations_StartConversation on Whatsapp channel ${err.data}`);
})
.then(generatedConversationId => {
this.conversationIds[from] = generatedConversationId; this.conversationIds[from] = generatedConversationId;
this.inputMessage(client, generatedConversationId, text, from, fromName);
this.pollMessages(client, generatedConversationId, from, fromName); this.pollMessages(client, generatedConversationId, from, fromName);
}) this.inputMessage(client, generatedConversationId, text, from, fromName);
.catch(err => {
GBLog.error(`Error starting conversation ${err.data}`);
});
} else { } else {
this.inputMessage(client, conversationId, text, from, fromName); this.inputMessage(client, conversationId, text, from, fromName);
} }
res.end(); res.end();
}).catch(err => {
GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err.data}`);
});
} }
public inputMessage(client, conversationId, text, from, fromName) { public inputMessage(client, conversationId, text, from, fromName) {
client.Conversations.Conversations_PostActivity({ return client.Conversations.Conversations_PostActivity({
conversationId: conversationId, conversationId: conversationId,
activity: { activity: {
textFormat: 'plain', textFormat: 'plain',
@ -164,8 +160,6 @@ export class WhatsappDirectLine extends GBService {
}, },
replyToId: from replyToId: from
} }
}).catch(err => {
GBLog.error(`GBWhatsapp: Error receiving message: ${err.data}.`);
}); });
} }

View file

@ -74,6 +74,7 @@ export class GBServer {
public static run() { public static run() {
GBLog.info(`The Bot Server is in STARTING mode...`); GBLog.info(`The Bot Server is in STARTING mode...`);
GBServer.globals = new RootData(); GBServer.globals = new RootData();
GBConfigService.init();
const port = GBConfigService.getServerPort(); const port = GBConfigService.getServerPort();
const server = express(); const server = express();
GBServer.globals.server = server; GBServer.globals.server = server;
@ -87,7 +88,6 @@ export class GBServer {
// Reads basic configuration, initialize minimal services. // Reads basic configuration, initialize minimal services.
GBConfigService.init();
const core: IGBCoreService = new GBCoreService(); const core: IGBCoreService = new GBCoreService();
const importer: GBImporter = new GBImporter(core); const importer: GBImporter = new GBImporter(core);
@ -168,7 +168,7 @@ export class GBServer {
// Opens Navigator. // Opens Navigator.
core.openBrowserInDevelopment(); // TODO: Config: core.openBrowserInDevelopment();
} catch (err) { } catch (err) {
GBLog.error(`STOP: ${err} ${err.stack ? err.stack : ''}`); GBLog.error(`STOP: ${err} ${err.stack ? err.stack : ''}`);
process.exit(1); process.exit(1);