Build a simple Twitter Bot with Node.js in just 38 lines of code

  • Check out my Top Three Node.js courses.Twitter NPM: An NPM module that allows to easily interact with the Twitter API.Create an ApplicationFirst thing’s first: The only reason this bot will work is because of the Twitter API.
  • In this application, the API will allow us to search for tweets, and favorite the tweets that our application finds.To use the Twitter API, we need some information!
  • { “name”: “twitter-bot”, “version”: “1.0.0”, “description”: “Nodejs Twitter Bot”, “main”: “app.js”, “scripts”: { “test”: “echo \”Error: no test specified\” exit 1″ }, “repository”: { “type”: “git”, “url”: }, “author”: “Brandon Morelli”, “license”: “ISC”, “bugs”: { “url”: }, “homepage”: Next, we’ll save our one needed dependency by running:npm install –save twitter4.
  • /config.js’);var T = new Twitter(config);// Set up your search parametersvar params = { q: ‘#nodejs’, count: 10, result_type: ‘recent’, lang: ‘en’}q is the only required parameter and it stores our search query.
  • err){ // This is where the magic will happen } else { console.log(err); }})To gain a deep understanding of what is happening here, you’ll need to read the Twitter API Documentation on GET search/tweets, but I’ll try and provide a basic introduction:With the help of the npm Twitter module, we are able to make a get request to ‘search/tweets’ and pass in our search params we set previously.

Tutorials don’t have to be complicated. Together we’ll build a simple Twitter favorite bot with Node.js in just 38 lines of code. This is the first post in a multi-part series. In future posts, I’ll…

@simonsmadsen: Build a Twitter bot with just 38 lines of code in @nodejs

#Javascript #NodeJS #reactjs #angularjs #angular2

Build a simple Twitter Bot with Node.js in just 38 lines of code

Tutorials don’t have to be complicated. Together we’ll build a simple Twitter favorite bot with Node.js in just 38 lines of code.

This is the first post in a multi-part series. In future posts, I’ll show you how to add more advanced functionality to our bot. For now, we’re going to build a very simple Twitter bot that favorites anyone who tweets about a certain subject.

Before we get started, all of the code for this tutorial can be found in my GitHub Repository

Ready? Let’s do it. Here’s what we’re going to need:

First thing’s first: The only reason this bot will work is because of the Twitter API. On the most basic of levels and API is simply a way for one program to interact with another. We’ll be creating a Node.js application that interacts with the Twitter API. In this application, the API will allow us to search for tweets, and favorite the tweets that our application finds.

To use the Twitter API, we need some information! Head on over to the Twitter API and create a new application.

You’ll have to fill out some information, then look for the following information under the Keys and Access Tokens tab. You may have to click Generate Access Token to create your access token.

Once you see these four pieces of information, you’re ready to move on to the next section.

Now that we have the application information we need, lets set up our bot.

npm init

2. Fill out the required information to initialize our project.

file looked like after initializing my project. Note: Yours may look slightly different, that’s OK.

“name”: “twitter-bot”,

“version”: “1.0.0”,

“description”: “Nodejs Twitter Bot”,

“main”: “app.js”,

“scripts”: {

“test”: “echo \”Error: no test specified\” && exit 1″

“repository”: {

“type”: “git”,

“url”: “git+https://github.com/bmorelli25/Twitter-Bot.git”

“author”: “Brandon Morelli”,

“license”: “ISC”,

“bugs”: {

“url”: “https://github.com/bmorelli25/Twitter-Bot/issues”

“homepage”: “https://github.com/bmorelli25/Twitter-Bot#readme”

3. Next, we’ll save our one needed dependency by running:

npm install –save twitter

file will contain our twitter bot code.

file. Replace each of the blanks below with your corresponding app data that you got from Twitter.

// config.js

module.exports = {

consumer_key: ”,

consumer_secret: ”,

access_token_key: ”,

access_token_secret: ”

file:

// app.js

var Twitter = require(‘twitter’);

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

. This npm module simplifies the process of interacting with Twitter’s API. Now we can make get and post requests to the API which is what allows us to ‘do things’ on Twitter via our bot!

Build the Bot

Remember, we’re starting very simple with this tutorial. Let’s visualize the flow of favoriting a tweet: There are two parts. First, you search for Tweets, then you select one and favorite it. Lets work on the search functionality first:

variable which will house our search parameters. There are a lot of parameters we can use. You can see the full list on Twitter’s API documentation here.

var Twitter = require(‘twitter’);

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

var T = new Twitter(config);

// Set up your search parameters

var params = {

q: ‘#nodejs’,

count: 10,

result_type: ‘recent’,

lang: ‘en’

is the only required parameter and it stores our search query. In my example we’re searching for tweets containing #nodejs. Feel free to change this to whatever query you’d like.

I also use three optional search parameters:

 — specify the number of tweets you’d like to return

 — returns only the most recent results

 — returns only English results

Now that we have our search parameters we plug them into a get request so we can find some tweets!

T.get(‘search/tweets’, params, function(err, data, response) {

if(!err){

// This is where the magic will happen

} else {

console.log(err);

To gain a deep understanding of what is happening here, you’ll need to read the Twitter API Documentation on GET search/tweets, but I’ll try and provide a basic introduction:

we set previously. This get request returns a callback. If there is no error in this callback, we will run our code to favorite a tweet. If there is an error, we will log the error and be done.

statement above!

object. So we need to loop through all of the tweets in that array and favorite each one individually. Here’s how we do that:

// Loop through the returned tweets

for(let i = 0; i < data.statuses.length; i++){ // Get the tweet Id from the returned data let id = { id: data.statuses[i].id_str } // Try to Favorite the selected Tweet T.post(‘favorites/create’, id, function(err, response){ // If the favorite fails, log the error message if(err){ console.log(err[0].message); // If the favorite is successful, log the url of the tweet else{ let username = response.user.screen_name; let tweetId = response.id_str; console.log(‘Favorited: ‘, `https://twitter.com/${username}/status/${tweetId}` ) That’s it! Watch in glory as your bot favorites and logs each tweet url out! file looks like. I also have all the code on my GitHub, so head over there if you want a way to easily download and play with the bot! Here’s a list of possible enhancements and additions we can make to our twitter bot. What do you want to learn? Tweet me @BrandonMorelli to let me know and I’ll cover it in Part 2 of this series!

Build a simple Twitter Bot with Node.js in just 38 lines of code

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.