code, howto

Creating a basic Twitter bot in Node.js (using node-twitterbot)

Requirements:

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

Background on Node.js

Starting from hobby projects and small demonstrations just few years ago, Node.js has taken the web and server development scene by storm. Allowing programmers to write JavaScript for both front and backend processes, it has expanded well beyond its roots in web servers to include modules from everything from interfaces with WebKit to full-blown build and code maintenance environments.

In short, if you’d like to use JavaScript as your scripting language of choice beyond browsers, Node.js is your new best friend.

Creating a Twitter App

You will need a registered Twitter account. Although I included it as a requirement at the top of this page, this is the part where you will need it. Before you can continue, make sure you have a Twitter account!

Naming your App

After logging into your account on its website, 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 node-twitterbot 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 node-twitterbot

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 Node-TwitterBot module into, either copy the following code or download the file directly from GitHub.

Within the configuration section of the code, fill in each of the four items it needs: Consumer Key, Consumer Secret, Access Token, and Access Token Secret.

[gist https://gist.github.com/52061c54cbe74bb1a92b]

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.

To do that, we need to consult the documentation of the Node-TwitterBot module and see what else it can do and how we might proceed in creating something more.

For example, instead of calling the Bot.tweet function repeatedly, we can setup actions for the bot to take and then call them instead. And, when using something like the setInterval function to create an infinite loop, we can have our bot post content every few minutes to a feed.

[gist https://gist.github.com/ed3c10fa4caac0aa346d]