Reading through the documentation for Actions on Google, I've built a browse carousel by using a rich response class and supplying it with a simple response, the carousel and some suggestion chips.
const carouselItems = [];
items.forEach(item => {
const carouselItem = new BrowseCarouselItem({
description: item.description.text,
image: new Image({
alt: item.title,
url: item.image,
}),
url: item.link,
title: item.title,
})
carouselItems.push(carouselItem)
});
conv.ask(new RichResponse({
items: [{
simpleResponse: new SimpleResponse("the simple response message"),
carouselBrowse: new BrowseCarousel({
items: [ browseCarouselItem1, browseCarouselItem2 ],
}),
}],
suggestions: ["suggestion1", "suggestion2"]
});
After spending time doing this, I discovered that the rich response class appears to be unnecessary. In fact, I can get the same result by using conv.ask()
three times in a row instead.
conv.ask("the simple response message");
conv.ask(new BrowseCarousel({
items: [ browseCarouselItem1, browseCarouselItem2 ]
}));
conv.ask(new Suggestions([ "suggestion1", "suggestion2" ]);
With this discovery, I find myself uncertain of the correct approach. Does one of these methods replace the other? What is the best practice? I've used RichResponse
a few times throughout my webhook and I'm now unsure if I am working with an obsolete class.
It is not "obsolete", but it is unnecessary. The standard practice is to call ask()
multiple times and have the library build the RichResponse
object for you. But the library does use it internally, so it isn't obsolete.
When I use the actions-on-google library, I prefer to use the multiple ask()
since I don't have to use all of them. I can include some of them in different blocks depending on what features were available or what responses may be appropriate.
Use whatever works best for you.