有关列表卡中被选择对象的问题(Google的操作)

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

我正在开发一个购物机器人,用户在其中寻求产品,并且他们正在从数据库中动态显示在列表卡中。在这里,我的问题是如何获得用户在项目列表中选择的选项。附上我的控制台的代码和屏幕截图。

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!')
      } 


    }
}

请帮帮我,Thnx Ramya。

list dialogflow actions-on-google
1个回答
0
投票

发送列表时,您为列表中的每个项目指定了一个“选项键”。这些键是将根据用户选择发送回的字符串。如果您使用这样的方式发送列表

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

然后您将拥有三个键:

  • SELECTION_KEY_ONE
  • SELECTION_KEY_GOOGLE_HOME
  • SELECTION_KEY_GOOGLE_PIXEL

其中一个将在处理程序方法的option参数中返回给您。因此您的处理程序可能看起来像

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!')
      } 
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.