Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.setContext() in dialogflow v2 api?

What is the equivalent of dialogflow's app.setContext() from v1 in the v2 API? Given the setup that the migration guide outlines (below), what call would you make to--for example--set a context when the welcome intent is triggered in the demo code below?

// v2
const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');

const app = dialogflow();

app.intent('Default Welcome Intent', conv => {
  conv.ask('How are you?');
});

exports.factsAboutGoogle = functions.https.onRequest(app);
like image 290
hmisenar Avatar asked Jan 02 '23 11:01

hmisenar


1 Answers

Set the context like this:

    const parameters = { // Custom parameters to pass with context
      welcome: true,
    };

    conv.contexts.set('welcome-context', 5, parameters);

The second parametr is for context lifespan.

In your example code:

const functions = require('firebase-functions');
const { dialogflow } = require('actions-on-google');

const app = dialogflow();

app.intent('Default Welcome Intent', conv => {
  conv.ask('How are you?');
  const parameters = { // Custom parameters to pass with context
      welcome: true,
    };
  conv.contexts.set('welcome-context', 5, parameters);
});

exports.factsAboutGoogle = functions.https.onRequest(app);

Then you can access the contexts with:

const contexts = conv.contexts;
like image 191
Denis Avatar answered Jun 06 '23 17:06

Denis