将 lambda 与 IBM tone_chat 集成错误

Integrating lambda with IBM tone_chat error

我正在尝试将我的 lambda 函数与 IBM Tone_chat 情绪分析器集成。 我收到以下错误:

"errorMessage": "The first argument must be one of type string or Buffer. Received type undefined"

这是我的活动:

{ "utterances": [
        {"text": "Hello, can you help me", "user": "customer"},
        {"text": "How are you ?", "user": "agent"},
        {"text": "Nothing is working", "user": "customer"},
        {"text": "Sorry to hear this", "user": "agent"}
    ]}

如果我将事件更改为:

{"text":"hello, this is test test, Happy sad"}

我得到一个错误:“{\"code\":400,\"sub_code\":\"C00012\",\"error\":\"Invalid JSON input at line 1, column 2\"} “

这是我的代码:


            const AWS = require('aws-sdk');
            var http = require('https');
            exports.handler = (event, context, callback) => {
                    var text = event.text;
                    var options = {
                           method: process.env.method,
                           hostname: process.env.watson_hostname,
                           port: null,
                           path: process.env.path,
                           headers: {
                               'content-type': process.env.content_type,
                                authorization: process.env.authorization,
                               'cache-control': process.env.cache_control,
                               'X-Watson-Learning-Opt-Out': 'true'
                           }
                     };
                       var req = http.request(options, function (res) {
                       var chunks = [];
                       res.on("data", function (chunk) {
                       chunks.push(chunk);
                          });
                    res.on("end", function () { 
                           var sentimentResponse = JSON.parse(Buffer.concat(chunks));
                       console.log("Sent respose");
                        callback(null, JSON.stringify(sentimentResponse));
                   });
                   })
      req.write(text);
      req.end()
        }

任何人都可以帮助我,我是新手,现在我坚持了一段时间!

谢谢

我有点惊讶你在两个输入中没有得到相同的错误,因为你实际上没有将 eventevent.texttext 传递到 http要求。具体如何操作取决于您使用的是 POST 还是 GET

如果我要提出建议,那将是 POST,您可以在其中添加 text 作为请求 body 的一部分。

Amendment to answer : After your update it looks like you are using a POST. in which case you need to stringify the json. So if event.text is your json object -

let text = JSON.stringify(event.text);

有关用法,请参阅 API 文档 - https://cloud.ibm.com/apidocs/tone-analyzer#analyze-general-tone