Skip to content

Write a Slackbot in Less Than 20 Lines of Code

What is Slackbot?

Slack has an API called “slash commands” that lets a user type a slash (/) followed by a command name in order to perform a special action. For example, Slack has many built-in slash commands, one example is /help. Here’s how you can easily add your own slash commands using a Xojo web app and the HandleSpecialURL (or HandleURL) method.

Your slash command makes an HTTP request to your Xojo web service app. The web services does its thing and then returns the result back to Slack to display.

A Xojo Web Service

For demonstration, I’m going to show you how to create a Slackbot that returns a “cat fact”. The slash command will be /catfact. When you use the /catfact command, Slack calls a Xojo web service which then makes a call to the Cat Fact API, parses the resulting JSON and returns an interesting cat fact back to Slack to display in your channel.

coding_cat.png

As I’ve covered in a couple Web Services webinars last year, it is pretty easy to make a web service using Xojo. For this example, create a new web project and name it CatFact.

First you will create a simple method that will fetch a Cat Fact. Create a method on the App object, GetCatFact As String, with this code:

// New request came in, so create a new HTTPSocket
Dim catSocket As New HTTPSocket

// Get a cat fact and wait for it to be returned
Dim factData As String = catSocket.Get("http://catfacts-api.appspot.com/api/facts?number=1", 30)

// Once the cat fact is returned, parse the JSON and send it to the original requester.
Dim jsonText As Text = DefineEncoding(factData, Encodings.UTF8).ToText
Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(jsonText)
Dim facts() As Auto = jsonDict.Value("facts")

// Extract the fact text from the JSON
Dim fact As Text
If facts.Ubound >= 0 Then
  fact = facts(0)
End If

Return fact

In this Xojo project you’ll now create a simple web page UI to test that calling the Cat Fact API works. On the default page (WebPage1), add a Button and a TextField. In the Button’s Action event handler, add this code:

CatFactArea.Text = App.GetCatFact

Run the web app and on click the button. You should see a cat fact appear in the Text Area. The fact I got was:

The cat has 500 skeletal muscles (humans have 650).

Now you can add the code to process the web service request. Add the HandleSpecialURL event to the App object and use this code:

If Request.Path = "CatFact" Then
  Request.Print(GetCatFact)
  Return True
End If

You can test this by running the web project and then in a new tab entering the API URL, which displays a cat fact in the browser:

http://127.0.0.1:8080/api/CatFact

Hook Up Web Service to Slack

Now that you know the web service works, the next step is to hook it up to Slack. But before you can do that, you have to host the web app somewhere on the Internet; Slack can’t see your local web app! For this example, I’ve published the Cat Fact web service to Xojo Cloud because it’s fast and easy. This is the URL to the Cat Fact web service on Xojo Cloud:

http://demos.xojo.com/CatFacts/index.cgi/api/CatFact

Now you need to go to your Slack team settings to add a slash command. On that screen you’ll want to set the name of the Command to “/catfact” and the URL to “http://demos.xojo.com/CatFacts/index.cgi/api/CatFact” (the URL from above). Also change the Customize Name field to “CatFact”. You can ignore all the other fields and click the Save Integration button at the bottom.

That’s it. The slash command is now active. Head on over to your Slack team and type /catfact into a channel. You’ll get back a fact from “CatFact”.

SlackbotCatFact.png

And it took less than 20 lines of code! You can now use this technique to integrate your own web services into Slack.

By default, only you will see the cat fact returned by the Slashbot; it is not broadcast to the entire channel. You can change this by returning specifically formatted JSON rather than just a text string (as described on the Slack Commands API page).

If you come up with a great slash command and want to make it available to the world, you can attach it to a Slack app and publish it to the App Directory. The steps for this are also in the Slash Commands API page.


Use HTTPSocket with Cat REST API