Blue Skies Above Us

Just here to have fun.


5-Minute Read

Now that you’ve read the post title and are hooked, LET’s GO!

Following up on the last post about all/some of the Twitter bots I’ve made, here’s another I made yesterday. And I’m going to go into detail about making it.

If you read the previous post, you might have realized that these bots are simple. These bots are not sophisticated AI trying to sow discord and sway elections. They’re posting simple, goofy stuff. Today’s bot is called @badassnames, and it posts cool names that some friends and I have thought up. And it:

  • Is a script written in Ruby
  • It reads a line from a text file, and tweets that line
  • Next time it runs, it reads the next line.
  • It sits on a Raspberry Pi
  • The Pi has a crontab that instructs the script to run twice a day
  • The script uses the chatterbot ruby gem to tap into the Twitter API and post


Starting from the beginning: Ruby is a programming language. And it’s fairly easy to understand even if you don’t know much about programming. If you want to learn ruby, start with The Odin Project. Install RVM to manage ruby and its “gems”, then install ruby.

Here is the script for this bot.

!/usr/bin/env ruby

require 'rubygems'
require 'chatterbot/dsl'

names = "/home/pi/Documents/Bots/badassnames/badassnames.txt"
counter = "/home/pi/Documents/Bots/badassnames/counter.txt"

line_num = File.open(counter) {|f| f.readline}
line_num = line_num.to_i

File.foreach(names).drop(line_num).take(1).each {
  tweet_text = line
  tweet "#{tweet_text}"

line_num = line_num.to_i + 1

File.write(counter, line_num)

And here’s a little breakdown.

The names = "/home/pi/Documents/Bots/badassnames/badassnames.txt" line is explicitly pointing to a txt file. The text file is very simple. Each line in it has a first and last name. It’s just a long list of names. Like:

Broxton Terradome
Tab Chamberlain
Griff Manifold
  Change the path of that to whatever folder your script and text files are in.

Next we have another text file: counter = "/home/pi/Documents/Bots/badassnames/counter.txt"

This one is a counter whose function is to tell the script which line of the text file to read. First you’ll create this text file, and put `` in it. And this line in the script line_num = line_num.to_i + 1 increments the value by 1 each time the script is run (to_i means turn the string into an integer… not sure why it wasn’t always an integer).

line_num = File.open(counter) {|f| f.readline}

Is saying to read the counter.txt file, and assign the value in line number 1 of the file (line number 1 is actually line 0 in ruby… also it’s the only line in the file that has any content) to the variable line_num).

Now’s the magic. The Tweet de resistance (?).

File.foreach(names).drop(line_num).take(1).each {
  tweet_text = line
  tweet "#{tweet_text}"

This is saying to read the text file that has the names, and to ignore (drop) the first n lines (whatever the counter tells it), and then read (take) a single (1) line. The tweet text is a chatterbot function that does the magical Twitter API work. I assigned the contents of the line to a variable called tweet_text, just because.

So, to reiterate: when you first run the script, it will read the first line from the names file, and then it will read the number that’s in the counter file, and add +1 to it. It’s a nice simple script for incrementally reading a text file.


Ok, maybe I won’t go into super detail here.

  • If you have a Raspberry Pi (this is useful because it’s a computer that is always on, and thus can run your script whenever), put your files onto it.
  • I use scp to do that. Like scp myfile.txt pi@
  • Install ruby on it
  • Then type crontab -e
  • cron is basically a scheduler. You can read up on the formatting.
  • My cron here looks like 26 9,16 * * * /bin/bash -l -c '/home/pi/Documents/Bots/badassnames/badassnames.rb'
  • So it runs twice a day, at 9:26am and 4:26pm.
  • Why am I using bash to run a ruby script? well, it works.

Oh and you’ll need a Twitter Developer account

Can’t forget that:

  • Start a twitter account.
  • Use a gmail/yahoo/hotmail account or they’ll mysteriously insta-ban you because their spam sniffing is a total pile of crap.
  • Go to dev.twitter.com and apply for an account and create an “app” and get your credentials.
  • Put them in a .yml file that looks like
:consumer_key: blah
:consumer_secret: bleh
:access_token: bloh
:access_token_secret: bleh

In Conclusion

I don’t know who the audience here is. You’re either looking for a ruby script that does this. Or you’re curious to learn that twitter bots can be super basic and just for fun. If you have an idea, you can spit one out pretty quickly! In this case, I have to come up with a lot of cool names in order to keep this running. But, I don’t mind spending a couple minutes a day thinking of cool things. Do you mind that?

Possible use case for this script: Write a story, one line at a time, and tweet out the story on a regular schedule. I’m sure there are lots of services that do this. But you can do it yourself!

What is it?


Nothing yet.

Recent Posts



Founded 2005. Over the years I've posted writing, comics, ringtones, and stuff about maps, bikes, programming, pinball. And I had a robust music blog mostly about '90s hardcore punk (category = music).