🤖 Building Slack Bots In Swift 🤖
As a remote iOS developer, I love Slack. It’s both my meeting room and my water cooler. As interest in bots exploded, my interest was piqued. Of course I was interested in writing bots for one of my favorite services! My love of Slack and my love of Apple’s new programming language, Swift, came together in the form of SlackKit, a Slack client library for iOS, tvOS, and macOS. Unfortunately, it’s not very practical to have to deploy and run your Slack bots on a Mac or iPhone, and SlackKit wasn’t compatible with Linux — until now.
Zewo to Sixty on Linux
Even in the rapidly changing world of technology, the server-side Swift ecosystem is very new. Apple’s Linux Foundation port is a huge undertaking, as is the work to get libdispatch, one of the main concurrency frameworks that Foundation relies upon, up and running on the platform. Fortunately, a vibrant ecosystem of open source software has emerged to fill in the gaps left by Apple’s official libraries. In researching the possibilities for Swift on Linux, I discovered the open source organization Zewo, a large part of this budding community. If this sounds interesting to you, you should get involved! (Oh, and of course they have a Slack)
Do You Want to Build a Slack Bot?
The following is a step-by-step guide to writing a Slack bot in Swift and deploying to Heroku. The focus here is on macOS but this is also doable on Linux — just skip the Xcode steps and use your editor of choice.
[Update 1/2/2017: I’ve updated these instructions to work with Xcode 8.2.1 and the official release of Swift 3.]
Building the Application
For our example application, we’re going to be making an application that can render judgement on a very specific question: Robot or Not?
First, we need to create the directory for our application and initialize the basic project structure.
mkdir robot-or-not-bot && cd robot-or-not-bot
swift package init --type executable
Next, let’s edit our
Package.swift file to add the SlackKit package as a dependency:
and generate our development environment:
swift package generate-xcodeproj
Show Me the Swift Code!
To create our bot, we need to open the
robot-or-not-bot.xcodeproj file we just generated, and edit the
main.swift file in Sources > robot-or-not-bot to contain our bot logic. The following code uses SlackKit to listen for messages directed at our bot and then respond to them by adding a reaction to the inquiry.
Setting Up Your Slack Bot
Next, we need to create a bot integration in Slack. You’ll need a Slack that you have administrator access to; if you don’t already have one of those to play with, go sign up. Slack is free for small teams.
- Go here: https://my.slack.com/services/new/bot
- Enter a name for your bot. We’re going to go with “robot-or-not-bot” so there’s no confusion about our bot’s sole purpose in life.
- Click “Add Bot Integration”
- Copy the API token that Slack generates and replace our placeholder token in
main.swiftwith the real deal.
With our bot token in place, we’re ready to do some local testing! Back in Xcode, select the robot-or-not-bot application executable target and run your bot (⌘+R).
Then head over to Slack; robot-or-not-bot’s user presence indicator should be filled in. It’s alive!
To test if it’s working, ask it if something is a robot:
@robot-or-not-bot Darth Vader?
robot-or-not-bot should add the 🚫 reaction in response to your question, letting you know that Darth Vader is not a robot.
Deploying to the ☁️
Now that it’s working locally, it’s time to deploy. To the cloud! We’re going to be deploying on Heroku, so if you don’t have an account go and sign up for a free one.
First, we need to add a Procfile for Heroku. Back in the terminal, run:
echo slackbot: .build/release/robot-or-not-bot > Procfile
Next, let’s check in our code:
git add .
git commit -am'robot-or-not-bot powering up'
Finally, we’ll setup Heroku:
- Install the Heroku toolbelt
- Log in to Heroku in your terminal:
3. Create our application on Heroku and set our buildpack:
heroku create --buildpack https://github.com/kylef/heroku-buildpack-swift robot-or-not-bot
4. Set up our Heroku remote:
heroku git:remote -a robot-or-not-bot
5. Push to master:
git push heroku master
At this point, you’ll see Heroku go through the build process — exciting!
Once the build is complete, run:
heroku run:detached slackbot
Over in Slack, you’ll see robot-or-not-bot’s user presence indicator fill in. It’s alive! (again)
Just to be sure if it’s working, we should ask it an existential question:
@robot-or-not-bot Robot Or Not Bot?
robot-or-not-bot will (sadly, I imagine) add the 🚫 reaction to your question — it knows it is just a computer program, not a robot.
🎊 You’re Done! 🎊
Congratulations, you’ve successfully built and deployed a Slack bot written in Swift on to a Linux server!