Runtime Revolution

Toilet Vacant: A Rails app which notifies you via Slack

jpachitas profile image João Chitas Originally published at revs.runtime-revolution.com on ・4 min read

Photo by Gabor Monori on Unsplash

Every day, there’s that time when you need to go to the toilet. You get off your chair, start walking towards a door with a drawing of a gentleman or a lady and hope that it is vacant. If your prayers were answered, you can enter and do what you got to do.

But here, at our office, sometimes you see people (me included), walking around the office. They are not walking because it is healthy. No. They are walking because they are looking for a toilet that is vacant. And sometimes, in the worst case scenario, they go back to their place without doing their business.

It happens to me a lot and it really grinds my gears (insert Peter Griffin meme here)!

If only there was a way we could be notified on Slack that a toilet was vacated…

The Solution

The true purpose of this post is not to present a complex solution to check if a real toilet is vacant. It is to show you how to use Slack’s Incoming Webhooks API on your Rails application, in a funny way.

The full app is available at https://github.com/jpac-run/toilet-vacant-app for you to checkout and see the application working. There is a README file with instructions on how to run it. Because of this, I won’t get into too much details for some parts. The only outstanding requirement you’ll need is to create a Slack application and permit the Incoming Webhooks. You can see how in this link: https://api.slack.com/incoming-webhooks.

Ok, now for some code.

You know the drill: create a fresh Rails application and add httparty and dotenv-rails gems to your Gemfile.

Create a ToiletsController with the index method and set it to be the application root in routes.rb.

In the application root folder, create a file called .env. This file is used to store your environment variables.

After you go through the steps in the Slack Incoming Webhooks link mentioned above, you’ll now have a special URL to send a message via the API, that we will store in a .env file, along with the channel name, like this:

Again, inside your routes.rb, add two new routes:

And define the index and update actions inside ToiletsController.

But wait… we need some toilets!!! So, on a command line, type:

rails g model Toilet toilet_number:integer, open:boolean

and after your toilet model is created, set the open column with a default value of true inside the migration.

With a toilet model, you can now create thrones — I mean, more toilets! You can either use the seeds.rb file or just run the Rails console and create some.

Create a view to index all the toilets and for each one add a button/link that will call either the get_in or the get_out actions, depending on the state of the toilet. Something like this:

Yeah, it’s not pretty, but neither is your toilet when you use it…

Ok, sorry. Each of the actions will change the state of the selected toilet (change the open value to true or false) and (here comes the cool part) it will trigger an event that takes care of sending a notification to the Slack channel.

To do this, we will use the ActiveSupport::Notifications to treat the events logic. Create an events.rb file inside config/initializers and subscribe your app to an event that will send the notification:

Every time the occupied.toilet event is triggered, the code inside the subscribe block is executed. This block uses the httparty gem to call the Slack API. Inside the body of the request, you have to specify the name of the channel and your username. The field text contains the message you want to appear on the channel. The 🚽 and 💩 strings will appear as an emoji!

You can send a more custom message, just follow the API documentation.

Now try to use this same logic to create an event that notifies the channel when the toilet is vacant!

With this, I hope you got motivated to develop your own notifier for your office toilets and avoid getting up from your chair and go around the office looking for an empty little room to do what you got to do.

I’m currently a Ruby-on-Rails developer at Runtime Revolution that still likes to play the good old Pokemon games for the GameBoy. Go check our website to know us more. Oh and we’re hiring!


markdown guide

I would like to continue this small project and apply it to our office's toilets. Probably going to use a Raspberry Pi (because I own one and is still new) and it could be an opportunity to learn more about this kind of hardware :)


I had such a hard time figuring out how to make a little Slack bot like this before. 😭 Great guide!