How to Create a Twitter Bot with Node.js

By  on  

Twitter bots have been in the news over the past few years due to election meddling, not only in the United States but stretching across the globe.  There are, however, good and logical reasons for creating Twitter bots.  In order to see how easy it was to create a Twitter bot, for good or evil, I decided to create my own Twitter bot.  Five minutes of work and I had a working bot -- let's see how it's done!

The first step in creating a Node.js Twitter bot is creating an app on the Twitter website:

Provide the required information and you'll have the ability to create access token and consumer information.

The next step is downloading the twit Node.js resource:

yarn install twit

With twit available, create an instance of Twit with the access token consumer information you were given by the Twitter app website:

const Twit = require('twit')

const T = new Twit({
  consumer_key:         'YOUR_INFO_HERE',
  consumer_secret:      'YOUR_INFO_HERE',
  access_token:         'YOUR_INFO_HERE',
  access_token_secret:  'YOUR_INFO_HERE',
  timeout_ms:           60 * 1000,
});

Now the action can happen.  Here are a few examples of basic Twitter bot functionality:

// Post a tweet
T.post(
  'statuses/update',
  { status: 'This is an automated test!' },
  (err, data, response) => {
    console.log(err, data, response);
  }
)

// Retweet a given tweet
T.post('statuses/retweet/:id', { id: '697162548957700096' })

Let's think of a more practical example:  using the Stream API to "like" any tweet you are mentioned in:

const stream = T.stream('statuses/filter', { track: ['@davidwalshblog'] });

stream.on('tweet',
  tweet => {
    console.log('tweet received! ', tweet)
    T.post(
      'statuses/retweet/:id',
      { id: tweet.id },
      (err, data, response) => {
        console.log(err, data, response);
      }
    )
  }
);

Getting a Twitter bot up and running takes minimal effort, which is why it's important that services like Twitter protect its users from evil-doers.  Bad guys aside, there are plenty of good reasons to create a Twitter bot, whether it be for internal analytics, promotion, or even creating your own Twitter app.  Thank you to Tolga Tezel for creating an amazing JavaScript resources for interacting with Twitter!

Recent Features

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    WebKit-Specific Style:  -webkit-appearance

    I was recently scoping out the horrid source code of the Google homepage when I noticed the "Google Search" and "I'm Feeling Lucky" buttons had a style definition I hadn't seen before:  -webkit-appearance.  The value assigned to the style was "push-button."  They are buttons so that...

  • By
    AJAX Page Loads Using MooTools Fx.Explode

    Note: All credit for Fx.Explode goes to Jan Kassens. One of the awesome pieces of code in MooTools Core Developer Jan Kassens' sandbox is his Fx.Explode functionality. When you click on any of the designated Fx.Explode elements, the elements "explode" off of the...

Discussion

  1. Hi David,

    This is a great introduction, however I must say that coming up with useful ways to utilize the Twitter API with these bots is the harder part. I suppose if one truly has a use for making a bot like this then that part will be self-evident.

    Thanks again for the write up.

  2. Doug

    damn if you’re a beginner. NONE of this will just WORKS like that.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!