6

I am reading the official documentation from this link: https://cloud.google.com/dialogflow-enterprise/docs/reference/rest/v2beta1/QueryParameters but I am unable to pass a context parameter into my request using the following code:

  var query = req.body.query;
    const request = {
      session: sessionPath,
      queryInput: {
        text: {
          text: query,
          languageCode: 'en-US',
        },
      },
      queryParameters: {
        contexts: ['Question-followup']
      },
    };

    // Send request and log result
    sessionClient
      .detectIntent(request)
      .then(responses => {
        const result = responses[0].queryResult;
        console.log(result);
        res.json(result);   
        if (result.intent) {
          console.log(`  Intent: ${result.intent.displayName}`);
        } else {
          console.log(`  No intent matchede.`);
        }
      })
      .catch(err => {
        console.error('ERROR:', err);
      });

In the documentation it says that I should have something like:

"contexts": [
    {
      object(Context)
    }
  ],

The reason I want this is that sometimes DialogFlow is not able to detect the Intent so I would think that by passing the context into the parameter would help dialogflow to find the correct intent!

Mizlul
  • 1
  • 7
  • 41
  • 100

1 Answers1

6

The contexts array needs to be an array of Context objects, not just a string with the context names.

The context object looks something like

{
  "name": "projects/<Project ID>/agent/sessions/<Session ID>/contexts/<Context Name>",
  "lifespanCount": 1,
  "parameters": {
    "anyParameterName": "parameterValue"
  }
}
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • I no longer get the error, which seem to fix, but one thing I dont get is, why does not detect the right intent when I give some input, what I mean I would like to force the user whatever is writing redirect to a specific intent, is this possible? I thought by passing the context name would do the work, but it doesnt! instead it is detecting the intent by input text – Mizlul Jun 06 '18 at 11:22
  • I will accept this answer, as it fixes the problem I had with giving the right parameters, but still I would like to know how do I redirect user input to a specific Intent regardless their input. – Mizlul Jun 06 '18 at 11:31
  • I'd suggest asking this as a new question directly - it isn't clear what you're trying to do and why it isn't working, so some examples (screen shots of the Intents and results, etc) would help. – Prisoner Jun 06 '18 at 14:37
  • Thanks, I am using dialog flow CX what should be the context ? – Aymal Jul 17 '22 at 09:28
  • Dialogflow CX handles this completely differently. You should post a new question with as much detail as possible about the issue you're having and tag it `dialogflow-cx` – Prisoner Jul 17 '22 at 10:26