首页 > 解决方案 > 根据 Watson Assistant 上下文自动加载网页

问题描述

我正在尝试让应用程序根据 Watson Assistant 对话中的上下文来编排特定的网页。

我使用的文件是对 github ( https://github.com/watson-developer-cloud/assistant-intermediate ) 上的 Watson Assistant(中间)示例文件的编辑。

这是我为使其工作而编辑的代码片段(在 api.js 中):

var http = new XMLHttpRequest();
http.open('POST', messageEndpoint, true);
http.setRequestHeader('Content-type', 'application/json');
http.onreadystatechange = function() {
  if (http.readyState === 4 && http.status === 200 && http.responseText) {
    Api.setResponsePayload(http.responseText);

var json = JSON.parse(http.responseText);
        context = json.context;

          if(json.context.action === 'SocialMedia') {

          document.getElementById("payload-column").setAttribute("style", "background-color: #ffffff;") //renders background white as without this line the webpage comes back with black hidden text for some reason
          document.getElementById("payload-column").setAttribute("src", "https://www.csc2.ncsu.edu/faculty/healey/tweet_viz/tweet_app/?q=" + json.context.AnalysisSearchItem);

          } 

Watson Assistant 工作区中的相应配置是:

{
  "context": {
    "action": "SocialMedia"
  },
  "output": {
    "text": {
      "values": [
        "Loading sentiment analysis from the Computer Science department of North Carolina State University..."
      ],
      "selection_policy": "sequential"
    }
  }
}

当正常的聊天机器人对话正常工作时,我已连接到 Watson Assistant 工作区,并且

document.getElementById("payload-column").setAttribute("src", "https://www.csc2.ncsu.edu/faculty/healey/tweet_viz/tweet_app/?q=" + json.context.AnalysisSearchItem);

行本身确实将 iframe 更改为我想要的网站(我在 index.html 文件中定义了一个 iframe),它只是 IF 条件

if(json.context.action === 'SocialMedia') 

当上下文属于 SocialMedia 时,不会成功执行。

如果有人能帮助解决这个问题,将不胜感激......!

标签: javascriptibm-watsonwatson-conversation

解决方案


推荐阅读