DEV Community

Thomas Südbröcker
Thomas Südbröcker

Posted on • Edited on

A Node-RED Twitter-follower-flow

In my last blog post I did an introduction "how use the starter kit for Node-RED on IBM Cloud" and in that new blog post I want to highlight the Node-RED Twitter-follower-flow I developed as an introduction to different topics I frequently face at hackathons. Here is the link to the "Twitter-follower-flow" GitHub project.

Node-RED is very good for prototyping, that is the reason why it is often used in hackathons. If you are new to Node-RED and you start to develop a Node-RED flow, you normally have following challenges:

  • How to ...
  1. ... define own REST endpoints to encapsulate an external API?
  2. ... automate the authentication to that external API?
  3. ... extract data from the external API?
  4. ... customize data and CRUD with databases?

The Node-RED flow of that project has the objective to provide an (little advanced) introduction to the first three topics above.
The CRUD (Create, Read, Update, Delete) topic is not covered here. Visit that CRUD example for more information.

  • The "Twitter-follower-flow" example ...

    • ... uses open technologies (Node-RED is a Project of the OpenJS foundation
    • ... creates no additional costs
    • Uses a free runtime on IBM Cloud
    • Uses the free Twitter developer API
    • ... has following technical level:
    • Beginner to intermediate
    • Needs a basic knowledge of JavaScript and REST
    • ... takes 30 - 45 min to setup the example from scratch
    • Register on IBM Cloud
    • Create a Node-RED instance on IBM Cloud
    • Register at Twitter for a developer API Account
    • Copy the existing Node-RED flow
    • Configure the flow
    • Run the flow

The YouTube video gives a 13 min introduction to the Twitter-follower-flow.

Introduction to the Node-RED flow

1. The UseCase

The UseCase "Extract the twitter follower list" addresses three topics listed above in the Node-RED Twitter-follower-flow.

  1. Define own REST endpoints to encapsulate Twitter developer API calls.
  2. Automate the authentication the two step authentication of the Twitter developer API.
  3. Extract data from the Twitter follower list and build an own list.

2. Topics related to the Node-RED usage

The Twitter-follower-flow is an concrete example of following topics ...

  • ... the implementation of Node-RED REST endpoints.
  1. Create a very basic authentication to protect each Node-RED endpoint
  2. Realize the two step authentication (also known as two-factor authentication) to access the Twitter developer API: 1. Basic authentication with a key and secret to request a bearer token from Twitter 2. Use the bearer token for authentication to use the Twitter API
  3. Get the follower list from Twitter using the bearer token
  • ... the usage of the implemented REST endpoints to automate following sequence to get the Twitter follower.
  1. Get the authorization bearer token from Twitter to access the Twitter API
  2. Get all followers from Twitter using a bearer token ( the challenge is: how implement the paging thought the followers list from Twitter )
  3. Extract the names of the follower from the follower list
  • ... the work with following Nodes in the Node-RED Twitter-follower-flow.
  1. Function (Do small programming in javascript.)
  2. Inject (Start a flow in Node-RED.)
  3. HTTP in (Creates an HTTP end-point for creating web services.)
  4. HTTP request (Sends HTTP requests and returns the response.)
  5. HTTP response (Sends responses back to requests received from an HTTP Input node.)
  6. Base64 (A function that converts the chosen property (default msg.payload) to and from base64 format.)
  7. JSON (Converts between a JSON string and its JavaScript object representation, in either direction.)
  8. Switch (Define a decision how to route the payload. Route messages based on their property values or sequence position.)
  • ... the usage of flow variables to exchange values between functions in the Twitter-follower-flow.

3. Setup the Twitter-follower-flow example

That section contains six major steps to setup the Twitter-follower-flow example on IBM Cloud.

  1. Setup Twitter developer application
  2. Create a Node-RED instance on IBM Cloud
  3. Install a additional Node to the Node-RED instance
  4. Import the Node-RED flow
  5. Configure the Node-RED flow
  6. Introduction to the Node-RED flow (13 min video)

In the image you see the full flow.


Step 1: Setup Twitter developer application

You need a Twitter account and a Twitter developer application.
With your Twitter account can register for the Twitter developer API and create a Twitter developer application. That application provides the needed credentials to access the Twitter API, which is used in that example.

a. Link to add a Twitter developer application.

Here is a blog post with an example how to setup a Twitter application: How to Register a Twitter App in 8 Easy Steps

Here is a preview, how to get the credentials from your Twitter developer application:

b. Twitter API documentation get-followers-list


Step 2: Create a Node-RED instance on IBM Cloud

We use a Node-RED instance on IBM Cloud with an IBM Lite Account.

  1. Create an IBM Cloud Lite Account just by register here.
  2. Follow the steps in my blog post to setup a Node-RED instance on IBM Cloud

Step 3: Install a additional Node to the Node-RED instance

Install the node-red-node-base64 Node to the Node-RED instance. The gif shows the installation.


Step 4: Import the Node-RED flow

Import the flow from the flow.json file in flows folder of that project. The gif below shows how to import the flow in the Node-RED.


Step 5: Configure the Node-RED flow

We need to configure ...

  • ... the Twitter authentication
  • ... the Twitter username
  • ... the Node-RED URL

a. Set Twitter API key and secret

Insert the values for the user and secret of the Twitter API credentials in the function set user and secret. The image shows an example, where you get the credentials information.

The mapping for the Node-RED flow:

  1. user = Twitter API key
  2. secret = Twitter API secret key
  3. nodereduser = Your own definition to secure the Node-RED REST Endpoints
  4. noderedpassword = Your own definition to secure the Node-RED REST Endpoints

Here is the source code for the set user and secret function.

var user = "USER";
var secret = "SECRET"
var nodereduser = "admin";
var noderedpassword = "notreallysecure"

flow.set("nodereduser", nodereduser);
flow.set("noderedpassword", noderedpassword);

msg.payload = user + ":" + secret;

return msg;

b. Set Twitter username

Define "username display name" you want get the follower list from, by inserting
in function set_basic_auth the "YOUR_TWITTER_DISPLAY_NAME".

// Set basic auth
flow.set("auth", msg.payload);
msg.payload = flow.get("auth");

// Init parameters:
// For more information visit get-followers-list:
// https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list
flow.set("twitterCursor", "-1");
flow.set("twitterResultCount", "50");
flow.set("twitterSN", "YOUR_TWITTER_DISPLAY_NAME");

msg.nodereduser = flow.get("nodereduser");
msg.noderedpassword = flow.get("noderedpassword");

return msg;

c. Set Node-RED URL

Configure the HTTP request nodes. Replace the https://node-red-my-hackathon.mybluemix.net URL with your URL in each HTTP request node:

  • getTwitterFollower-Indicrect
  • getTwitterToken-Indicrect

The image shows how you enter the URL in the HTTP requests node getTwitterFollower-Indicrect.

That image shows HTTP requests nodes to be change.


Step 6: Introduction to the Node-RED flow

The YouTube video gives a 13 min introduction to the Twitter-follower-flow.

Introduction to the Node-RED flow


I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

Blog post also at www.suedbroecker.net

Top comments (0)