Build a Node.js Twitter bot03 Dec 2013
Twitter bots are fun, and quite simple to make. You have to be careful about what you program your bot to do so it doesn't get banned for spamming, but the basic code can be adapted for all kinds of things. Here's how I built a bot using Node.js to send people hugs or pugs.
If you want to follow along, you can either create a new directory and copy the code step by step, or fork my repo.
Firstly, you need to register your bot as an application with Twitter in order to access the API. Create a new Twitter account for your bot - don't use your personal one - otherwise Tweets will come from you and not the bot. It's also useful to have this separate in case you mess up and end up getting suspended, as this way you won't risk losing access to your personal account. Create an access token and make sure its access level is set to 'Read, write, and direct messages'.
If you don't already have Node.js installed, you'll need to do that next.
Now go to your project directory. If you're working from a fork of mine, just run
npm install, then move onto the authentication section. If you started a new project, run
npm init and follow the instructions to create your package.json. Then run
npm install twode --save to install twode (the Twitter API client we're using). Your project directory should now contain a
Create a file in your project directory called auth.js to store your API authentication config, and add it to your .gitignore:
We're going to use environment variables to avoid hard coding these values. You don't want them public otherwise anyone could control your Twitter bot.
To set the environment variables, add this loop to auth.js:
Then, from your project directory, run
node auth.js and check the correct keys have been set. You only need to do this once.
Now create a new file called bot.js. This is where you'll write the rest of your code.
We're using the twode module, so require that at the top of your bot.js file:
To create the bot and feed it the correct authorization info, we're going to use the prototype pattern. This is the constructor function, which takes a Twitter handle and references the environment variables we set:
From this point, the code is going to be specific to my bot, but it's easily adaptable to do other things.
I want my bot to either Tweet hugs or a pug picture, based on a random chance outcome. So let's store some pug pictures:
I want a 10% chance of responding with a pug picture, so let's write a function to determine whether a given response should be a pug or not:
And a function to build the response:
The next function is where we listen for the stream of Tweets, filtering based on the bot's Twitter handle. This means that when someone mentions the bot, it will generate a response and post it as a Tweet.
We've referenced a
handleError function here in the callback, to log any errors in case something goes wrong. Let's write that now:
To make all of this work, all that's left to do is create an instance of the bot, passing its Twitter handle as a parameter, and call the
We can now actually run it to see it working by typing
Next, you might want to learn how to deploy your bot on Heroku.