I am developing a shopping bot where the user asks for a product and they are displaying in the list cards dynamically from the database. Here my question is how to get the option of what the user selects in the list of items. Attaching the code and screenshots of my console.
function did_select(conv, input, option)
{
console.log("option",conv);
const param = conv.getArgument('OPTION');
console.log("param",param);
for(var i = 0;i<=temparray1.length;i++)
{
if (option === temparray1[i]) {
conv.close('Number one is a great choice!')
}
}
}
Please help me out, Thnx Ramya.
When you sent the list, you specified an "option key" for each of the items in the list. These keys are strings that will be sent back based on what the user selects. If you sent the list using something like this
app.intent('List', (conv) => {
if (!conv.screen) {
conv.ask('Sorry, try this on a screen device or select the ' +
'phone surface in the simulator.');
return;
}
conv.ask('This is a list example.');
// Create a list
conv.ask(new List({
title: 'List Title',
items: {
// Add the first item to the list
'SELECTION_KEY_ONE': {
synonyms: [
'synonym 1',
'synonym 2',
'synonym 3',
],
title: 'Title of First List Item',
description: 'This is a description of a list item.',
image: new Image({
url: 'https://storage.googleapis.com/actionsresources/logo_assistant_2x_64dp.png',
alt: 'Image alternate text',
}),
},
// Add the second item to the list
'SELECTION_KEY_GOOGLE_HOME': {
synonyms: [
'Google Home Assistant',
'Assistant on the Google Home',
],
title: 'Google Home',
description: 'Google Home is a voice-activated speaker powered by ' +
'the Google Assistant.',
image: new Image({
url: 'https://storage.googleapis.com/actionsresources/logo_assistant_2x_64dp.png',
alt: 'Google Home',
}),
},
// Add the third item to the list
'SELECTION_KEY_GOOGLE_PIXEL': {
synonyms: [
'Google Pixel XL',
'Pixel',
'Pixel XL',
],
title: 'Google Pixel',
description: 'Pixel. Phone by Google.',
image: new Image({
url: 'https://storage.googleapis.com/actionsresources/logo_assistant_2x_64dp.png',
alt: 'Google Pixel',
}),
},
},
}));
});
Then you would have three keys:
One of these would be returned to you in the option
parameter of your handler method. So your handler might look something like
var temparray1 = [
'SELECTION_KEY_ONE',
'SELECTION_KEY_GOOGLE_HOME',
'SELECTION_KEY_GOOGLE_PIXEL'
];
function did_select(conv, input, option)
{
console.log("option",option);
for(var i = 0;i<=temparray1.length;i++)
{
if (option === temparray1[i]) {
conv.close('Number '+(i+1)+' is a great choice!')
}
}
}