Search code examples
dialogflow-esdialogflow-es-fulfillmentinline-editor

Dialogflow Context


I got some problem, I can't access my parameters from context on dialogflow, i just trying using agent.getContext and agent.context.get but still not work.

there is my code for set the context

function noTelp(agent){
const telp = agent.parameters.phoneNumber;

let query = db.collection('pelanggan').where('no_telp','==',telp);
return query.get().then(snapshot => {
    if (snapshot.empty) {
      agent.add('Mohon Maaf data no telepon '+telp+' tidak ditemukan');
      agent.add('untuk menambahkan data kamu silahkan tuliskan nama kamu');
      agent.setContext({     >set the context
        name : 'tambahData',
        lifespan : 2,
        parameters : {noTelp : telp}
      });
      console.log('No matching documents.');
      return;
    }
}

and this for the calling the context

function tambahData(agent){
   const context = agent.getContext('tambahData'); >get the context
   const telp = context.parameters.noTelp; >get the parameters from context
   const nama = agent.parameters.nama;

   agent.add(nama+telp); >test calling parameters
}

Solution

  • Used a consistent method either from V1 or V2. You can modify the code as below, it will work. I managed to work like this only.

    Setting context:

    agent.context.set({
            name: 'global_main_context',
            lifespan: 5,
            parameters: param
        });
    

    Getting Context

    let globalContext = agent.context.get('global_main_context');
    

    I would suggest to keep updating the context in each of transaction because it as lifespan that will automatically kill that context if you cross a number of transactions.