Making twitter bot using node.js

Hello there, Building your own Twitter bot is easier than you think using node.js
So let’s start, all you need for this tutorial is little knowledge about javascript.
First, download and install node.js, if you’re in Linux then you better use NVM (Node version manager) to install node.js, because we need the latest version of the node to make our bot work. don’t forget to install npm also.
okay, so let’s get into the code.

make a folder with the name of your bot. get into that folder and execute this command: npm init
 This command will initialize a package.json file in your folder with the info about bot and dependencies that your bot need to run.
After running the above command you will see 

package name: (folder_name) —-Enter the name of your bot here, if you want to keep the name that you’ve given to the folder then simply press enter.

version: (1.0.0)  — This is the next thing you will see, we have nothing to do with it, just press enter

description: Enter the description of your bot if you want or else you can press enter to skip

entry point (index.js)– This is an important part, enter the main file name for your bot. node.js execution starts from this file.

test command– we’ve nothing to do with this step, press enter

git repository — press enter
keywords– you can enter some keyword that bot contains
author — enter your name here
license– enter
after all this you will be asked if that information is ok or not, type yes to save.
Now we are gonna install our twitter API client, there are a lot of API clients out there, but my personal favorite is Twit package. Here is the GitHub link for the package.
let’s install it with the command npm install twit.

once you’re done with your installing, Create a config.js file within your folder
and copy this code there:

module.exports = {
consumer_key:  ‘….’,
consumer_secret: ‘….’,
access_token: ‘….’,
access_token_secret:’…’,
}
These are the credentials you need to take on Twitter, for your bot to tweet behalf of you.

Go to twitter application management site, there you can see an option to create new app, create your app with the name of your bot and

edit the index.js or whatever you’ve named your file as entry point file,

first, at the starting, you should print a message whenever you will be initialized for the first time.
console.log(“Bot starting”);

var Twit = require(‘twit’); 
//importing the twit package

var config = require(‘./config’); 

//importing config file where all credentials are stored

var T = new Twit(config); 
//Creating new instance of twit package with our configuration file

  tweetIt();

//Tweeting first time whenever bot started and then it will take interval of specified time

setInterval(tweetIt, 1000*60*60*4); 
//Here I specified 4 hour of interval, so it’s gonna twit every 4 hour

 tweet: {
   status: ‘first tweet from node.js’
}

T.post(‘statuses/update’, tweet, tweetIt);

function tweetIt(err, data, response){
if(err){
console.log(“Something went wrong”);
}
else{
console.log(“It worked”);
}


As per the above tweetIt function you see, it gonna print Something went wrong, if tweeting is not successful, and if everything went well you’re gonna get “It worked” message on your console.

You’re not just limited to posting a tweet, you can do all sort of stuff, that you could do with the Twitter web site. to get more information visit the GitHub repository Here
To deploy your node.js online you have to register for a Heroku account. It’s absolutely free. Leave a comment if you want me to write a tutorial how you can deploy your node.js app on Heroku for free. Thank you.

Leave a Reply

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