Varo OP
Varo OP

Reputation: 83

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. This other integer is different for each option. You can think of the other integer as an ID, it's unique for each option. How can I achieve that?

Upvotes: 1

Views: 51

Answers (2)

Prisoner
Prisoner

Reputation: 50711

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).

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 encodeOptionKey( key, otherValue ){
  return `${key}:${otherValue}`
}

and then decode it with a function such as

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

Upvotes: 1

Varo OP
Varo OP

Reputation: 83

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. Then I used that map to get the parameter from the carousel key that was being passed to the handler intent.

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.

Upvotes: 1

Related Questions