I'm trying to integrate my lambda function with IBM Tone_chat sentiment analyzer. I'm getting an error of:
"errorMessage": "The first argument must be one of type string or Buffer. Received type undefined"
here is my event:
{ "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"}
]}
If I change the event to:
{"text":"hello, this is test test, Happy sad"}
I get an error of: "{\"code\":400,\"sub_code\":\"C00012\",\"error\":\"Invalid JSON input at line 1, column 2\"}"
Here is my code:
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()
}
Can anyone please help me, I'm new to this, and I'm stuck on this for a while now!
Thank you
I am a little surprised that you are not getting the same error for both inputs, as you are not actually passing in event
, event.text
nor text
in to the http request. How you do it really depends on whether you are using a POST
or a GET
.
If I was to give a recommendation it would be as a POST
where you would add text
as part of the request 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 ifevent.text
is your json object -
let text = JSON.stringify(event.text);
For Usage see the API documentation - https://cloud.ibm.com/apidocs/tone-analyzer#analyze-general-tone