【问题标题】:One bot to support thousands of Facebook pages一个机器人支持数千个 Facebook 页面
【发布时间】:2017-02-06 22:03:59
【问题描述】:

我喜欢机器人框架,但我想扩展以支持数百个(如果不是数千个)都指向我的单个机器人实例的 Facebook 页面。我的机器人实例通过传入的页面 ID 来区分功能,或者我猜是通过 MSFT 应用程序/秘密 ID。

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

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

解决此问题的一种方法可能是扩展 UniversalBot 以接受我创建的所有 MSFT 应用程序和秘密 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 传递了什么,除非那是任意唯一的本地值。

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

我希望有人可能有更好的主意。我正在使用节点顺便说一句。谢谢。

【问题讨论】:

  • 即使这是可能的,如果您的机器人服务于数千个页面,我也会担心 Microsoft 方面可能会限制速率。
  • @K48 这是一个很好的观点,我没有考虑过。让我想知道 Bot Framework 是否是一个可行的长期解决方案。值得研究。谢谢。
  • 所以,只是为了了解更多:你有一个代码,它依赖于 facebook 页面 id 来做一些事情。然后,您在 Bot Framework 中注册了多个机器人(这是获得多个 MS App/Secret 的唯一方法)。他们都指向同一个网址吗?而且您不想拆分机器人,因为...您可能不想管理多个部署?
  • @EzequielJadib 我们的页面基本上都在逻辑上引用了同一个机器人。我想要一个机器人实例来服务所有这些。与其说是“垃圾邮件”,不如说是在给定不同页面 ID 的情况下功能略有不同。我相信 Bot Framework 强制使用单独的机器人实例。对于没有我们要求的大量机器人来说,这是有意义的。事实上,当我在早期的项目中使用 BotKit 时,我的 bot 能够在没有体操的情况下为多个页面以及多个聊天平台提供服务。除了一些细微的差异之外,该机器人的行为相同。

标签: node.js botframework


【解决方案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}`);
});

我们正在创建不同的机器人和连接器实例,用于捕获每个客户的应用 ID 和密码,并将其绑定到相应的 REST API,机器人框架将其用作消息传递端点。

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

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

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2018-11-30
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多