How to get context variables from the response of IBM watson assistant V2 using Nodejs?

We are using IBM watson assistant for NLU and backend we are using Nodejs for API calling. I am able to connect and get JSON response from watson assistant. For that I used following package 'watson-developer-cloud/assistant/v2'.

I am handling context/slots within watson so I just want to get the values of those context variables/slots in the JSON response.

I tried to send the context variables in text response (JSON file) from watson.

Also tried to get context with watson assistant V1 but there the problem is session handling so I preferred to go with watson assistant V2

const AssistantV2 = require('watson-developer-cloud/assistant/v2');
const assistant = new AssistantV2({
  version: 'XXXX-XX-XX',
  iam_apikey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  url: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
});

var sess_id;

assistant.createSession({
    assistant_id: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
})
  .then(res => {
    sess_id=res.session_id;
})
  .catch(err => {
    console.log(err);
});

assistant.message({
    assistant_id: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
    session_id: sess_id,
    input: {
        'message_type': 'text',
        'text': 'Message'},
    })
    .then(res => {
        console.log(JSON.stringify(res, null, 2))
        console.log(res)})

Expected: I should get context in watson Json response

Actual: I am not getting context in watson Json response

Upvotes: 1

Views: 2193

Answers (2)

Vidyasagar Machupalli
Vidyasagar Machupalli

Reputation: 2865

As @data_henrik rightly mentioned, you need to pass return_context as part of your request to see MessageContext in the response.

Here's a sample request with context returned from the previous response being passed

const AssistantV2 = require('watson-developer-cloud/assistant/v2');

const service = new AssistantV2({
  iam_apikey: '{apikey}',
  version: '2019-02-28',
  url: '{url}'
});

service.message({
  assistant_id: '{assistant_id}',
  session_id: '{session_id}',
  input: {
    'message_type': 'text',
    'text': 'Hello',
    'options': {
      'return_context': true
    }
  },
  context: {
    'global': {
      'system': {
        'user_id': 'my_user_id'
      }
    },
    'skills': {
      'main skill': {
        'user_defined': {
          'account_number': '123456'
        }
      }
    }
  }
})
  .then(res => {
    console.log(JSON.stringify(res, null, 2));
  })
  .catch(err => {
    console.log(err);
  });

Under input, you can see the return_context set to true

Upvotes: 1

data_henrik
data_henrik

Reputation: 17176

See the V2 API documentation for IBM Watson Assistant and line 473 in the Node.js SDK:

There is an input parameter return_context. It is false by default. If set to true it tells the server to return the context data with the message response.

  /** Whether to return session context with the response. If you specify `true`, the response will include the `context` property. */
    return_context?: boolean;

Upvotes: 1

Related Questions