How to: Build a Twitter bot with Node.js – fast!

comment 1
English / Tutorials

Building a Twitter bot with Node.js is easy these days (if you have at least some web development & JavaScript knowledge). Get ready with me and set up your own one in just a few minutes.

At first you will need to register a (new) Twitter account specifiying the screen name, through which you’d like to tweet into the world. After that you are ready to apply for a developer’s account and add a new Twitter developer’s app here. This is necessary for generating keys and tokens for accessing the official Twitter API. Twitter asks for several information about what you want to do with your app. Writing down what the bot is all about went just fine for me. Finally it’s time for coding. Make sure node and npm are installed on your computer with the following commands.

node -v
npm -v

After that, we are ready to setup a working folder and init a npm project with:

npm init

The only library that is mandatory for our project is the npm package twitter-lite, a tiny, full-featured, modern client / server library for the Twitter API, whose documentation you will find here. It was built because existing ones have not been recently maintained, or depend on outdated libraries. Within our working folder we create a Node.js script, which could be named “bot.js”. 

const Twitter = require("twitter-lite");

const client = new Twitter({
  consumer_key: 'xyz',
  consumer_secret: 'xyz',
  access_token_key: 'xyz',
  access_token_secret: 'xyz'
});

This creates a new authorized Twitter API client. All keys and secrets are available in your Twitter developer’s account. 

What should the bot do exactly? Well, for example, we could filter all incoming new tweets around the world with the help of keywords or hashtags by opening up the Twitter Stream and providing some parameters, like so:

 
const parameters = {
  language: "en",
  track:
    "#girlswhocode,#girlsintech,#womenintech,#womenwhocode,#momsintech,#momswhocode"
};

client
  .stream("statuses/filter", parameters)
  .on("start", response => console.log("start"))
  .on("data", async data => {
    console.log("got data", data.id_str);
  })
  .on("error", error => console.log("error", error))
  .on("end", response => console.log("end")); 

Instead of just logging filtered tweets we could now automatically retweet those tweets by adding:

const url = `statuses/retweet/${data.id_str}`;
await client.post(url);

That’s it! Just start your script with

node bot.js

and your bot starts firing. There are many more possibilities on how the Twitter API can be used. You would like to deploy this app? Sure, you could deploy on Heroku or use a hoster like DigitalOcean that could run your app in the background with pm2. Beware: Your secrets and keys should be stored in environment variables! A great library to do so is dot-env.

Have fun!

 

1 Comment

  1. OH, this is amazing I posted the previous comment not properly checking your site!! I will use this for my twitter!

Leave a Reply

Your email address will not be published. Required fields are marked *