A Node-RED Twitter-follower-flow

In my last blog post I did an introduction to “how use the starter kit for Node-RED on IBM Cloud” and in this 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
    • … 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
      1. Register on IBM Cloud
      2. Create a Node-RED instance on IBM Cloud
      3. Register at Twitter for a developer API Account
      4. Copy the existing Node-RED flow
      5. Configure the flow
      6. Run the flow

The YouTube video below gives you a 13 min more detailed introduction to the Twitter-follower-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 of 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 following image you see the full flow. The green arrows showing the invocation of the Node-RED endpoints, from the automated flow to get the Twitter follower list.

twitter-follower-flow


Step 1: Setup Twitter developer application

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

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:

configure-twitter-auth

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.

install-node


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.

import-flow


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.

configure-twitter-auth

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.

configure-http-node

That image shows HTTP requests nodes to be change.

request-automation-followers-setup


Step 6: Introduction to the Node-RED flow

The gifs shows a execution of the Twitter-follower-flow:

 

twitter-follower-flow-execution

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


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

Greetings,

Thomas

PS:  You can try out Cloud Foundry Apps or Kubernetes on IBM Cloud. By the way, you can use the IBM Cloud for free, if you simply create an IBM Lite account. Here you only need an e-mail address.

#IBMDeveloper, #IBMCloud, #Node-RED, #REST, #Endpoints, #HTTP, #TwitterAPI, #Node-RED-flow, #hackathon

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.