Search code examples
dialogflow-esdialogflow-es-fulfillment

How to use session entity in dialogflow's inline editor?


My problem is passing parameters between intents' hooks.

As example: at one hook I made a request for my own backend and receive a sessionId. I need pass the sessionId to an another intent for second request in it's hook.

How I can do it? I can't find any examples, documentations or best practices.

Thanks for your attention!


Solution

  • You can do this with contexts.

    In your first intent you do something like this:

    function firstIntentHandler(agent){
      return axios.get(`https://yourserver.com`)
       .then(function (response)  {
        agent.add(response.data.sessionId);
    
        const context = {'name': 'yourcontextname', 'lifespan': 3, 'parameters': 
                         {'sessionid': response.data.sessionId }}; 
        agent.setContext(context);
    
            }).catch(function (error) {
        agent.add("An error occured.");
      });
    }
    

    sessionId is the variable name in your json data sent by your server.

    In your other intent you can access this data like this:

    function otherIntentHandler(agent) {
      const context = agent.getContext("yourcontextname");
      const sessionid = context.parameters.sessionid;
      // do something with this session id
      });
    } 
    

    Be careful with camelcase for your context name. I have the feeling that uppercase letters are stored as lowercase...