Create a Random Word Generator with Node-Red

Words
955
Reading
5 min
Listen
Play
4y

A few weeks ago I posted about using low-code/no-code software to build a "word of the day blog post" generator. My initial idea was to build one use the free version of the software from the company I work for, and I did! It worked. Until marketing got overzealous with a software update and wiped out my work.

Alas.

So I've decided to redo, but this time I will use a self-hosted Node-Red instance.

image.png

I need to say, I really, really, really like Node-Red. Every single time I use it I feel good and warm on the inside. Node-Red is an open source project with open source addons for it and, every time I look for something new, it's their, and I end up feeling that there isn't anything Node-Red would not be able to do.

For those unfamiliar, Node-Red is a "low-code/no-code," or "drag and drop" utility, used originally to built Internet of Things automations. It has since grown in popularity and can now do so much more.

To show the drag and drop capabilities of Node-Red, let's build the most simple type of flow: the current timestamp.

  • On a Node-Red instance simply click the "Inject" node onto the canvas.

image.png

  • Drag the "Debug" node onto the canvas. I like to put my side by side, but the canvas is flexible.

image.png

  • Click on the small grey button on the right of the timestamp node (this is the output of the timestamp node) and drag it to the small grey button on the left of the debug node (this is the input of the debug node)

image.png

image.png

This creates a flow link between the two nodes: The timestamp node sends it's output to the debug node. The debug node can then do whatever it wants with it.

Then do the following three tasks in order:

  1. Deploy the flow.
  2. View the debug page.
  3. Run the flow by pressing the timestamp button.

Expected result: In the debug pane you will see a timestamp of a number of digits. That value is the number of microseconds that have elapsed since January 1, 1970. We call this epoch time. Don't worry about that we won't use it for the remainder of the tutorial. The purpose of this example is to show just how easy using Node-Red can be.

image.png

Using the Internet

So what we want to do is use Node-Red to perform two functions:

  1. Get a random word.
  2. Get the definition for that word.

There are publicly available resources to do this.

Random Word

A random word can be obtained by browsing to https://random-word-api.herokuapp.com/word. Go ahead. Open a browser and try it. You'll see a word. It will be in square brackets.

The square brackets indicate that what is inside them is a list. In this case it is a list of one item, but there could be more.

Let's do something with this in Node-Red. Node-Red has a series of nodes or network utilities. This is exactly what we want! In fact, we want the http request node, since all we need to do is use that one URL (as a web/http request) to get a word.

image.png

Simply dragging the http request node onto the connecting line between the timestamp and debug nodes puts it into the flow.

image.png

Now, double click on the http request node to open up the toolbox. There will be a field to input the URL we want to query. Ultimately it will look like this:

image.png

Hit Done on the toolbox. Since we have modified the flow we need to deploy it again, so press the Deploy button top right, then press on the timestamp node again. See that we have successfully pulled a word! Woot!

image.png

Now we want to do something with that and, at this point, some knowledge of programming/coding/scripting is required. This is where the technical portion of the post begins.

Node-Red passes messages from node to node by the use of an object named "msg". The msg object contains a host of information, but the one we are really interested in is msg.payload. The payload object, in this case a list, contains the word. Because we know that the list contains only one item we can easily reference it as msg.payload[0].

Word Definition

https://dictionaryapi.dev/ can be used to get the definition of a word using this syntax:

https://api.dictionaryapi.dev/api/v2/entries/en/<word>

To do this I will simply take the initial word from the first http request node, use a function with some javascript to build the url accordingly, such as this:

https://api.dictionaryapi.dev/api/v2/entries/en/stubble

image.png

and then use a second http request node to query dictionaryapi.dev. The result, as seen in our new debug, is a full definition of the word.

Next Steps

This flow works, but it's barebones and I do not like the names of the nodes. Each node/block can be renamed by double clicking on it, so to make it easier to read and understand I've done just that:

image.png

Beyond that there are ways to make this work better:

  1. Use a conditional in case "get random word" does not return a word and exit cleanly.
  2. Use a conditional in case "get definition" does not return a definition and exit cleanly.
  3. Clean up the final msg.payload with the definition to format it to be easier to read.

My initial idea was to then use the chatai api to have an AI generated paragraph showing how the word can be used. This is doable in Node Red! That will be the subject of a later post.


(c) All images and photographs, unless otherwise specified, are created and owned by me.
(c) Victor Wiebe


About Me

Sometimes photographer. Wannabe author. Game designer. Nerd. 
General all around problem-solver and creative type.

Blind Skeleton

Blind Skeleton Banner.png

Online Radio: https://blindskeleton.one/radio/
Three Tune Tuesday Live Stream: 12:00pm (noon) EST

Frogs of War Games

Frogs of War Banner.png

Tidwick

Tidwick Banner.png

The Feet Community

Feet Banner.png

What I Learned Today

What I Learned Today Banner.png

Create a Random Word Generator with Node-Red | Ecency