Twitter bot under 50 lines using Javascript

My latest project started with the decision to repurpose one of my testing repos as documentation for how to use the npm twit package. I assume that you have installed node and npm installed on your system if not kindly refer to this .

Before we get started, all of the code for this tutorial can be found in my GitHub Repository. Fork the repository ;)

Let’s get started

This article is meant as a reference for me and anyone else that’s interested in Twitter bots in JavaScript.

  1. Fork the repository and copy the github link of the repository from your account ,then

git clone

Step 1 animation

2. Move into the repository and type npm install twit . This installs some code to the npm_modules subdirectory, which you don't need to worry about. (It's Twit, the library that lets us talk to Twitter.

Connecting to Twitter

3. At this point you need to register a Twitter account and also get its “app info”.

So create a Twitter account for whatever account you want to tweet this stuff. Twitter doesn’t allow you to register multiple twitter accounts on the same email address. I recommend you create a brand new email address (perhaps using Gmail) for the Twitter account. Once you register the account to that email address, wait for the confirmation email. Then go here and log in as the Twitter account for your bot:

Once you’re there, fill in the required fields: name, description, website. None of it really matters at all to your actual app, it’s just for Twitter’s information. Do the captcha and submit.

Next you’ll see a screen with a “Details” tab. Click on the “Settings” tab and under “Application Type” choose “Read and Write”, then hit the update button at the bottom.

4. Then go to the Keys and Access Tokens tab, and at the bottom click “create my access token”. Nothing might happen immediately. Wait a minute and reload the page. then there should be “access token” and “access token secret”, which are both long strings of letters and numbers.

Now use a text editor to open up the “config.js” file. It should look like this:

module.exports = {
consumer_key: 'blah',
consumer_secret: 'blah',
access_token: 'blah',
access_token_secret: 'blah'

In between those quotes, instead of 'blah', paste the appropriate info from the Details page. This is essentially the login information for the app.

5. Now type the following in the command line in your project directory:

node bot.js

Hopefully at this point you see a message like “Success! Check your bot, it should have retweeted something.” Check the Twitter account for your bot, and it should have retweeted a tweet with the #machinelearning hashtag.

Final words

You can check out my bot on Twitter, and you can find the code on GitHub. If you need help in customizing your Twitter bot, or are trying to build a more complex Twitter bot, contact me via Twitter.

❤ If this post was helpful, please hit the little green heart!