IBM Watson Assistant:如何检测对话来自 Slack 或基于 Web 的集成

问题描述

用户通过基于 Web 的界面进入时,我希望能够返回 HTML 格式的文本。这很好用,但是如果您与 Slack 集成,您只会将纯 HTML 转储给用户。我使用的是开箱即​​用的,Watson Assistant 提供了 Web 和 slack 集成。有没有办法检测哪个集成正在推动对话?

解决方法

对于这个问题,我有两个解决方案。两个都试过了。

选项 1: 第一个是使用 JSON 编辑器指定格式,感谢 data_henrik 的响应 https://cloud.ibm.com/docs/assistant?topic=assistant-dialog-responses-json#dialog-responses-json-target-integrations。使用此解决方案时要记住两件事:

  1. 当您执行“Try Me”时,您会看到多个回复。您发回的每种格式对应一个。
  2. 您需要让 webhook 为您定位的每个渠道传回多种格式。
  3. 文档中提供的格式已过时。这是我想出的要放入 JSON 编辑器的格式。

JSON 编辑器

{
    "output": {
        "generic": [{
                "response_type": "text","channels": [{
                    "channel": "chat"
                }],"values": [{
                    "text": "$webhook_result_2.chat"
                }],"selection_policy": "sequential"
            },{
                "response_type": "text","channels": [{
                    "channel": "slack"
                }],"values": [{
                    "text": "$webhook_result_2.slack"
                }],"selection_policy": "sequential"
            }
        ]
    }
}

选项 2: 第二个选项是通过聊天客户端设置一个用户定义的变量,在 webhook 调用中传递。然后代码将使用它来知道要应用什么格式。不那么优雅,但使用较少的网络带宽和处理来构建响应。

更新了 Watson 提供的 Web 客户端脚本:

  <script>
    function preSendhandler(event) {
      event.data.context.skills['main skill'].user_defined.client = "html";
    }

    window.watsonAssistantChatOptions = {
      integrationID: "...",// The ID of this integration.
      region: "...",// The region your integration is hosted in.
      serviceInstanceID: "...",// The ID of your service instance.
      onLoad: function (instance) {
        // Subscribe to the "pre:send" event.
        instance.on({
          type: "pre:send",handler: preSendhandler
        });
        instance.render();
      }
    };
    setTimeout(function () {
      const t = document.createElement('script');
      t.src = "https://web-chat.global.assistant.watson.appdomain.cloud/loadWatsonAssistantChat.js";
      document.head.appendChild(t);
    });
  </script>

您还需要通过将参数设置为“client”的网络钩子和值“$client”来将该用户定义的变量传递到网络钩子中。

,

让我指出 Watson Assistant 提供了多种 how to compose a response 方式来回答。有简单的响应和更复杂(“丰富”)的响应以及图像、选项、暂停等等。此外,您可以define the responses in the JSON editor

后者允许create native JSON responses and to address specific output channels。因此,您可以定义特定于 Slack 的响应,甚至可以使用 Slack 提供的高级格式选项,而不仅仅是纯文本。

总而言之,您不需要检测输入来自哪里,而是需要为您使用的输出通道设计输出。不同的观点,但结果相同甚至更好...... :)