一个支持数千个Facebook页面的机器人 [英] One bot to support thousands of Facebook pages

查看:87
本文介绍了一个支持数千个Facebook页面的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I love the bot framework, but I want to scale to support hundreds if not thousands of Facebook pages all pointing to my single bot instance. My bot instance differentiates functionality by the incoming page id, or I guess by the MSFT App/Secret IDs.

该框架似乎要求MSFT托管的逻辑漫游器与FB页面之间具有1:1的对应关系,但是我的单个漫游器实例可以处理数千个此类页面和应用.

The framework appears to require a 1:1 correspondence between logical bot hosted by MSFT and a FB page, but my single bot instance can handle thousands of such pages and apps.

似乎我可能需要为每个逻辑bot页面创建一个唯一的ChatConnector和关联的UniversalBot实例.在我建议的规模上,这效率极低.

It looks like I might need to create a unique ChatConnector and associated UniversalBot instance for every logical bot-page. This is horribly inefficient at the scale I'm suggesting.

解决此问题的一种方法可能是扩展UniversalBot以接受我创建的所有MSFT应用程序和秘密ID的列表,但是我还没有尝试过.复习API之后,似乎可以在单个UniversalBot实例中注册更多连接器.

One way to solve this might be to extend UniversalBot to accept a list of all MSFT App and Secret IDs that I create, but I haven't tried this yet. After reviewing the API, looks like it might be possible to register more connectors with a single UniversalBot instance.

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传递什么,除非那是一个任意的唯一本地值.

But not sure what I pass for channelId unless that's an arbitrary unique local value.

我在这里查看了其他/类似的帖子,但没有发现我认为可以解决我的问题的任何内容.如果我弄错了,我深表歉意,希望能为您提供参考.

I have reviewed other/similar posts here, but not found specifically anything that I believe addresses my issue. If I'm mistaken I apologize and would appreciate a reference.

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

I am hoping someone might have a better idea. I am using Node btw. Thanks.

推荐答案

来自这里:

创建单个Bot服务以支持多个Bot应用程序

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和密码,并将其绑定到Bot Framework用作消息传递端点的相应REST API.

We are creating different bot and connector instances that capture the App ID and password for each customer, and binding it to the corresponding REST API that is used by the Bot Framework as the Messaging Endpoint.

创建bot实例时,我们调用bindDialogsToBot方法,并传递bot实例和客户ID.通过这样做,我们捕获了客户ID的闭合名,从而使内部对话框可以访问它.

When we create the bot instance, we call the bindDialogsToBot method, passing the bot instance and the customer ID. By doing that, we capture the customer ID in its closure making it accessible to the internal dialogs.

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

When a call is made to one of the REST APIs, the relevant bot instance is used, and the correct customer ID can be utilized by the dialog’s internal logic to process the request (for example, to retrieve a customer’s configuration/rules and act upon them).

这篇关于一个支持数千个Facebook页面的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆