How about a Twitter Bot that retweets, favorites, on the basis of hashtags and replies to other users if they follow it? I made a similar kind of a Twitter Bot (@ ) that feeds me the latest or the ongoing news/articles/how-to’s on a set of hashtags such as #Nodejs, #MongoDB, #AngularJS, #IonicFramework, et cetera. At the time I never expected it having more followers than me but that has been surpassed. nodejstweet A glimpse of a Twitter Bot that I made for sole purpose of information What this bot will do? This is a simple Twitter bot and will retweet, favorite/like randomly on the basis of hashtags as a query that we will use and continue to do so after a certain period of time interval. What you need? You must have installed on your system. Node.js A Twitter Account. Your bot will be using which is an npm module to manipulate tweets and streams, and to communicate with the . [twit](https://www.npmjs.com/package/twit) Twitter API Let’s Start Setup an empty directory and initialise it with: to configure this web application with file. Then create two new files: & in that directory. $ npm init package.json bot.js config.js will be our main app file in which we will be writing the source code of our Twitter Bot, and so in edit the field to: bot.js package.json main {"main": "bot.js",}, Your current directory structure should look like this: root/project-name|- bot.js|- config.js|- package.json Configuring and granting permissions from Twitter API After logging to to your Twitter account, follow to this link: to create a new application. Fill out the necessary fields in the form click on the button After creating the application, look for ‘Keys and Access Tokens’ under the nav-panes and click on ‘Generate Token Actions` and then copy: https://apps.twitter.com/app/new Create Your Twitter Application. Consumer Key Consumer Secret Access Token Access Token Secret Open the file and paste all four values inside it. Expose those values using : config.js module.export //config.js/** TWITTER APP CONFIGURATION* consumer_key* consumer_secret* access_token* access_token_secret*/ module.exports = {consumer_key: '',consumer_secret: '',access_token: '',access_token_secret: ''} Now, the Twitter bot’s configuration is step is complete. for every different application, the , , and will differ. Please note, consumer key consumer secret access_token access_token_secret Building the bot Since the configuration step is complete, now let’s install our third requisite that is and will help us to communicate to Twitter API and provide an API for all necessary actions Twitter API client for node (such as retweet and favorite a tweet). We will start by installing the dependency we need for our application. $ npm install --save twit After the dependency has finished installing, go to the file and require the dependency and file. bot.js config.js var twit = require(’twit’);var config = require(’./config.js’); Pass the configuration ( ) of our Twitter application in to consumer and access tokens config.js twit: var Twitter = new twit(config); So far, so good? PLEASE NOTE: You must refer to [**twit**](https://www.npmjs.com/package/twit) documentation for a deep reference. Retweet Bot Let’s write a function expression that finds the latest tweets according to the query passed as a parameter. We will initialise a object that will hold various properties to search a tweet, but most importantly or property that will refine our searches. Whatever value you feed in this property, our bot will search the tweets to retweet based on this criteria. You can feed this property values like a twitter handler, to monitor a specific twitter account or a #hashtag. For our example bot, we have find latest tweets on #nodejs. params query q This is how the functionality of the retweet bot starts: var retweet = function() {var params = {q: '#nodejs, #Nodejs',result_type: 'recent',lang: 'en'} The other two properties: and are optional. On defining the notifies bot to only search for the latest tweets, tweets that have occurred in the time period since our bot has started or it made the last retweet. result_type lang result_type: 'recent' . There is a list of parameters provided by the Twitter API Our next step is to search for the tweets based on our parameters. For this, we will use function provided by API to GET any of the REST API endpoints. The REST API endpoint is a reference to the T we are going to make a call to search for tweets. The function accepts three arguments: API endpoint, params object (defined by us) and a callback. Twitter.get twit witter API endpoint Twitter.get To post or to retweet the tweet our bot has found we use method to . It also takes the same number of arguments as . Twitter.post() POST any of the REST API endpoints Twitter.get() Now to automate this action we defined above, we can use JavaScript’s timer function to search and retweet after a specific period of time. setInterval() // grab & retweet as soon as program is running...retweet();// retweet in every 50 minutessetInterval(retweet, 3000000); Please note that all JavaScript’s Timer functions take the argument in milliseconds. amount of time Favorite Bot Similar to bot we can define and initialise another function expression that will search and a tweet randomly. Yes, the difference here is to search and grab the tweet randomly. We will start by creating a parameter object that will consist of three properties as in function expression. The bot will search for tweets using the same function provided by API to GET any of the Twitter API endpoints. In our case, we need . Then we will store the status of the search for tweet to in a variable and in a another variable we will apply the random function by passing the “status of the search” variable as an argument. retweet favorite params retweet() .get() twit search/tweets favorite Note that the tweets searched by our bot are all stored in an array. Again, we use JavaScript’s timer function to search and favorite the tweet after a specific period of time in milliseconds. setInterval() The complete module: : bot.js Usage To run this bot, go to your terminal: $ node bot.js To avoid this monotonous process you can use or You can also deploy this app on for a continuous integration. npm scripts nodemon. Heroku To use npm scripts, make this edit under in : scripts package.json {"scripts": {"start": "node bot.js",}} Then from terminal: $ npm start There are various ways to write a Twitter Bot, this is just one way. Your bot can be smart and you can do various things with it. You just have to refer to for other RESTful API methods to manipulate . [twit](https://www.npmjs.com/package/twit) documentation Twitter API endpoints For further reading check out for various types of bots on vast amount of platforms. For advanced reading, check out . Botwiki.org Botwiki’s list of tutorials of Twitter Bots in different programming languages If you have come so far, and you enjoyed it reading, please hit the💚 button. Want to receive more articles like this one? Subscribe me here . Sometimes, I send “never seen before” content to my subscribers. I hope this was helpful! See you on . Twitter