Tweeting using Node js

ahmed_mahallawy

Mahallawy

Posted on July 25, 2020

Tweeting using Node js

Introduction

Twitter is a great social media platform where you can interact with a lot of people all over the world. But you can use twitter in a different way, the Developers Way. You can do that by consuming the Twitter API.

Twitter API gives you the capability to do what you do on twitter but from outside of it. You can build applications that can tweet, message, like, and retweet. There are many ways to do that, but here you’ll build an application using Node js.

Here you’ll use only Node Js and twitter lite to post tweets to your account.

Create a twitter developer account

  • First, you’ll need to have a developer account on Twitter, you can apply for it at Twitter Developer Website.
  • After you created the developer account, log in to it Twitter developer website
  • Create an App by clicking on Dashboard in the top right of the page, then under Standalone Apps click on + Create App.
  • Then you’ll only choose a unique name for your App
  • That’s great. Now you have an App on twitter. We’ll get back to it in a few minutes.

Using Node JS

  • If you don’t have Node js yet, you have to install it from Node JS website and choose the LTS version.
  • If you have it installed check its version in cmd using:
node -v
Enter fullscreen mode Exit fullscreen mode
  • Now start building the application by creating a folder and name it TweetBot (you can change the name).
  • Open cmd and type the following command to create package.json file:
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • Then add the twitter lite using npm with the command:
npm install –save twitter-lite
Enter fullscreen mode Exit fullscreen mode
  • Open VSCode or any code editor and create a file and name it config.js and add the twitter-lite configurations as follows:
module.exports = {  
  consumer_key: ' ' ,  
  consumer_secret: ' ',  
  access_token_key: ' ',  
  access_token_secret: ' '  
}
Enter fullscreen mode Exit fullscreen mode
  • The configuration values can be found in the app you created in the twitter developer website under Keys and Tokens link. >The latest twitter changes included changing the consumer key name to be API key, and consumer secret name to be API key secret.
  • You may need to generate access token and secret.
  • Now create an index.js file and start it by adding the configuration file and twitter-lite to the app as follows:
const config = require('./config');
const twitter = require('twitter-lite');
const client = new twitter(config);
Enter fullscreen mode Exit fullscreen mode
  • Then, you create a request for the Twitter API.
  • For tweeting, we can use the post request 'statuses/update'. You can see full details for this request here.
  • You'll use twitter lite to handle the request (for more information about twitter lite visit their Github page)
client.post('statuses/update', { status: 'Hello world!' }).then(result => {
  console.log('You successfully tweeted this : "' + result.text + '"');
}).catch(console.error);
Enter fullscreen mode Exit fullscreen mode
  • This post method consists of 2 parts: the first is the endpoint text (which is 'statuses/update'), the second is a parameter object that requires a status attribute that contains the text to be tweeted.
  • If the request is successful, the result object will contain all tweet information. If an error happens, the catch method will log it to the console.
  • Now go to cmd and type:
node index.js
Enter fullscreen mode Exit fullscreen mode
  • And voila!!! You tweeted from your application 💪

In the following tutorial, we will make like, retweet and follow bot, 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

Pictures used in cover are:

💖 💪 🙅 🚩
ahmed_mahallawy
Mahallawy

Posted on July 25, 2020

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

Sign up to receive the latest update from our blog.

Related