

đ€ 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! The result is SlackKit, a universal Swift framework for building Slack apps.
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 6/4/2017: Iâve updated these instructions to work with Xcode 8.3.2 and the official release of Swift 3.1.1.]
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-botswift 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.
Testing Locally
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, and a .swift-version file for swiftenv. Back in the terminal, run:
echo slackbot: .build/release/robot-or-not-bot > Procfile
echo 3.1.1 > .swift-version
Next, letâs check in our code:
git init
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:
heroku login
3. Create our application on Heroku and set our buildpack (youâll need to use a unique name like robot-or-not-bot23 since Heroku app names are unique):
heroku create --buildpack https://github.com/kylef/heroku-buildpack-swift robot-or-not-bot
4. Set up our Heroku remote (use your unique name here, too):
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!


Run It!
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!