Google action fetching data from an RSS feed

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Now that we are able to fetch data from an RSS feed and show it in the Google Action, how about we add a button to learn more. A thing to note is that depending on the device, the button might not be rendered. For instance smart home devices won't sh...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In the previous article, we looked at fetching data from an API for our Google action.
However, in my case, I don't have an API at my disposal. I have an RSS feed, so let's see how we can use that to fetch the latest article, which we can return.
If you'd like to follow along, you can use this article as your starting point.
Since our action is based on a cloud function, we can add some dynamic data fetching.
Open up your webhook package.json, and let's first add the package we need to parse XML.
{
"dependencies": {
// The other deps
"xml-js": "^1.6.11"
}
}
Now you can head over to the index.js file and start importing the packages we need.
This includes the node-fetch API.
const fetch = require('node-fetch');
const convert = require('xml-js');
Then we'll need to convert our handle function to an async function.
// Previous
app.handle('greeting', (conv) => {});
// New
app.handle('greeting', async (conv) => {});
And then, we can start by fetching our RSS feed. This can be parsed as plain text.
const response = await fetch('https://daily-dev-tips.com/sitemap.xml');
const text = await response.text();
From here, we can use the xml-js package to convert it into a readable stream of JSON.
const jsonData = JSON.parse(convert.xml2json(text, { compact: true }));
Note: I'm using compact mode, which removes a lot of unneeded lines
Then we can add a new card with the first item's data.
conv.add(
new Card({
title: jsonData.feed.entry[0].title._cdata,
text: jsonData.feed.entry[0].content._cdata,
image: new Image({
url: 'https://daily-dev-tips.com/images/logo.png',
alt: 'Daily Dev Tips logo',
}),
})
);
Save and deploy your function; wait for the deployment to finish and test it out.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter