DEV Community

Ashutosh Kumar Singh
Ashutosh Kumar Singh

Posted on • Originally published at Medium on

Create a simple yet effective Twitter BOt in nOdejs (javascript)

Create a simple yet effective Twitter BOt in nOdejs (javascript)

So let’s get started to make our very first twitter bot which will retweet, favorite, post, follow, unfollow people. In this post I am focused on only setting up your account and the retweet function, other functions will be in my next post. thanks enjoy

To get started you will need this development environment:

  1. Nodejs (npm) to download click here https://nodejs.org/en/ and install it

nodejs

  1. any code editor of your choice (I prefer VScode bcoz it has many great extensions for new developers to help

3.Twitter Developer Account —

if you don’t already have one ..don’t worry, it’s very easy to create first you should have a working twitter account or you could set up a new one for this tutorial purposes. Now go to https://developer.twitter.com/en/apply-for-access

Twitter Developer Account

apply for a new account, now I know that the process is a little boring and time consuming but just bear it and complete it.

Once you are setup go to https://developer.twitter.com/en/apps and create a new app, now the name of the app can be anything just complete the steps, it will ask a URL also you can give any URL like your Facebook or Gmail, it doesn’t matter.

It should look like this

Once your app is created, now you have the access tokens which will be used in authenticating your account in the code. Go to keys and tokens and generate an access token and access token secret, these four alphanumeric string thingies are the reason we went through so much trouble of making a developer account and app.

Now make a new file called config.js and in it paste these tokens and keys like

module.exports = {

consumer_key:’ ’,

consumer_secret:’ ’,

access_token:’ ’,

access_token_secret:’ ’

}

It should look like this but with your keys and token.

Now a mistake which I made a lot was when I was copying and pasting the keys and tokens, I left a space between the quotes to remember no spaces at any end It will give an error during authentication.

So now we have one file named config.js, we just need a bot.js (where we will do our coding) and a package.json.Open the folder where this file is located and make bot.js file.

So what are we waiting for

Go to your terminal or command line and open the folder where this config.js file is stored and write

npm init -y

make sure your main file is the bot.js

this will create a package.json file in that folder. Let us now install twit, the package we will use in our coding, again in the command line

npm install — — save twit

Now if you check your package.json file it should have the twit dependency.

Now we can finally start our real coding, the moment you all have been waiting for. Open bot.js

first, we will require the config file and twit module like shown below

see we have imported twit module, and in the next line, we are making an instance of this twit module.

Now the next step is using the get and post method, these are the most basic yet most important methods in twit module, there is also stream method but we will limit ourselves to get and post only, at least in this article.

So I want to retweet the tweets containing a specific parameter like it should be #ironman or #marvel or @edsheeran etc. so first we will make a function retweet and in that function, we will search the tweets according to the parameter using the get method and finally retweet those posts using post method.

this is the function that we will do all the magic

first, there are the params object that specifies all the parameters for our search, here q is the required one and the others are optional, you can find all the parameters in the Twitter API documentation(link below)

then we will search using

T.get(‘search/tweets’,parameters,call back function)

search/tweets, searches the tweets using the parameters and the callback back function gets initiated so that we can see if we get an error or response or data. Data is the thing we want because it contains all the different types of data, so I store it in a variable called tweets, you can see this data by writing

console.log(tweets);

You can play around this data see, the actual text in the tweet and many other things

But I only need id_str which is required in the post method of twit once I have stored all the data in tweets I use a loop, there are man different id_str and I want to use every one of them

now we have our id_str which is stored in variable retweetId and use it in post method which

T.post(‘statuses/retweet/:id’, {id: retweetId},callback function)

here statuses/retweet/:id is the element retweets using the id of the tweet which is stored in the retweetId variable. The callback function gets initiated and if it gives an error I print it in console or if I get a response I print retweet successful in the console.

At last, I call this function in setInterval so it keeps on initiating over a fixed interval, remember if you call the function too quickly twitter may think you are abusing the API and can terminate your account.

Now in the command line call the program by

node bot.js

and see the magic happens (only if there are no errors)

Alright, guys, this is all in the next article I will continue from here and will cover following, unfollowing people, making a tweet, favoriting a tweet, retweeting with a comment and also deploying to a cloud server so that you don’t have to call it again and again in the command line so wait for it . Happy Coding

If you have any problem or doubt, question feel free to comment and I will get back at you as soon as possible .

A nice way of writing above code is

also if there is any error in the code let me know in the comments.

my GitHub source code for the above is

lelouchB/botsy

https://raw.githubusercontent.com/lelouchB/botsy/master/bot.js

Twitter Documentation

Docs

Top comments (1)

Collapse
 
andypiper profile image
Andy Piper

It's worth remembering that when you sign up for a developer account, you're subject to the developer agreement and policy, and also you must follow the Twitter Automation Rules.