如何在dialogflow中设置列表选项的输出上下文?

问题描述 投票:0回答:1

如何为列表的每个项目提供输出上下文?我在处理列表选项时遇到了麻烦,因为输出上下文不存在。例如下面的代码:

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;
};
node.js actions-on-google dialogflow-fulfillment
1个回答
0
投票

好,我来晚了,但最近我这样做了。

在此示例代码中,我将返回项目的下拉列表

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);
 });

创建另一个意图来处理此问题并添加事件Google助手选项此事件可以解决问题,它将把选定的选项发送到您的意图处理程序,您可以在其中访问选定的选项以及我们提供的列表在上下文中设置。

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

© www.soinside.com 2019 - 2024. All rights reserved.