Watson 对话 - Oracle 数据库集成

Watson Conversation - Oracle DB Integration

很好morning/afternoon,我正在尝试让 Watson return 从我们的 Oracle 数据库手动设置响应。

我正在使用异步顺序访问数据库和 return 响应,因为我遇到的第一个问题是在 Watson 已经 return 响应之后才会发生数据库查询。 Async.waterfall 解决了这个问题。

我当前的问题:在控制台中我看到所有记录都正确。查询数据库,response.output.text 设置正确,然后 returned 到 Watson 但我的聊天中没有出现任何响应。如果我在 response.output.text = "asdfa" 之前设置async,asdfa 已按预期 return 编辑。

我在试图解决这个问题时不知所措,所以我感谢所有帮助。如果需要更多信息,请告诉我。

  // Send the input to the conversation service
conversation.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
return res.json(updateMessage(payload, data));
});
});

function updateMessage(input, response) {
 var responseText = null;
 if (!response.output) {
 response.output = {};
} else {
// checkNames check
if (response.output.nodes_visited[0] === 'slot_11_1519333387192' && response.entities[0].entity === 'confirm') {

  /* This code actually returns asdfa as a response from Watson.. */
  // response.output.text = "asdfa";
  // return response;
  async.waterfall([
      // this function queries the database
      // TODO: module out the Oracle connection parts once POC is completed
      function queryDB(callback) {
        console.log('Starting queryDB');
        var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";
        oracledb.getConnection('hr',
          function (err, connection) {
            var conn = oracleGetConnection(err, connection);
            conn.execute(query, {}, {
                outFormat: oracledb.OBJECT
              },
              function (err, result) {

                console.log('result from Oracle: ', result);
                // pass a null error and the result of the query

                callback(null, result.rows[0]);
              });
          });
      },
      // this function formats the result of the query
      // TODO: this should not be it's own function. This can happen at the same time the db gets the row 
      function formatName (arg1, callback) {
        console.log('this should happen after query..');
        console.log('arg1: ', arg1);

        var r = JSON.stringify(arg1);
        r = r.substring((r.indexOf(':') + 1) + 1, r.length - 2);
        console.log('Name is: ', r);
        // pass a null error and the formatted name
        callback(null, r);
      }
    ],
    // Final function to be ran after the two above have completed 
    function finalFunction (err, result) {
      if (err) {
        console.log('uh oh async err: ', err);
      } else {
        console.log('This is final Function');
        // set output text
        response.output.text = 'Is your name ' + result + '?';
        // response.context.dbResponse = 'Is your name ' + result + '?';
        // response.output.text = "asdfasdfasd";
        // console.log('This is the value of response\n\n', response);
        // var resp = returnResponse(input, response);
        response.context.dbResponse = response.output.text[0];
        return returnResponse(input, response);
        // return response;
      }
    });
    // response.output.text = "asdfa";
    console.log('This is response.output.text ', response.output.text);
    return response;
} else {
  //If no special if case to query the db just run Watson Conversation stock
  return returnResponse(input, response);
}

} }

这是控制台日志示例。

This logs the Input from the user:
name 111111111
This logs the Response from Watson:
Is 111111111correct?
This logs the intent recognized, if any:
nameCheck
This logs the entity recognized, if any:
VSUID
This logs the text that is being returned to the user:  [ 'Is 111111111correct?'
]
Starting queryDB
Connected to database
result from Oracle:  { outBinds: undefined,
  rowsAffected: undefined,
  metaData: [ { name: 'TABLE_FIRST_NAME' } ],
  rows: [ [ 'Tyler' ], [ 'Tyler' ] ],
  resultSet: undefined }
this should happen after query..
arg1:  [ 'Tyler' ]
Name is:  "Tyler
This is final Function
This logs the Input from the user:
yes
This logs the Response from Watson:
Is your name "Tyler?
This logs the entity recognized, if any:
confirm
This logs the text that is being returned to the user:  Is your name "Tyler? 

像下面这样编写代码会使您面临 SQL 注入漏洞(以及可能的性能问题):

var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";

请阅读section of the documentation on bind variables

关于你的问题...

您将 updateMessage 视为同步函数,但它执行的是异步工作。执行异步工作的函数将需要异步 API,例如 Node.js 样式回调、Promises 或 AsyncFunctions (async/await)。

如果您看到您提供的代码的第 73 行,则您是 "returning" 响应对象,但它在 async.waterfall 调用之外。由于 Node.js 的异步性质,即使第 67 行的 return 也无法工作。

这是我最近尝试描述所有这些工作原理的尝试: https://www.youtube.com/watch?v=iAdeljxq_hs

您可以在此处访问幻灯片和示例代码: https://www.dropbox.com/s/quu7oxiug0gh6ua/Understanding%20Async%20Processing%20and%20Patterns%20in%20Node.js.zip?dl=0

在示例代码的 code > header-detail 目录中,您将看到 5 个以 header-detail-with 开头的不同文件- 之后是您可以做出的不同 API 选择的名称。您将不得不对 updateMessage API 做出类似的选择。

为了运行测试,使用ddl.sql文件创建目标表,然后编辑db-config .js 根据您的环境需要,最后 运行 node test.js 1 来自该目录中的终端。您可以将末尾的数字更改为 运行 不同的测试文件。