了解 Activity 的名称

Understanding the Name of an Activity

我有一个试图模仿反向通道示例的聊天机器人。

大部分都是正确的,除了我无法理解下面的代码应该如何工作。

  botConnection.activity$
    .filter(function (activity) {
      console.log("show me the activity name passed" + activity.name);
      return activity.type === 'event' && activity.name === 'changeBackground';
    })
    .subscribe(function (activity) {
      changeBackgroundColor(activity.value);
    });

我假设我可能需要在我的机器人中命名一些 changeBackground?另外 activity.type 不应该是 'message' 而不是事件吗?

当我执行 console.log 向我显示 activity.name 时,我传递的每条消息都是 "undefined"。我究竟应该在哪里设置 Activity 的名称,以便我可以触发更改背景?

activity 是一种通用信封,其中 message(在用户和机器人之间发送的文本 and/or 附件)是最常见的类型。如果您希望让您的客户端和机器人在用户不可见的情况下交换信息,那么您可以使用 event 类型。按照惯例,它们将被客户端忽略,例如 Web Chat.

按照它的编写方式,您的示例代码记录了从机器人发送到客户端的每个activity。 name 字段适用于 event 类型的活动,但不适用于 message,这就是它显示为 undefined.

的原因

在此示例中,changeBackground 是客户端函数的名称,每当机器人发送类型为 event 的 activity 时,该函数就会执行某些操作。

您现在缺少的部分是实际发送该事件的(服务器)机器人代码。有关此示例,请参阅 Ryan Volum 的 backchannel sample bot