Monday, 17 April 2017

Why Watson-conversation is returning the same default response for any request when I created a post call in node js

I've created a sample training data in conversation service and Now I'm trying to create a post call for that service using node js to create a chat application.I created a post call and it is working but not as expected.It is giving me the default response for any call..

I came to know we need to pass the context value which we gets in the response to the next call to carry on the flow.But not sure of how to do that. can someone help me in that.Below is my code

var express = require('express');
var conversationV1 = require('watson-developer-cloud/conversation/v1');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));
var conversation = new conversationV1({
    username: 'e3f7301b-1a06-4a90-b5a0-e00cf4160eb9',
    password: 'svyD0fngjxam',
    version_date: conversationV1.VERSION_DATE_2016_09_20
});
const updateMessage = (input, response) => {
    var responseText = null;
    if (!response.output) {
        response.output = {};
    } else {
        return response;
    }
    if (response.intents && response.intents[0]) {
        var intent = response.intents[0];
    if (intent.confidence >= 0.75) {
        responseText = 'I understood your intent was ' + intent.intent;
    } else if (intent.confidence >= 0.5) {
        responseText = 'I think your intent was ' + intent.intent;
    } else {
        responseText = 'I did not understand your intent';
    }
}
response.output.text = responseText;
return response;
};

app.post('/api/message', (req, res, next) => {
    const workspace = '254654de-2bfe-423a-92ec-6aa66620625a'; 
    if (!workspace || workspace === '<workspace-id>') {
        return res.json({
            output: {
                text: 'Please check the workspace'
            }
        });
    }
    const payload = {
        workspace_id: workspace,
        input: req.body.input || {},
        context: req.body.context || {}
    };

    // Send the input to the conversation service
    conversation.message(payload, (error, data) => {
        if (error) {
            return next(error);
        }
        return res.json(updateMessage(payload, data));
    });
});

app.listen(process.env.PORT || 3000);

exports.app = app

Can someone help me how we can pass the context to make it work..Can someone help.U can run the code in your local to test.



via user7350714

No comments:

Post a Comment