Recent Blogs

How to Build a Twitter Bot With Node.js

How is Learning Management Systems Revolutionizing the Education Sector?

I’ve created a Node.js Twitter bot! I am very excited to share all the details with you on how to create a Twitter bot using Node.js.

How to create a Twitter Bot using Node.js

The rest of this article will lay-out in a below-mentioned way:

What does this bot will do?

This is an easy Twitter bot and can retweet, like/favorite, randomly based on hashtags as a query. And we will use and proceed to do. So after some period of the time interval.

What do you need?

You should have Node.js installed on your PC/laptop.

A Twitter accounts.

Your bot will use twit, that is an npm module to manipulate tweets and streams. And also to converse with the Twitter API.

Let’s get Started

Configure an empty directory and initialize it with:$ npm init to configure this web app with package.json file. After that, build two new files: bot.js & config.js in the directory.

bot.js will be our primary application file in which we write the source code of our Twitter Bot. And then in package.json edit the primary field to:

“main”: “bot.js”, 

},

Your current directory structure must look like this:

root/project-name

|- bot.js

|- config.js

|- package.json

Configuring and giving permissions from Twitter API

After logging to your account, follow to this link: https://apps.twitter.com/app/new to build a new app. Fill out the required section in the form and click the button Create Your Twitter Application. Once you created the app, look for ‘Keys and Access Tokens’ under the nav-panes. After that, click on ‘Generate Token Actions` and click the copy:

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

Now, open the config.js file and paste all four values inside it. Thus, expose these values using module.export:

//config.js

/** TWITTER APP CONFIGURATION

* consumer_key

* consumer_secret

* access_token

* access_token_secret

*/module.exports = {

consumer_key: ”, 

consumer_secret: ”,

access_token: ”, 

access_token_secret: ”

}

Now, you have completed the one-step of Twitter bot’s configuration. Please note, for each app, the consumer secret, consumer key, access token, and access_token_secret will vary.

Creating the bot

So, once you’ve finished the configuration step, now let’s install the third requisite. It is the Twitter API client for node. And will help us to converse to Twitter API. Also, this gives an API for all required activities (for example, retweet and favorite a tweet).

We will begin by installing the dependency we want for our app.

$ npm install –save twit

After the dependency has completed the installation, go to the bot.js file. And put the dependency and config.js file.

var twit = require(’twit’);

var config = require(’./config.js’);

Now, pass the configuration (access tokens and consumers) of our Twitter app in config.js to twit:

var Twitter = new twit(config);

So far, so great?

PLEASE NOTE: You should refer to twit documentation for a profound reference.

Retweet Bot

Let’s write a feature expression that discovers the latest tweets as per the query passed as a parameter. Also, we will initialize a params object, which will hold many properties to discover a tweet. But, most essentially, a query or a property that will refine our searches. Whatever value you provide in this property, our bot will search the tweets to retweet because of these criteria. Moreover, you can put these property values like a twitter handler. To check a particular twitter account or a #hashtag. For our example bot, we’ve discovered the most recent tweets on #nodejs.

This is how the features of the retweet bot begin:

var retweet = function() {

var params = {

q: ‘#nodejs, #Nodejs’,

result_type: ‘recent’,

lang: ‘en’ 

The other two properties: result_type and lang are alternatives. On defining the result_type: ‘recent’ alerts the bot to search for the recent tweets. And tweets that have appeared in the time. Since our bot has begun, or it has created the last retweet.

Our next step is to discover the tweets based on our criteria. For this, we will use Twitter. get features offered by twit API to GET any REST API endpoints. Basically, the REST API endpoint is a reference to the Twitter API endpoint. We will make a call to discover tweets. The Twitter.get feature acknowledges three arguments: API endpoint, params object (defined by us) and a callback.

Well, to post or to retweet the tweet our bot has discovered we use Twitter.post() approach to POST any of the REST API endpoints. Additionally, it also takes a similar number of arguments as Twitter.get().

Presently, to automate this activity we’ve defined above, we may use JavaScript’s timer feature setInterval() to locate and retweet after a specific period.

// grab & retweet as soon as the program is running…

retweet();

// retweet in every 50 minutes

setInterval(retweet, 3000000);

Please note that all JavaScript’s Timer features take the turnaround time argument in the milliseconds.

Favorite Bot

Same as retweet bot we can also define and initialize another feature expression. That will discover and favorite a tweet randomly. Indeed, the distinction here is to discover and grab the tweet randomly. Furthermore, we will begin by building a parameter object params that will comprise three major properties as in retweet() function expression. 

Also, the bot will find for tweets using the similar .get() function offered by twit API to GET any Twitter API endpoints. In my case, we want to search/tweets. After that, we will save the status of the search for the tweet to favorite in a variable. 

And, also in another variable, we will apply the random feature bypassing the “status of the search” variable as an argument.

Note that the tweets discovered by our bot are all saved in an array. Thus, we use JavaScript’s timer feature setInterval()to discover and favorite the tweet after a particular period in milliseconds.

The whole module: bot.js :

Usage

To run this bot, visit your terminal:

$ node bot.js

To avoid this monotonous procedure, you may use npm scripts or nodemon. Additionally, you can deploy this application on Heroku for a regular integration.

To use npm scripts, make the edit under scripts in package.json :

{

“scripts”: { 

“start”: “node bot.js”, 

}

}

Then from the terminal:

$ npm start

Basically, there are numerous ways to build a Twitter Bot. And this is just one approach. So, your bot can be intelligent, and you can do lots of things with it. Hence, you only need to refer to twit documentation for other RESTful API ways to deploy Twitter API endpoint.

Share this