fix(whatsapp.gblib): Service latency due to res.end missing call.
This commit is contained in:
parent
46261d7b1f
commit
82dcfac4e5
3 changed files with 43 additions and 44 deletions
|
@ -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++;
|
||||
|
|
|
@ -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}.`);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue