Search code examples
dialogflow-es

I need to store sesion variable witn PHP y Dialogflow


my project is to have a chatbot that can sell electronic device like resistor, diode and so on. There are many type of resistor and diode

I have my intents in dialogflow so when user choose one item I need to store it into php session variable so when user have finished so I need to restore that items in session variable but it doesnt work

Recieve dialogflow intent that is called "tomar_cantidad"

  if (intent_recibido("tomar_cantidad")) {

  $modelo = obtener_variables3('modelo');
  $cantidad = obtener_variables2('cantidad');
  //enviar_texto(print_r($cantidad));

  $datos_usuario = array('modelo'=>$modelo,'cantidad'=>$cantidad);

   $_SESSION['datos_usuario'][]= $datos_usuario;

   enviar_texto(print_r($_SESSION['datos_usuario']));
   }

enviar_texto is a method to send information to dialogflow(that is for testing purposes).

I recieve session variable but only store the last item that user choose not all items


Solution

  • The issue is that the PHP $_SESSION variable is tied to an HTTP session, which is usually implemented with an HTTP cookie. But Dialogflow doesn't manage HTTP cookies when it sends messages to a webhook, so each fulfillment call is a new session each time.

    You have a few ways you can approach this:

    • Get the Dialogflow session ID and store the information against this ID

    • Store any values you want to store between turns in the conversation as a parameter in an Output Context. You should set the lifespan for the Context to a large number, 99 is typical, or re-set this as the Output Context each time. Then, you can read the parameter in the named Context when you need the information.