Last active
June 25, 2020 14:47
-
-
Save thomaslombart/6033a4ce32ea5b121f8416346b0d3b11 to your computer and use it in GitHub Desktop.
Save data in conversation for dialogflow-fulfillment Node.js library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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