Skip to content

Instantly share code, notes, and snippets.

@thomaslombart
Last active June 25, 2020 14:47
Show Gist options
  • Save thomaslombart/6033a4ce32ea5b121f8416346b0d3b11 to your computer and use it in GitHub Desktop.
Save thomaslombart/6033a4ce32ea5b121f8416346b0d3b11 to your computer and use it in GitHub Desktop.
Save data in conversation for dialogflow-fulfillment Node.js library
// agent is an instance of WebhookClient
function setContextData(agent) {
const handler = {
set(target, property, value) {
const parameters = agent.context.get("data")
? agent.context.get("data").parameters
: {};
parameters[property] = value;
agent.context.set("data", 99, parameters);
}
};
const obj = agent.context.get("data")
? agent.context.get("data").parameters
: {};
agent.data = new Proxy(obj, handler);
return agent;
}
// This function must be used as a middleware
const agent = setContextData(new WebhookClient({ request, response }))
// Later on, we can make use of the data object
agent.data.count = 0 // -> will be available in the whole conversation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment