Create a simple yet effective Twitter BOt in nOdejs (javascript)
Tags: twitter, node, javascript
Published: 2019-07-20 09:55:03

---
title: Create a simple yet effective Twitter BOt in nOdejs (javascript)
published: true
tags: twitter,nodejs,node-js, JavaScript,JS
canonical_url: https://medium.com/@lelouchb/create-a-simple-yet-effective-twitter-bot-in-nodejs-javascript-4fbabc359404
---

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

![](https://cdn-images-1.medium.com/max/1024/1*laq2FhOjUQclngSvJoK91Q.jpeg)

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/](https://nodejs.org/en/) and install it

![nodejs](https://cdn-images-1.medium.com/max/1024/1*2il8EDhY7nJa5MhXzUqaFQ.png)

2. 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](https://developer.twitter.com/en/apply-for-access)

![Twitter Developer Account](https://cdn-images-1.medium.com/max/1024/1*Adrvxqm5kBXlvtLkYwHv3Q.png)

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](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.

![](https://cdn-images-1.medium.com/max/1024/1*VdDFQP_R4cirabUpE72NNg.png)<figcaption>It should look like this</figcaption>

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:’ ’**

**}**

![](https://cdn-images-1.medium.com/max/381/1*CCOKlYyJ1XU8KWeh8svUXg.png)<figcaption>It should look like this but with your keys and token.</figcaption>

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_

![](https://cdn-images-1.medium.com/max/661/1*pKlvMbma1_9LyrBycPm05Q.png)

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**

![](https://cdn-images-1.medium.com/max/803/1*x-CodQ1yNxMxRrXU4gYDYg.png)

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

![](https://cdn-images-1.medium.com/max/429/1*c4G42RlQvZDSG3U9FdTicA.png)

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.

![](https://cdn-images-1.medium.com/max/755/1*jyb6yMRnlgNIAMsfkD03_Q.png)

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

![](https://cdn-images-1.medium.com/max/688/1*q32LNPCP71b5HzA_fOgftQ.png)

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://github.com/lelouchB/botsy/blob/master/bot.js)

[https://raw.githubusercontent.com/lelouchB/botsy/master/bot.js](https://raw.githubusercontent.com/lelouchB/botsy/master/bot.js)

Twitter Documentation

[Docs](https://developer.twitter.com/en/docs)