Making a Mastodon Bot in Ruby

I’m not an experienced programmer, so I had to search around for the basics of setting up a Mastodon bot. After synthesizing information from a few resources (and figuring out Bundler, too), I thought it’d be a good idea to write a quick guide to the basics of making a Mastodon bot.

First, sign up for a new account on a Mastodon instance. I chose botsin.space.

Follow this helpful guide by Darius Kazemi on Tiny Subversions to generate your bearer token. This code lets you access your Mastodon account through other apps. Store it somewhere safe — you’ll need to paste it later.

Next, make a new directory for your bot and then navigate there:

$ mkdir bot
$ cd bot

If you don’t already have it, install Bundler. Bundler tracks and stores your gem dependencies so you don’t have to worry about setting that up manually.

$ gem install bundler

You’ll need a Gemfile so you can tell Bundler which gems your bot relies on. In terminal, type: $ nano Gemfile and add the following:

source 'https://rubygems.org'

gem 'mastodon'
gem 'mastodon-api'

Write out with ctrl+o, enter, then ctrl+x. Now, open up your text editor and write this into a blank file:


require 'rubygems'
require 'bundler/setup'
require 'mastodon'

client = Mastodon::REST::Client.new(base_url: "INSTANCE_URL", bearer_token: "BEARER_TOKEN")

toot = "test!"
client.create_status(toot)

Replace INSTANCE_URL and BEARER_TOKEN with your chosen instance URL and the bearer token generated by the curl command from the Tiny Subversions tool. Save it as bot.rb, and then do bundle install in terminal. Run the bot with ruby bot.rb.

Check your bot on Mastodon, and you should see that it has tooted “test!”.

Now you’ve got an authenticated bot live on the server, you can do endless things with it. The documentation is very clear and well-written.

You can do things like save the last 10 toots with a specific hashtag to an array, and then boost them one at a time. Or, listen for new items on an RSS feed and toot the link and title. Basic programming knowledge can be applied to do some really cool things with bots, and they’re fun to experiment with.

For this bot, I simply replicated @grimlocations, a Twitter bot I made a while ago. The bot picks two random words (a descriptor and a landscape) and puts them together:


require 'rubygems'
require 'bundler/setup'
require 'mastodon'

client = Mastodon::REST::Client.new(base_url: "INSTANCE_URL", bearer_token: "BEARER_TOKEN")

descriptors = ["ancient" .. "hidden"] # loads of words

landscapes = ["desert" .. "peninsula"] # loads more words, cut for brevity

def toot(client,descriptors,landscapes)
  descriptor = descriptors.sample
  landscape = landscapes.sample
  grimlocation = "#{descriptor} #{landscape}"
  client.create_status(grimlocation) # toooot
  puts "tooted: #{grimlocation}"
end

while true
  toot(client,descriptors,landscapes)
  puts "Sleeping..."
  sleep(3600) # toots once per hour
end

Console output:

 ➜ grimlocations git:(master) ✗ ruby grimlocations.rb
tooted: corrupted mountain
Sleeping...
tooted: wild crag
Sleeping...
tooted: corrupted flatland
Sleeping...

Mastodon output:

The botsin.space instance is filled with bots for inspiration, and you can check out these resources for more:

Update: Once your bot is ready to go, you’ll want a place to host it so you don’t have to keep the process running on your computer at all times. I deployed my bot to a Raspberry Pi over SSH, but you can also use Heroku for free.