sipgate has a fantastic kitchen, serving breakfast and lunch. We have a web page which lists what’s on the lunch menu. In a sense there’s no point in checking because the food is, without fail, always yummy. Nonetheless, it’s still nice to know. I wondered, since I spend so much time in Slack, whether I could just find out what’s for lunch from there?

Enter Slash Commands

As it happens, Slack has something called Slash Commands. According to Slack, slash commands “enable Slack users to interact with external services directly from Slack.”

The first step is to add a slash command in Slack. Go to the App Directory and select ‘Build’ from the navigation bar. You want to select Make a Custom Integration. You’ll see a list of 4 types of custom integrations which you can build:

  1. Incoming WebHooks
  2. Bots
  3. Slash Commands
  4. Outgoing WebHooks

Select Slash Commands and start by choosing your command. This is what a Slack user will type in at the Slack command line to trigger your slash command. In this case the command is ‘/lunch’.

After adding this command, you have to add some integration settings. Most importantly, you need to set a URL which the slash command will request when it’s run. I’ll talk about that next.

Screenshot of integration settings for a Slack slash command

Your application

As you can see, the URL I added was for a Heroku application. I wrote a simple Sinatra application which just scrapes the website. (Something better like JSON is not an option right now.) I’ve included the relevant parts of the application below…

post '/' do
	#code which sets 'lunch' string omitted

	#pass what's for lunch to our 'respond_message' method
	respond_message lunch

# Slack expects us to respond with json
def respond_message message

  content_type :json
  {:text => message}.to_json


The Result

Once you’ve got your application running and the slash command configured in Slack, you’re good to go. As soon as you type the forward slash, Slack will show a list of available slash commands and then filter these as you type more letters.

Screenshot of lunch slash command being triggered

It’s 09:12 and I’m already thinking about lunch. Mmm, Kalbsbraten.

Screenshot of what's for lunch