I am having a problem with the contexts of dialogflow, when trying to change them through fulfillment. When I want to add (or clear, by setting lifespanCount to -1) a context I generally use this code:
const {WebhookClient} = require('dialogflow-fulfillment');
const agent = new WebhookClient({ request, response });
/*Irrelevant Code Here*/
var context=agent.session+"/contexts/contextName";
agent.setContext({
"name":context,
"lifespanCount": 1
});
If I use this snippet, the contexts can be successfully manipulated. However, I am now trying to use this snippet when sending a request-promise and, based on the response of that request, I want to have different outputContexts. For some reason, this action doesn't have any results, I cannot clear the contexts or create one, as these don't appear on the Firebase logs when I print JSON.stringify(request.body).
return new Promise( (resolve,reject) => {
const request = require('request-promise-native');
const options = {
uri: 'link',
method: 'POST',
headers: {
'api-token': 'token'
},
body: {
someData:"someData",
},
json: true
};
request(options)
.then(function (body) {
if (body.status===0)
agent.add("Option 1");
else {
var sessionIdArray=agent.session.split("/");
var sessionId=sessionIdArray[sessionIdArray.length-1];
var context=agent.session+"/contexts/contextName";
agent.setContext({
"name":context,
"lifespanCount": 1
});
agent.add("type something");
}
resolve();
})
.catch(function (err) {
// POST failed...
agent.add("An error happened during your last call.");
console.log(err.stack);
resolve();
});
});
It's like the promise-request prevents the agent to set the context. Does anyone know how to make this work? Thanks in advance!