DIY Twitter Bot: Automate your twitter

I had been planning to develop a product related to social media and as an incremental step towards it, I decided to make a Twitter bot.
In this tutorial article, I will be guiding you on how to make a twitter bot in Node Js which will be able to retrieve tweets based on hashtags and retweet them on the scheduled period. If this is what you want to learn today then stick on otherwise share to the one whom it concerns.
Before you start this i hope you have created a twitter developer account and created an app.
Step 1: Directory and File Setup
Let’s create our project directory or folder and change directory to that using the following command in terminal:
mkdir TwitterBot
cd TwitterBotAfter that initialize package in that directory using
npm initonce done create these three files into it as shown in the below
TwitterBot
|---index.js
|---twitterConfig.js
|---debug.logStep 2: Install Dependency
We will be using an npm module called twit. Go ahead and install it with the following command
npm install -s twitStep 3: Add configuration
Now in twitterConfig.js we will create our configuration element like consumer key & secret, access token & secret as highlighted below
//twitterConfig.jsmodule.exports = {
consumer_key: “ ”,
consumer_secret: “ ”,
access_token: “ ”,
access_token_secret: “ ”
};
you must have got these keys, secret and token from your app in twitter developer account.
Step 4: Code in index.js
Let’s start by making twit and twitterConfig.js available to our entry file index.js and creating an instance of a twit,
var twit = require(“twit”);
var config = require(“./twitterConfig.js”);
var T = new twit(config);Let’s create a function which will retrieve all tweets based on hashtag and i will call it getTweets and add code as shown below.
function getTweets() { //params which we will pass to search api
var params = {
q: "#freeCodeCamp",
result_type: "recent",
lang: "en"
}; //search tweets api
T.get("search/tweets", params, function(err, data, response) {
if (response.statusCode == 200) { //mapping over array of tweet recieved
data.statuses.map((ele, i) => { //delaying execution of retweet
setTimeout(() => { //retweet function
reTweet(ele.id_str);
}, i * 4 * 60000); });
}
});}
In the above snippet, we call search/tweets API and withparams as the parameter to that. Once API call is successful we go through an array of tweets in data.statuses and delay execution of retweet()to a certain time. I will let you figure out the time delay thing.
Next, we will go ahead and write the retweet method as shown below,
function reTweet(tweetId) { //api for retweet
T.post(
"statuses/retweet/:id",
{ id: tweetId }, function(err, data, response) {
console.log("succesfully retweed a tweet");
});}
In the above snippet, we get tweetId as a parameter and we use that for calling statuses/retweet/:id API to retweet and on successful call we will console succesfully retweed a tweet.
now the final portion,
Let’s create our init method as shown below,
function init() {
//function which need to be run on start of script
getTweets();
}so finally we are done with it.
Wait!, also create a script in your package.json for running it,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},once you are done go ahead and run it with the npm start command and you will see your first retweet from the bot.
Playaround, Tweak it, break it!
Happy Hacking!
If you like the article give some claps (you can give up to 50) and follow me on twitter at goodwillsandy.
I have added the GitHub repo of code with extra functionality of logger, check it out at this link https://github.com/goodwillsandy/twitterbot
Pick a tech stack that you enjoy and which matches your goals, and focus on becoming an expert in that.
