blog

home > blog >

#anydayshaders twitter bot

— 7 min read

I deployed my first twitter bot two days ago, and I thought of sharing my learnings in case someone else is also curious about it and the reasons behind why I did it.

If you prefer to skip my motives for making a twitter bot, go to the resources directly.

Why make a twitter bot?

Last year, in April 2020, I created a challenge for myself to learn GLSL shaders. I came up with a hashtag to use while I shared my learnings and invited my twitter peeps to join.

At the end of the summer, a few peers and new people joined the challenge, and I thought it'll be cool to gather all of those creations people were sharing somewhere. In addition to keeping track of how many people were joining the challenge.

If new people continue joining the challenge in the future, it'll be nice to have a place to talk to them. To kind of build a small community around it [yep, I'm a community-oriented person].

Making a twitter bot to retweet the tweets using the #anydayshaders hashtag on twitter came to my mind, as at the same time, I was always intrigued about how one could program one of those bots.

Time to kill two birds with one stone [no birds were harmed while writing this blog post].

Maybe others like me want to see what individuals are doing with shaders to be inspired and might want to follow such a twitter account instead of clicking on the hashtag.

How to make a twitter bot?

Luckily, we're living in an era where the Internet has tons of resources whenever we want to learn something new.

To make a twitter bot, I didn't ask any of my developer friends for advice, but instead, I googled it. To be precise I deved it.

No need to browse deved in the dictionary, because I made up this word as I was writing this post. When I said deved I meant I searched in dev.to.

I found several amazing resources to write a twitter bot that retweets tweets from a hashtag with JavaScript and deploy it for free with a Heroku server.

I'll break the process into three steps.

Make a twitter bot — step #1

First thing to do before you even start writing your bot is to apply for a developer twitter account and create a project app.

It might take a day or more before your project is approved, and you can move on with getting the access keys you'll need to use the twitter API. A post by Seema Saharan was helpful during this step.

Make a twitter bot — step #2

Choose the programming language in which you want to write your twitter bot. I went for JavaScript, but Phyton seems popular as well.

Please be kind and remember that if you also decide to create a twitter bot, as Stan Lee wrote for one of his characters, “With great power comes great responsibility.”

I'm no spiderman fan whatsoever and stole that quote from one of the resources I found on making a Twitter bot by Ben Greenberg.

What I liked the most from this resource was learning to create environment variables. Such variables will store safely the access keys we got from our developer twitter account. And avoid having them publicly in a JavaScript file.

export consumer_key=YOUR KEY HERE
export consumer_secret=YOUR SECRET KEY HERE
export access_token=YOUR TOKEN HERE
export access_token_secret=YOUR ACCESS TOKEN SECRET HERE

The second useful resource I found during this step was by Sumedh Patkar. The main interesting piece of advice from this post was learning to avoid duplication of retweets with a little helper function and some extra JavaScript code.

// Utility function - Gives unique elements from an array
function uniqueTweets(value, index, self) {
  return self.indexOf(value) === index;
}

/* More JS */

// Get unique entries
tweetIDList = tweetIDList.filter(uniqueTweets);

Make a twitter bot — step #3

Last step is deploying our twitter bot and let it run by itself, working like a charm.

Both of the previous resources show how to deploy the bot in Heroku with a free account. I followed their advice and can recommend the same.

Make an account in Heroku and create an app where you'll have your bot. The post by Sumedh Patkar goes more into detail about the whole setup.

You might need to write some Heroku commands in your terminal, so make sure you install the Heroku CLI in your system.

I have macOS and brew, so I made the install with this terminal command:

brew tap heroku/brew && brew install heroku

If you have a different environment, read about your setup in the Heroku CLI docs.

Once your bot is successfully deployed and working, you can view it in your Heroku dashboard. Remember that your web dyno should be OFF, and your worker dyno should be ON.

One thing I found useful after I deployed my bot was cloning the Heroku repo. I had previously made a personal GitHub repo for it when I created the bot.

With the Heroku repo on my computer, I made a small update in my JavaScript file and kept better track of the last deployed changes.

Wrapping up

That's it! My twitter bot is alive. You can view it at @anydayshaders_

If you thought I might have skipped some steps to create a Twitter bot, I didn't. I purposely wrote the steps that way and linked all the resources I used, highlighting what I thought most important to know.

I felt there's no need for me to copy and paste again what those resources I'm pointing to are already giving us. I preferred to guide you through it, revisit what I learned, and hope that if you're also curious to experiment in making a twitter bot, you'll use that power with responsibility. ✌🏽

References: