如何从对话外部接收 watson 中的用户名 Node.js

How receive username in watson from outside the dialog Node.js

我正在使用 IBM 的 webApplication 和 Node.js 创建一个聊天机器人。

聊天机器人将迁移到需要登录的 Intranet 中的 "blog"。所以我想在聊天开始之前将用户名作为上下文传递给 Watson,从网站上获取它。

通过代码在 json 上输入新上下文。

  "context": {
    "conversation_id": "8a433bbf-8e27-42ba-bdac-9341f5b16fcf",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "node_1_1528906594423": [
          0
        ],
        "node_14_1527085355836": [
          0,
          0
        ]
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    },
    "timezone": "America/Sao_Paulo",
    "nome_bot": "Malu"
    "username": variable
  }

所以最后,对话开始为:

用户登录为 John。

沃森:"Hello, $username." / "Hello, John."

破案了! 'public/script.js' 是代码调用 .json 的地方,所以我们需要在那里进行更改!

// Calling server and get the watson output
const getWatsonMessageAndInsertTemplate = async (text = '') => {
  const uri = 'https://localhost/conversation/';
  const response = await (await fetch(uri, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      text,
      context,
    }),
  })).json();

因此,如果我们将从数据库中获取用户名的变量添加到上下文中,并将其放在上下文中 'username',结果如预期!

...
context: { 'username': variable },
...