简体   繁体   中英

Infinite loop between a function and if condition (Javascript, Dialogflow)

I'm working with dialogflow to make a weather chatbot, using NodeJS and ngrok. I need to create a new kind of conversation, like this :

  • User : What is the weather in London ?
  • Bot : Before I answer, I want to know your name
  • User : My name is Jack
  • Bot : Jack, the weather in London is etc...

To make it, I need a function to search if a name is already registered inside the context. Name is stored inside a context name memory

var memory = chercherContexte(req.body.queryResult.outputContexts,'memory');

Here is the function :

function chercherContexte(outputContexts, nom) {


    for (var i=0; i<outputContexts.length; i++) {
          console.log(outputContexts[i])
          if (outputContexts[i].name === "projects/botname/agent/sessions/id_sessions/"+nom ) {
          console.log("Context is found");
          return outputContexts[i]
          }
      }
     return undefined;
    }

Then, I need to check if a name is defined (or not). And if I can't find a name, I need to set new contexts and events.

var first_name = '';
if ((memory !== undefined) && (memory.parameters['first_name']!==undefined)) 
   first_name=memory.parameters['first_name'];

if (first_name === '' ) {
    out = construireReponse("");
    out.outputContexts = [{name:'projects/botname/agent/sessions/id_sessions/memory', lifespanCount:5, parameters:{intention:'question', intention_action:'openWeatherMap',parameters_Action:req.body.queryResult.parameters}}];
    out.followupEventInput = {name:'askName'}
    console.log("Done");
    res.json(out);
}

What happens is that the webhook keep searching for the context, go through the if, then restart. The webhook redo the same operation three times. In the dialogflow console, I notice that the call to the webhook is considered as failed because of this loop.

How can I solve this problem ?

Thank you !

If you call chercherContexte(req.body.queryResult.outputContexts,'memory') ,

in your if statement "projects/botname/agent/sessions/id_sessions/" + name would result in

projects/botname/agent/sessions/id_sessions/memory

But when setting the context, you use

projects/agent_name/agent/sessions/sessions_id/contexts/memory

so afterwards you will never find the right context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM