2

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
}
Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • Showing the code is very useful! However, can you update your question to show how `noTelp()` and `tambahData()` are being called? Or are these your Handlers (in which case, can you show how you are registering them as handlers)? You've also said that it doesn't work. Can you show an example of what you're doing that illustrates what is happening instead? – Prisoner Mar 02 '20 at 09:41

1 Answers1

1

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.

Abhinav Kumar
  • 2,883
  • 1
  • 17
  • 30