How to save data between conversation in Dialogflow?

I don't understand how you're supposed to save data. I tried using

let conv = agent.conv()
conv.data.data1=mydata //to save
mydata = conv.data.data1 //to load
agent.add(conv)

but it crash my app. I saw that you could put info in

var token = JSON.stringify(request.body.originalDetectIntentRequest.payload.conversation.conversationToken);

but how do you put data in conversationToken in the response?

What is your method? Thanks


Solution 1:

Use the output context to save parameters

{  
  "fulfillmentText":"This is a text response",
  "fulfillmentMessages":[  ],
  "source":"example.com",
  "payload":{  
    "google":{  },
    "facebook":{  },
    "slack":{  }
  },
  "outputContexts":[  
    {  
      "name":"<Context Name>",
      "lifespanCount":5,
      "parameters":{  
        "<param name>":"<param value>"
      }
    }
  ],
  "followupEventInput":{  }
}

If you are using NodeJS client

You can save context with parameters like

let param1 = [];
let param2 = {};
let ctx = {'name': '<context name>', 'lifespan': 5, 'parameters': {'param1':param1, 'param2': param2}};
agent.setContext(ctx);

and get it like

let params = agent.getContext("<context name>").parameters;
let param1 = params.param1;
let param2 = params.param2; 

You can store arrays, JSON obj but there is a limit to the total payload the response can save. check for more details here. For large data, use DB.

Also, if you are using standalone actions-on-google, then you can simply add key-value pair to the data object. See the link where they are storing a count