How to create a basic Twitterbot in Node.js (using Twit)

Requirements:

  • Node.js installed on your platform of choice
  • A registered Twitter account*
  • Ease with or at least knowledge of using the command-line tools for your operating system

*In order to create apps on your Twitter account, you need to verify it using a mobile phone number. For that, you can use your own phone number or, if you have a Google account, create one using Google Voice.

Naming your App

After logging into your Twitter account, proceed to apps.twitter.com. This is where you will register your app, get your API information, and control how your application works.

Twitter_NewApp

Click on the “Create New App” button to start the process of creating a new app and registering it with Twitter.

Twitter_CreateApp

For each required field, “Name”, “Description” and “Website”, make sure you fill out the information.

Note, too, that the “Name” will be how this application is known to other services. While you can using something simple like “TestingBot” for this example, it might be worthwhile to find a name you like and is not already in use for more advanced projects.

Twitter_Agree

 

Before you can finalize creating a Twitter application, you must agree to the Developer Agreement.

Take the time right now to read through it and become aware of how Twitter expects an application to act and what rights you have as a developer through this agreement.

Once done, click on “Create your Twitter application”.

Twitter_AppScreen

With your application now created, you can change the default permissions from “Read-only” (the application can only read your tweets) to “Read and Write” (the application can read and write tweets).

Changing Permissions

To do that, click on the “Permissions” tab.

Twitter_Permissions

 

From the choices, select “Read and Write” and then click on the “Update Settings” button.

Now that the permissions have been changed and your application will be allowed write tweets in the future, we need to find the API key information and create an access token.

Click on the “Keys and Access Tokens” tab.

Creating an Access Token

The “Keys and Access Tokens” tab contains your Consumer Key, Consumer Secret, and other private information. As noted in its description, none of those details should ever be public.

Twitter_CustomerScret

 

While Twitter will generate your Consumer Key and Secret pairs for you, it does not automatically create an Access Token for your application. To do that, scroll down the page to find that section.

Twitter_AccessToken

 

To generate an Access Token at your current permission levels (“Read and Write”) for your application, click on the “Create my access token” button.

Twitter_AccessTokenGranted

Once generated, you should now have a Consumer Key, Consumer Secret, Access Token, and Access Token Secret. All four will be needed to use the basic Twitter bot successfully.

Starting with Node.js

Before moving to the next section in this guide, I suggest you go ahead and create a new folder for testing the Twitter bot. That way, you can install the modules locally and not potentially disrupt any other processes or Node.js projects. (This is generally a best practice.)

Installing the Twit module

Open a command-line window and navigate to the folder you created for the project.

Once there, type the following and press Enter:

npm install twit

Once installed successfully, you are now ready to write code and one step closer to having a basic Twitter bot.

Testing your configuration

In the same folder your created and installed the Twit module into, create a file called “twitterBot.js” and copy the following code into it.

[gist https://gist.github.com/videlais/dee7f7b079770dfcc289]

Within the configuration section of the code, be sure fill in each of the four items it needs: Consumer Key, Consumer Secret, Access Token, and Access Token Secret from your application settings.

Once done, save the changes.

To test the script and your configuration, run it with the following:

node twitterBot.js

If everything went successfully, you should now have a new tweet in your feed. It should look something like the following, but matching your own Twitter account.

Twitter_PostedTweet

Congratulations! You have now created a very basic Twitter bot capable of posting tweets from your Node.js code.

Where to go from here

Now, obviously, having a single tweet does not a Twitter bot make. It needs something more than the ability to post just a single string of “I’m posting a tweet!” over and over.

For that, consider looking at the Twit GitHub page for details on its API.

For example, if you wanted to collect tweets instead of writing them, you might want something like the following code:

[gist https://gist.github.com/videlais/28c450b9472cd3b27a75]

Really, the sky’s the limit now! Just don’t abuse Twitter’s API. You will just blacklisted for spamming or otherwise breaking their terms of service.