如何为对话框流中的列表选项设置输出上下文?
How to set output context for list options in dialogflow?
如何为列表中的每一项提供输出上下文?
我在处理列表选项时遇到问题,因为输出上下文不存在。
例如在这段代码中:
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;
};
好吧,我来晚了,但我最近做了这个。
在这个示例代码中,我返回了一个项目列表
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);
});
创建另一个 intent 来处理这个并添加事件 Google Assistant Option 这个事件起到了作用,它会将选定的选项发送到您的 intent 处理程序,其中您可以访问所选选项以及我们在上下文中设置的列表。
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))
});
如何为列表中的每一项提供输出上下文? 我在处理列表选项时遇到问题,因为输出上下文不存在。 例如在这段代码中:
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;
};
好吧,我来晚了,但我最近做了这个。
在这个示例代码中,我返回了一个项目列表
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);
});
创建另一个 intent 来处理这个并添加事件 Google Assistant Option 这个事件起到了作用,它会将选定的选项发送到您的 intent 处理程序,其中您可以访问所选选项以及我们在上下文中设置的列表。
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))
});