How To Make A Twitter Bot With Node JS For Beginners – PART 1

How To Make A Twitter Bot With Node JS For Beginners – PART 1

Hello folks I am Aditya. I develop Chat Bots, mostly on Messenger and Twitter Bot.

In this tutorial we’ll code our Twitter Bot in JavaScript and then run it with Node JS. Don’t worry if you aren’t an expert in JavaScript. I have tried my best to be as detailed as possible and I hope you won’t have any trouble following along.

Before we start writing any code let’s first set up all these requirements

  • A Twitter Account and a Twitter App
  • A command line interface which you probably already have (Command Prompt, Terminal …)
  • A text editor to write the codes (Sublime Text recommended)
  • Installing and Setting up Node JS ( A Javascript runtime )

NOTE: If you want to host it on a web server then you will be needing a Heroku account. Heroku lets you host your Twitter Bot online for free. But for the sake of simplicity let’s not talk about it just yet.

1. CREATING A TWITTER APP

You can’t create a bot without a twitter account. Go create one here.

Awesome, you have a fresh new Twitter account. Now we’ll be needing something called API keys. API keys are the ‘username and password’ for Bots. Your Bot will require this to make API calls, or in simple words, interact with the data on Twitter’s server.

To get your API keys, you need to first create a Twitter app. Go to apps.twitter.com to get this started.

Create new Twitter application
Create new Twitter application

Click on ‘Create New app’. Then on the next page fill up all the details. If you don’t have a website just put a placeholder in the Website field. You can leave out the Callback URL field. Tick the Developer Agreement and then finally click on ‘Create Your Twitter application’ at the bottom.

Creating a Twitter Application for the Twitter Bot

We now have a Twitter application. Let’s get the API keys.

On the next page go to ‘Key and access Tokens‘ tab. We’ll need four different key from here out of which 2  (Consumer Key and Consumer Secret) are already created. We need to generate a thing called “access tokens”. On the bottom of the page, click on the “Create my access tokens”.

Creating Access Tokens

Now you shall have the following information:

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

Never ever share your keys. I have deleted this app so don’t try doing anything stupid with it.

Now we need to create a new file called config.js. Open your text editor and make a JavaScript object exactly like this filling up the necessary information. Do not modify the parameters. For example : Do not change consumer_key to Consumer Key. It should be exactly as shown in the image below.

Config.js file to store our API Keys and Tokens

By the way, you can get the code on my Github page here. But I highly recommend typing the code rather than copying.

2. Installing and Setting up Node JS

Now, let’s install Node JS. The instructions will vary a bit depending on your operating system, so just follow the official download and installation guide it’s pretty simple.

Once you have Node JS installed, fire up your command line and type node --version. If you get something like ‘v6.10.3‘ then your installation was successful. The number you get back is the version of Node.

Node JS comes with NPM (Node Package Manger) with which you can download Node Modules. Node modules are simply JavaScript libraries. Don’t worry about it right now. Once again in your command line, type npm --version. You should get a back a number like before.

If you get an error during this process please re-install Node. Great! We now have all the requirements fulfilled.

3. Setting Up The Project

Hold your horses folks. Few more minutes and then we’ll get into coding. Lets set up our project first. This process takes about a minute or so.

Create a folder to store all your bot files. Inside the Folder you should have the config.js file we created earlier. Create another file called bot.js (you can name it anything you want but it should have .js extension). This is the file where we’ll be writing all our codes.

Finally open your command line and then navigate to this folder. Then type the following command

npm init

This will set up our package.json file which will contain all the metadata of our project like project name, project version, licence and most importantly the dependencies. Dependencies are the node modules that we use in our program.

Creating a package.json file with npm init
Creating a package.json file with npm init

It’s not necessary to fill the details. In fact you can leave everything blank. Just press enter to skip the field.

Finally you should have a package.json file created on your main folder. You folder should have 3 files by now

  • config.js
  • bot.js
  • package.json

4. Installing Twit Module

We’ll be using a Node Module called Twit. This module helps us communicate with Twitter Server using our API keys. This is the only module we’ll need for our Twitter Bot.

Open your terminal and navigate to the main folder. Run the following command

npm install --save twit

This will install the Twit Node Module. If you check your package.json file you should see Twit in your dependencies. Every time you install a node module it’ll automatically be added to your package.json file. But it’s necessary that on your command line you must be on the same path where you want to install the module.


I think I’ll need to divide this tutorial into 3 parts because its already getting lengthy.

Part 2 : Writing The code
Part 3 : Hosting Twitter Bot on Heroku

About the Author