简体   繁体   English

如何从显示轮播的意图向处理轮播的意图传递除选项以外的一个参数?

[英]How to pass one more parameter other than option from the intent showing the carousel to the intent handling the carousel?

I am using an intent to first present the carousel to the user.我正在使用意图首先向用户展示轮播。 When the user clicks on one of the options in the carousel, in the handler intent I get the key of the carousel item that the user selected.当用户单击轮播中的选项之一时,在处理程序意图中,我将获得用户选择的轮播项目的键。

Example of carousel intent,轮播意图示例,

app.intent('search', async (conv,params) => {
 conv.ask(`Choose one item`,new Carousel({
   title :`Search results`,
   items : carouselItems,
        }));
});

Example of the handler intent,处理程序意图的示例,

app.intent('handle_carousel', async (conv,params,option) => {
const key = parseInt(option);
});

However, along with the key of the option selected I also want to pass another integer from the carousel intent to the handler intent.但是,除了所选选项的键之外,我还想将另一个 integer 从轮播意图传递给处理程序意图。 This other integer is different for each option.其他 integer 对于每个选项都不同。 You can think of the other integer as an ID, it's unique for each option.您可以将其他 integer 视为一个 ID,它对于每个选项都是唯一的。 How can I achieve that?我怎样才能做到这一点?

I created a map of the keys of various carousel options and the corresponding parameter I wanted to pass, saved that map in conv.data.store, which is the conversation storage provided by actions-on-google.我创建了一个 map 的各种轮播选项的键和我要传递的相应参数,将 map 保存在 conv.data.store 中,这是 actions-on-google 提供的会话存储。 Then I used that map to get the parameter from the carousel key that was being passed to the handler intent.然后我使用 map 从传递给处理程序意图的轮播键中获取参数。

For example in the carousel intent:例如在轮播意图中:

let map = {
keyofcarousel : option,
other_parameter : otherparam,
};

conv.data.store = map;

Then call conv.data.store in the handler intent.然后在处理程序意图中调用 conv.data.store。

You have a few approaches for passing additional data that should be associated with each key.您有几种方法可以传递应与每个键关联的附加数据。

The first is, as you note in your answer, storing that mapping in a table that is stored as part of session data (either using conv.data or a Dialogflow context).第一个是,正如您在回答中指出的那样,将该映射存储在一个表中,该表存储为 session 数据的一部分(使用conv.data或 Dialogflow 上下文)。

Another is to encode that data as part of the key that you include with each option, and then decode the key when you get it back.另一种方法是将该数据编码为您包含在每个选项中的密钥的一部分,然后在您取回密钥时对其进行解码。

So, for example, you could make the key a result of an encode function like因此,例如,您可以将密钥作为编码 function 的结果,例如

function encodeOptionKey( key, otherValue ){
  return `${key}:${otherValue}`
}

and then decode it with a function such as然后使用 function 对其进行解码,例如

function decodeOptionKey( option ){
  const [key,otherValue] = option.split(':');
  return {
    key,
    otherValue
  }
}

and call this from your handler with something like并从您的处理程序中调用它,例如

app.intent('handle_carousel', async (conv,params,option) => {
  const {key, otherValue} = decodeOptionKey( option );
  // ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM