0

In Dialogflow fulfillment I am trying to add or update parameters for a context.

let currentContext = agent.context.get('setupcall-followup');
console.log(agent.context.get('setupcall-followup').parameters); //1
currentContext.parameters.date = '2019-09-18T12:00:00-04:00';
currentContext.parameters.time = '2019-09-17T13:00:00-04:00';
currentContext.parameters['Test'] = 'Test';
console.log(agent.context.get('setupcall-followup').parameters); //2
agent.context.set(currentContext); //Seems to not be needed, since it is by reference
console.log(agent.context.get('setupcall-followup').parameters); //3, same as #2

By #2 & #3 the log shows that it was updated. But in the Diagnostic Info > Raw API Response, the outputContext information is still the original inputContext, before modification.

What I can do:

  • Add a new context, with it's own parameters.

What I can't do

  • Get a context & change a parameter
PrivatMamtora
  • 2,072
  • 2
  • 19
  • 29

2 Answers2

1

This is the only way I finally got it to work.

Use a custom context & recreate it completely, every-time. FYI Code below requires lodash for _.merge

function getSessionData() {
    if(agent.context.get('session_details')) {
        return agent.context.get('session_details').parameters.sessionDetails;
    } else {
        return;
    }
}

function setSessionData(data) {
    if(agent.context.get('session_details')) {
        let session = agent.context.get('session_details').parameters.sessionDetails;  //Extract the data from the existing "session_details" context
        _.merge(session, data);   //Update our data
        let sessionContext = {    //Define the complete context object again
            name: "session_details",
            lifespan: 5,
            parameters: {
                sessionDetails: session
            }
        }
        agent.context.set(sessionContext);
    } else {
        let sessionContext = {    //Define the complete context object again
            name: "session_details",
            lifespan: 5,
            parameters: {
                sessionDetails: data
            }
        }
        agent.context.set(sessionContext);
    }
}
PrivatMamtora
  • 2,072
  • 2
  • 19
  • 29
0

I think this may help.

const parameters = {'param1':value, 'param2': value};
agent.context.set(context, 5, parameters); //here "5" is lifespan.

Tell me if it worked.

Ali Hasnain
  • 190
  • 1
  • 1
  • 9
  • Nope, it doesn't work. I tried it with this: const parameters = {'param1': 'value', 'param2': 'value'}; agent.context.set('Test', 5, parameters); – PrivatMamtora Sep 18 '19 at 17:57
  • Correction. That does work, if I am trying to set a new context. What I am trying to do is manipulate a parameter (update) inside a current context. – PrivatMamtora Sep 18 '19 at 18:14
  • If this is the case, you can get parameters from old context -> save parameters in variable -> delete old context -> create new context with same name -> add params from variable. – Ali Hasnain Sep 19 '19 at 06:57
  • When 'deleteing', the context lifespan is set to 0. And removed for next round of conversation. (I tried) – PrivatMamtora Sep 20 '19 at 13:32