Making a Basic Twitter Bot With Ruby

Larry Sass-Ainsworth
4 min readJul 31, 2019

--

If you’ve been on twitter, you’ve probably seen a twitter bot. Maybe you didn’t even know it was one. Twitter doesn’t give us specific numbers about how many of its users are bots, but a USC study from 2017 estimated that as many as 15 percent of Twitter’s users could be bots, and that about two thirds of all links shared on twitter are shared by bots. We hear a lot about bots and their outsized influence on our politics and the way we consume our media. As someone who is still learning ruby and programming, a few weeks ago I had the thought of how hard would it be to program a bot using ruby?

The answer is, it’s pretty easy, depending on what exactly you’re trying to get your bot to do. Twitter has an open API, and all you need to do is apply for a developer account to get started.

You can do tons of stuff with the Twitter API, but my interests were just in making a bot. The process seems a but intense at first, and you have to answer a lot of questions, but it’s pretty easy to breeze through as long as you’re not trying to do anything too crazy.

What not to say when applying for a developer account

I didn’t have any good ideas for a bot, so I decided to make “bugbot”, a simple bot that would just tweet out stuff that a bug would say. It seemed kind of funny at the time.

There are a few steps when developing a bot. You have to create an account, create an app, generate keys for that app, and then connect it to your developer environment. When it comes to ruby, it’s as easy as installing a twitter gem (‘gem install twitter’) and following some basic steps.

I found this template online, filled in my keys and tokens, and started to figure out how to make this thing tweet.

It turns out it’s pretty easy if you’re looking at the ruby guides for the Twitter gem. Client.update sends out tweets from your account. I thought it would be cool to have it tweet regularly, so I added sleep to a ruby .times loop and let it run for a while. I forgot that sample only pulls unique things from the array so it only tweeted 4 times, but hey! progress was made.

While it’s kind of cool to be able to send out an array of tweets, Twitter already has built in ways to just schedule tweets. I hadn’t really done anything robotic yet. While searching for more to do, I found a method that let you find users that interacted with hashtag, and respond to the most recent ones to do so.

Naturally, my hashtag was ‘bug’.

I didn’t get much engagement with my posts clearly, but it felt like a step in the right direction. I started feeling a bit more powerful here and wanted to see what would happen if I reached out to more users.

I upgraded my message and thought I’d start with 30. Twitter didn’t like that. I got an error message in my terminal, and my account was temporarily suspended.

However, after passing the captcha test….

I am become spam, the destroyer of mentions.

I only just started to dip my toes into the bot world, but how easy it was is a testament to Twitter’s API and to the ruby skills taught to me at Flatiron. I can’t wait to do more.

--

--