The Making of a Twitter Bot

Here's a quick introduction/how-to on making a Twitter bot. I had to do this recently, and figured I would document the process for those who needed a reference. I'll be using Node.js to make my bot.

Step 0

I'm assuming you already have a recent version of Node.js and npm (node package manager) installed.

Step 1 - Make the Twitter account

First, you'll need to sign up for a Twitter account for the bot. This is a straight-forward process. You'll have to verify your account.

Step 2 - Apply for a Developer account

Go to developer.twitter.com and apply for a developer account. I was approved right away. Choose the making a bot option for what you want to do.

Screenshot from 2019-06-07 12-13-55.png Select the "making a bot" option here.

On the next screen, the form will ask you to verify some basic details, and agree to the terms and conditions.

Once you fill out this form, you should be immediately approved, and see the next screen:

Screenshot from 2019-06-07 12-17-06.png Select the "Create an app" option

Step 3 - Make an App

From the console, select the option to create an app. From there, the only bits we need to fill out here are the required ones, a name, a website, and a description of your bot. There's also an internal description at the end of the form.

Screenshot from 2019-06-07 12-17-31.png Fill in the fields marked required

Step 4 - Generate your API keys

Once the app is created, navigate to the "Keys and Tokens" tab and generate a access token. You should also see your consumer token there. These keys will be necessary later for making the bot.

Screenshot from 2019-06-20 10-23-40.png This screen shows your access tokens and consumer keys

Step 5 - Install Twit

Node.js has a handy package, called twit to help us post to Twitter. Run

# initialize your package if you haven't
npm init 
# install twit
npm install --save twit

Step 6 - Write the Script

Now it's time for the magic

In a JavaScript file, say app.js place the following:

const Twit = require('twit');
const T = new Twit({
// copy these from your Twitter app you just made and paste them here
  access_token: '09u390-jfwj-293jr-9j2rf-9]', // fake data - obviously
  access_token_secret: '2309uoijf0j2f409j240f9j02994f',
  consumer_key: 'afjh08j320n2fo9uj2fomj0urf',
  consumer_secret: '0sef9uifsws0f9j-9j0rwef'
});
T.post('statuses/update', {
  status: "Hello World!"
}, (err, data) => console.log(data));
// that's it!

Then type

node app.js

Then navigate to Twitter and see your first post! :)