Taken from here:
Create a single bot service to support multiple bot applications
var express = require('express'); var builder = require('botbuilder'); var port = process.env.PORT || 3978; var app = express(); // a list of client ids, with their corresponding // appids and passwords from the bot developer portal. // get this from the configuration, a remote API, etc. var customersBots = [ { cid: 'cid1', appid: '', passwd: '' }, { cid: 'cid2', appid: '', passwd: '' }, { cid: 'cid3', appid: '', passwd: '' }, ]; // expose a designated Messaging Endpoint for each of the customers customersBots.forEach(cust => { // create a connector and bot instances for // this customer using its appId and password var connector = new builder.ChatConnector({ appId: cust.appid, appPassword: cust.passwd }); var bot = new builder.UniversalBot(connector); // bing bot dialogs for each customer bot instance bindDialogsToBot(bot, cust.cid); // bind connector for each customer on it dedicated Messaging Endpoint. // bot framework entry should use the customer id as part of the // endpoint url to map to the right bot instance app.post(`/api/${cust.cid}/messages`, connector.listen()); }); // this is where you implement all of your dialogs // and add them on the bot instance function bindDialogsToBot (bot, cid) { bot.dialog('/', [ session => { session.send(`Hello... I'm a bot for customer id: '${cid}'`); } ]); } // start listening for incoming requests app.listen(port, () => { console.log(`listening on port ${port}`); });
We create different instances of the bot and connector that capture the identifier and password for each client and bind it to the corresponding REST API, which is used by the Bot platform as the messaging endpoint.
When we create the bot instance, we call the bindDialogsToBot method, passing the bot instance and client ID. By doing this, we fix the client identifier in its closure, making it available for internal dialogs.
When calling one of the REST-API, the corresponding bot instance is used, and the correct client identifier can be used by the internal logic of the dialog boxes to process the request (for example, to obtain the clientโs configuration / rules and act on them).
Mor shemesh
source share