r3plica
r3plica

Reputation: 13367

dialogflow with node.js how do you swtich intents

I have an intent that has a required parameter and a fulfilment. When you answer it takes you to my node.js application which currently looks like this:

app.intent(QUESTIONS_INTENT, (conv, params) => {
  let data = conv.data;

  let categoryId = data.categoryId;
  let formulas = data.formulas;
  let options = {
    'method': 'PUT',
    'url': apiUrl + 'questions/filter',
    'body': {
      categoryId: categoryId,
      organisationId: organisation,
      formulas: formulas
    },
    'json': true
  };


  return request(options).then(response => {

    // We need to change how we get these
    var questionText = params.questions;
    var questions = response.filter(item => item.text === questionText);

    data.questions = questions;
    conv.ask(questions[0].text);
    conv.contexts.set('colour');
  }, error => {
    conv.ask(JSON.stringify(error));
  });
})

Currently it gets the questionText and finds the question that matches what you said. What I want it to do is to swap to a new intent. I have tried by conv.contexts.set('colour') but that doesn't appear to work.

I have an intent setup with input context as "Colour" so I would expect that when my fulfilment completes, it should swap to that intent, but it doesn't.

Can someone help me with this?

Upvotes: 0

Views: 307

Answers (1)

James Ives
James Ives

Reputation: 3355

You need to make sure that you have a colour intent setup in DialogFlow that will handle the input from the user and respond to them. The question should be asked after you set the context.

return request(options).then(response => {

    // We need to change how we get these
    var questionText = params.questions;
    var questions = response.filter(item => item.text === questionText);

    data.questions = questions;

    // Sets the context to colour.
    conv.contexts.set('colour', 1);

    // Now that the context is in control you'll be able to ask a question.
    conv.ask(questions[0].text);

You'll also want to provide a lifespan after the context arg. This will make the context active for that many prompts, in the example above I set it for one so your question context will only be active for that specific response.

conv.contexts.set('colour', 1);

Upvotes: 1

Related Questions