Search code examples
node.jsactions-on-googledialogflow-es-fulfillment

How to set output context for list options in dialogflow?


How do I give an output context to each item of a list ? I am having trouble with handling the list options because the output context is not there. For example in this code :

const meditatetrackList = () => {
  const list = new List({
    title: 'Choose a track.',  
    items: {
           'healing water': {
       title: 'Healing Water',
       synonyms: ['healing water'],
       image: new Image({
         url: 'http://www.vstplanet.com/News/2016/Nature-sounds/Relaxing-nature-sounds.jpg',
         alt: 'healing water',
       }),

     },
     'elven forest': {
       title: 'Elven Forest',
       synonyms: ['elven' , 'forest' , 'elven forest'],
       image: new Image({
         url: 'https://scx2.b-cdn.net/gfx/news/2018/europeslostf.jpg',
         alt: 'elven forest',
       }),
     },
      'warm light' : {
        title : 'Warm Light',
        synonyms: ['warm','light','warm light'],
        image: new Image({
          url: 'https://www.socwall.com/images/wallpapers/37753-2000x1300.jpg',
          alt: 'warm light',
        }),
      }
    }
  });
  return list;
};

Solution

  • Okay, I am late here but I recently did this.

    In this example code, I am returning a dropoff list of items

    Util.js

    getDropOffListCard: function(nearestDropOffResponse, agent) {
            let objItem = {};
            for (let index = 0; index < nearestDropOffResponse.length; index++) {
                const element = nearestDropOffResponse[index];
                let key = element.name.toUpperCase().replace(/ /g, "_");
                let each = {
                    synonyms: [
                        'synonym 1',
                        'synonym 2',
                        'synonym 3',
                    ],
                    title: element.name,
                    description: element.address + '\n' + element.distance + ' ' + element.unit,
                    image: new Image({
                        url: url + 'info-icon.png',
                        alt: 'Image alternate text',
                    })
                };
                objItem[key] = each;
            }
            agent.context.set({ // here set the context
                name: 'global_context',
                lifespan: 2,
                parameters: {
                    dropLocationList: objItem
                }
            });
            return new List({
                title: 'Nearest drop off location',
                items: objItem
            });
        }
    

    app.js

    intentMap.set("Nearest Drop Off Intent - yes", function(agent){
            const conv = agent.conv();
            conv.ask('Here are nearest drop off location. Where you can drop or pickup your parcel.');
            conv.ask(trackingUtil.getDropOffListCard(nearestDropOffResponse, agent));
            agent.add(conv);
     });
    

    Create another intent to handle this and add the event Google Assistant Option this event do the trick, it will send the selected option to your intent handler, where you can access the selected option as well as a list that we set in context.

    intentMap.set("Nearest Drop Off Intent - yes - selected", function(agent){
       const option_intent = agent.context.get('actions_intent_option');
        const option_key = option_intent.parameters.OPTION;
        const global_context = agent.context.get('global_context');
        const selection = global_context.parameters.dropLocationList[option_key]
        agent.add(selection.title);
        agent.add(selection.description);
        agent.add(trackingUtil.dropOffDetailCard(selection))
    });
    

    enter image description here