Like, Retweet, and Follow in Twitter using Node JS

ahmed_mahallawy

Mahallawy

Posted on August 2, 2020

Like, Retweet, and Follow in Twitter using Node JS

Introduction

In the previous tutorial, I built a node js application with the help of twitter lite to add a new tweet to Twitter. You can see the tutorial here.

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);
Enter fullscreen mode Exit fullscreen mode
  • 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);

Enter fullscreen mode Exit fullscreen mode
  • 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.
  • 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);
Enter fullscreen mode Exit fullscreen mode
  • You can see full details for this request here
  • 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);

Enter fullscreen mode Exit fullscreen mode
  • You can see full details for this request here
  • 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);

Enter fullscreen mode Exit fullscreen mode
  • You can see full details for this request here
  • We wrote all the required code, and now we can run it from cmd using the command:
node index.js
Enter fullscreen mode Exit fullscreen mode

Congratulations!
You did it!!!!!!!!!! ๐Ÿ’ช

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);
Enter fullscreen mode Exit fullscreen mode

In the following tutorial, we are going to explore more about twitter API. I have several ideas to share with you, so stay tuned ๐Ÿ˜‰

For the full code, you can visit my github page

If you like my tutorials, support me here ko-fi and follow me on Twitter Twitter URL

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
ahmed_mahallawy
Mahallawy

Posted on August 2, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About