Like, Retweet, and Follow in Twitter using Node JS
Tags: tutorial, twitter, node, javascript
Published: 2020-08-02 13:08:25

###Introduction
In the previous tutorial, I built a node js application with the help of [twitter lite](https://github.com/draftbit/twitter-lite) to add a new tweet to Twitter. You can see the tutorial [here](https://dev.to/ahmed_mahallawy/tweeting-using-node-js-5986).  
  
In this new tutorial, we'll make a similar application to like a tweet, retweet, and follow a twitter user. The new application follows the same structure used in the previous one. 

###The Application
- As we did in the previous tutorial, We should have 2 files, the first one is config.js that contains twitter lite configurations, and the second file is index.js which contains the following code:
```
const config = require('./config');
const twitter = require('twitter-lite');
const client = new twitter(config);
```
- We'll use my twitter account to explain how to like, retweet, and follow. So first, we'll search for my account using twitter API:
```
// Get twitter user
client.get('users/show', { screen_name: 'ahmed_mahallawy' })
    .then(result => {

    var user = result;
    var latestTweet = result.status;
}).catch(console.error);

```
- This code makes a get request to 'users/show' endpoint that retrieves user data using my screen name (screen name in twitter is the one preceded by @). You can see full details for this request [here](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show).  
- We'll save the result in **`user`** variable and the user’s latest tweet in **`latestTweet`** variable using **`result.status`**, so we can use both of them later.

- To like the selected tweet, we'll use a post request to 'favorites/create' endpoint that will add a like to tweet using the tweet id_str attribute:
```
// Like a tweet using its id_str attribute
client.post('favorites/create', { id: latestTweet.id_str })
    .then(result => {

    console.log('Liked tweet successfully!');
}).catch(console.error);
```
- You can see full details for this request [here](https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-favorites-create)  
- Now, we make a retweet in a very similar way of liking. We'll make a post request to 'statuses/retweet' endpoint that will make retweet using the tweet id_str attribute:
```
// Retweet a tweet using its id_str attribute
client.post('statuses/retweet', { id: latestTweet.id_str })
    .then(result => {

    console.log('Retweeted successfully!');
}).catch(console.error);

```
- You can see full details for this request [here](https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-retweet-id)  
- Finally, we follow the user by making a post request to 'friendships/create' endpoint using the user screen name:
```
// Follow a user using his/her screen_name attribute
client.post('friendships/create', { screen_name: user.screen_name })
    .then(result => {

    console.log('Followed ' + user.screen_name + ' successfully!');
}).catch(console.error);

```
- You can see full details for this request [here](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/post-friendships-create) 
- We wrote all the required code, and now we can run it from cmd using the command:
```
node index.js
```
Congratulations! 
You did it!!!!!!!!!! :muscle:  

Here's is the full code for index.js file:
```
const config = require('./config');
const twitter = require('twitter-lite');
const client = new twitter(config);

// Get twitter user
client.get('users/show', { screen_name: 'ahmed_mahallawy' })
    .then(result => {

    var user = result;
    var latestTweet = result.status;

    // Like a tweet using its id_str attribute
    client.post('favorites/create', { id: latestTweet.id_str })
        .then(result => {

        console.log('Liked tweet successfully!');
    }).catch(console.error);

    // Retweet a tweet using its id_str attribute
    client.post('statuses/retweet', { id: latestTweet.id_str })
        .then(result => {

        console.log('Retweeted successfully!');
    }).catch(console.error);

    // Follow a user using his/her screen_name attribute
    client.post('friendships/create', { screen_name: user.screen_name })
        .then(result => {

        console.log('Followed ' + user.screen_name + ' successfully!');
    }).catch(console.error);
}).catch(console.error);
```
In the following tutorial, we are going to explore more about twitter API. I have several ideas to share with you, so stay tuned :wink:

For the full code, you can visit [my github page](https://github.com/amahallawy/LikeRetweetFollowBot)

If you like my tutorials, support me here [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/F2F41UH5T) and follow me on Twitter [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/ahmed_mahallawy.svg?style=social&label=Follow%20%40ahmed_mahallawy)](https://twitter.com/ahmed_mahallawy)