Watson Conversation - Oracle数据库集成

时间:2018-03-27 15:33:24

标签: javascript node.js api watson-conversation watson

早上好/下午好,我正在努力让Watson从我们的Oracle数据库中手动设置回复。

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

我当前的问题:在控制台中,我看到所有内容都已正确记录。查询数据库,正确设置response.output.text然后返回Watson但我的聊天中没有响应..如果我在异步之前设置了response.output.text =“asdfa”,则asdfa按预期返回

我无法想象这一点,所以我感谢任何和所有的帮助。如果需要更多信息,请告诉我。

  // 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? 

1 个答案:

答案 0 :(得分:1)

编写如下代码会打开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行,则表示您已退回"响应对象,但不在async.waterfall调用之外。由于Node.js的异步特性,即使第67行的返回也不会起作用。

这是我最近尝试描述所有这些工作的方法: 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

在示例代码中代码> header-detail 目录,您会看到5个不同的文件,这些文件以 header-detail-with - 开头,但您可以选择其他API选项的名称。您必须使用updateMessage API进行类似的选择。

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