Oortone
Oortone

Reputation: 185

Correct way to set context in javascript for dialogflow

I've been struggling to set an output context in javascript for a dialogflow webhook and although I've tried numerous examples I've found I get an "Failed to parse" in DialogFlow whatever I try. This is the latest try:

const app = dialogflow({debug: true});
...
app.intent('YesNo', (conv) => {
  if (numberOfRounds < 5){
    conv.ask("Ny fråga?");
    ++numberOfRounds;
    conv.setContext({
      "name": 'yesnoexpected',
      "lifespan": 1,
      "parameters":{"param": "param value"}
    });
  } else {

I'm sure the error is related to the context object because if I remove it then there's no parse error, although functionality of course is not as intended. But where's the error?

Upvotes: 0

Views: 425

Answers (1)

jacobytes
jacobytes

Reputation: 3281

You are setting the context using a JSON notation, but this is not supported by the actions on google library. If you want to set context in a webhook you should use Javascript objects to do it, like such:

conv.contexts.set("ContextName", 5, {contextString: "Value", contextNumber: 1});

and then you can the parameters from a previous set context like this:

const context = conv.contexts.get("ContextName");
const parameter = context.parameters.contextString;

Upvotes: 3

Related Questions