Recently I built a Twitterbot to tweet 80’s movie quotes – because doesn’t the world need something like this?

I wanted to build something using Node.js. I’d never really done anything in Node before, outside of some tutorials. I think that learning by doing is the best way to go. At least, it is for me.

I started researching how to build a Twitterbot in Node and I found some really great articles on how to do it that I’ve posted at the bottom of this post. I used a tool called Twit to post to Twitter. I had to do a few things first.

First I made a Twitter account. Keep in mind that each Twitter account needs its own email address. You can’t share an email address between Twitter accounts. You’ll also want to verify the new account with a phone number. At this time I have been able to share my phone number across multiple accounts.

Next I went to to set up an application for this account. I signed in under this account name, and then set the application to have permission to ‘Read, Write, and Access direct messages’. Next I headed over to the ‘API Keys’ tab and clicked ‘Generate API Keys’.

Once I had these, I created some ENV variables on my computer. I’ll be putting out another blog post that explains how to do that later. (It’s done, check it out!) The reason I did it this way was for security. I didn’t want my API tokens floating around on GitHub for someone to find and use. This allowed me to use an object like this in my code:

var twitInfo = {
  consumer_key: process.env.consumer_key,
  consumer_secret: process.env.consumer_secret,
  access_token: process.env.access_token,
  access_token_secret: process.env.access_token_secret

I later uploaded my project to Heroku to run, and this worked there as well, after I added ENV variables there too. (There will be another blog post about Heroku coming soon.)

Writing the actual code for the bot was surprisingly simple. Twit makes posting to Twitter incredibly easy.

var twitter = new Twit(twitInfo);

Basically we are passing the API token information into this Twit function in the form of an object.

var postToTwitter = function () {'statuses/update', { status: quote }, function(err, data, response) {

And then actually posting to Twitter is as simple as calling this function, and passing in the quote in an object. The quotes themselves are held in an array in a different file. Once the quote to be used is selected, it gets passed into this function and posted to Twitter.

If you want to follow the bot you can do so here. Some articles that were extremely helpful in this process are posted below.

I hope this was a fun read, and keep an eye for follow-up posts, including one on promises!

Questions? Tweet me!