一个支持数千个Facebook页面的机器人

时间:2016-10-31 23:46:29

标签: node.js botframework

我喜欢僵尸框架,但我希望扩展以支持数百个(如果不是数千个)Facebook页面都指向我的单个机器人实例。我的bot实例通过传入的页面ID区分功能,或者我想通过MSFT App / Secret ID。

该框架似乎要求MSFT托管的逻辑机器人与FB页面之间存在1:1的对应关系,但我的单个机器人实例可以处理数千个此类页面和应用程序。

看起来我可能需要为每个逻辑机器人页面创建一个唯一的ChatConnector和关联的UniversalBot实例。在我建议的范围内,这是非常低效的。

解决这个问题的一种方法可能是扩展UniversalBot以接受我创建的所有MSFT App和Secret ID的列表,但我还没有尝试过。在查看API之后,看起来可以使用单个UniversalBot实例注册更多连接器。

UniversalBot:

/** 
 * Registers or returns a connector for a specific channel. 
 * @param channelId Unique ID of the channel. Use a channelId of '*' to reference the default connector.
 * @param connector (Optional) connector to register. If ommited the connector for __channelId__ will be returned. 
 */    
connector(channelId: string, connector?: IConnector): IConnector;

但不确定我为channelId传递了什么,除非它是一个任意唯一的本地值。

我在这里查看了其他/类似的帖子,但没有找到我认为解决我的问题的具体内容。如果我弄错了,我道歉并希望得到参考。

我希望有人可能会有更好的主意。我正在使用Node btw。感谢。

1 个答案:

答案 0 :(得分:1)

从这里采取:

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

我们正在创建不同的bot和连接器实例,这些实例捕获每个客户的App ID和密码,并将其绑定到Bot Framework用作Messaging Endpoint的相应REST API。

当我们创建bot实例时,我们调用bindDialogsToBot方法,传递bot实例和客户ID。通过这样做,我们在其闭包中捕获客户ID,使其可以访问内部对话框。

当调用其中一个REST API时,使用相关的bot实例,对话框的内部逻辑可以使用正确的客户ID来处理请求(例如,检索客户的配置/规则)并采取行动)。