DEV Community

loading...

Like, Retweet, and Follow in Twitter using Node JS

Mahallawy
Developer for 13+ years (GIS, C#, and javascript)
・3 min read

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);
  • 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.
  • 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
  • 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
  • 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
  • We wrote all the required code, and now we can run it from cmd using the command:
node index.js

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);

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

Discussion (2)

Collapse
generativexbot profile image
Generative Art Bot

Thank you so much for parts 1 & 2. I'm happy to read clear, simple instructions on how to do this! Do you have plans to do instructions for replying to a tweet or posting an image?

Collapse
ahmed_mahallawy profile image
Mahallawy Author

Yes, I will post more about Twitter API soon, that may include messaging, replying, scheduling, and more
Part 3 also is very useful if you building twitter bot and need to test th API endpoints
Thank you very much for your supportive comment, that makes me wanna post more here 🀩

Forem Open with the Forem app