Search code examples
javascriptnode.jsdialogflow-esactions-on-googledialogflow-es-fulfillment

How to clear user storage (Dialogflow)


I am storing a value in my user storage (in my case the city of which the user wants to hear the news). I want the user to be able to change this city and thus change the user storage. I have tried multiple things but did not get it to work yet, hope that someone can help.

When the user chooses to change the city, the regio_kiezen intent is called. This intent asks which city you want using an entity called regio which contains multiple cities. See the screenshot of this intent (btw: the event is added for another scenario where this intent gets called):

regio kiezen intent

After prompting for a parameter value it goes to the fulfillment. This is the fulfillment code for this intent and the intent called Regio (This is the intent where the news headlines are shown):

app.intent('regio_kiezen', (conv, params) => {
  conv.user.storage.regio = {};
  var chosen_regio = params['regio'];
  conv.user.storage.regio = chosen_regio;
  console.log(`user: ${conv.user.storage.regio}`);
  conv.followup('Regio');
  console.log("intent: regio kiezen intent");
});

app.intent('Regio', (conv) => {
  console.log("regio intent");
  console.log(`doebiesnoesje: ${conv.user.storage.regio}`);
  conv.ask(new Suggestions('Regio veranderen'));
  conv.ask(new Suggestions(['Suggestion 2', 'Suggestion 3']));
  return axios.get(`api-adress/${conv.user.storage.regio}`) //can not share the real api-adres
    .then((result) => {
      // informatie ophalen
      for (let i=0; i <= 5; i++) {
        titels[i] = (result.data.categories[0].news[i].title); // titel
        subtexts[i] = (result.data.categories[0].news[i].text); // subtext
        images[i] = (result.data.categories[0].news[i].media[0].image); // image
        links[i] = (result.data.categories[0].news[i].url); // link
      }

      // LIST
      if (!conv.screen) {
        conv.ask('Sorry, try this on a screen device or select the ' +
          'phone surface in the simulator.');
        return;
      }
      console.log(`Regioo:`);
      if(conv.user.storage.regio == 'nhgooi') {
        praat = 'het Gooi';
      } else {
        praat = conv.user.storage.regio;
      }
      conv.ask(`Hier volgt het laatste nieuws uit ${praat}`);
      conv.ask(new List({
        title: praat,
        items: {
          // Add the first item to the list
          'SELECTION_KEY_ONE': {
            synonyms: [
              'synonym 1',
              'synonym 2',
              'synonym 3',
            ],
            title: titels[0],
            image: new Image({
              url: images[0],
            }),
          },
          // Add the second item to the list
          'SELECTION_KEY_GOOGLE_HOME': {
            synonyms: [
              'Google Home Assistant',
              'Assistant on the Google Home',
          ],
            title: titels[1],
            image: new Image({
              url: images[1],
            }),
          },
          // Add the third item to the list
          'SELECTION_KEY_GOOGLE_PIXEL': {
            synonyms: [
              'Google Pixel XL',
              'Pixel',
              'Pixel XL',
            ],
            title: titels[2],
            image: new Image({
              url: images[2],
            }),
          },

          'SELECTION_FOUR': {
            synonyms: [
              'Google Pixel XL',
              'Pixel',
              'Pixel XL',
            ],
            title: titels[3],
            image: new Image({
              url: images[3],
            }),
          },

          'SELECTION_KEY_FIVE': {
            synonyms: [
              'Google Pixel XL',
              'Pixel',
              'Pixel XL',
            ],
            title: titels[4],
            image: new Image({
              url: images[4],
            }),
          },
        },
      }));
    })
    .catch((err) => console.log(err));
  });

The problem I get is that it keeps showing the news of the old (first set) city. Thus, it looks like conv.user.storage.regio is not being changed. I have no clue why this is happening and hope that someone can help! Thanks in advance.


Solution

  • Thanks for all the comments. I have found a solution. Clearing userstorage or updating is not seen in intent 1 when it happens in intent 2. What did work was making a new user storage variable in the regio_kiezen intent. Create a global variable to store this value in. In the Regio intent check whether this variable is undefined or not and choose to chose user storage or else the created variable. I know this might not be very clear so please have a look at the code:

    app.intent('regio_kiezen', (conv, params) => {
      console.log(`user1: ${conv.user.storage.regio}`);
      p = 0;
      r++;
      conv.user.storage.regio = '';  
      conv.user.storage.regio = params['regio'];
      test = conv.user.storage.regio;
      test2 = params['regio'];
      conv.user.storage.regio2 = test2;
      tert = conv.user.storage.regio2;
      console.log("testt: " + test + " test2: " + test2);
      console.log(`tert: ${conv.user.storage} en ${conv.user.storage.regio2} en ${tert}`);
      conv.followup('Regio');
      console.log("intent: regio kiezen intent");
      return tert;
    });
    
    app.intent('Regio', (conv) => {
      p++;
      console.log("regio intent");
      console.log(`tertt: ${conv.user.storage.regio2} en ${tert}`);
      //console.log("test: " + test + " test2: " + test2);
      console.log(`doebiedushi: ${conv.user.storage.regio}`);
      if (tert === undefined) {
        var regioo = conv.user.storage.regio;
      } else {
        var regioo = tert;
      }
      conv.ask(new Suggestions(['Regio veranderen', 'Meer nieuws']));
      return axios.get(`https:url/${regioo}`)
        .then((result) => { //rest of code
    

    This works and is stored between session and conversations!