Techidea12 is a tech blog with updated tech information

Breaking

Facebook posts may help predict depression risk: Study

Saturday, December 16, 2017

How to make a twitter bot easily with 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 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 than 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 a important part, enter the main file name for your bot. node.js execution start 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 -- ente your name here
licence-- enter
after all this you will be asked if those information are ok or not, type yes to save.
Now we are gonna install our twitter api client, there are 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 from twitter, for your bot to tweet behalf of you.

Go to twitter application management site, there you can see a option of 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 your will be initialize 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 successfull, and if everything went good you're gonna get "It worked" message on your console.

You're not just limited to posting 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. Its absolutly free. Leave a comment if you want me write a tutorial how you can deploy your node.js app on heroku for free. Thank you.

No comments:

Post a Comment