Ready for the bot uprising?

Building Slack Bots In Swift

As a remote iOS developer, I love Slack. Its 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?

[Update 11/14/2019: Ive updated these instructions to work with Xcode 11.2 and the official release of Swift 5.1.1.]

Building the Application

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, lets edit our Package.swift file to add the SlackKit package as a dependency:

Show Me the Swift Code!

Setting Up Your Slack Bot

  1. Go here:
  2. Enter a name for your bot. Were going to go with robot-or-not-bot so theres no confusion about our bots sole purpose in life.
  3. Click Add Bot Integration
  4. Copy the API token that Slack generates and replace our placeholder token in main.swift with the real deal.

Testing Locally

Then head over to Slack; robot-or-not-bots user presence indicator should be filled in. Its alive!

To test if its 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 儭

First, we need to add a Procfile for Heroku, and a .swift-version file for swiftenv. Back in the terminal, run:

echo slackbot: robot-or-not-bot > Procfile
echo 5.1.1 > .swift-version

Next, lets check in our code:

git init
git add .
git commit -am'robot-or-not-bot powering up'

Finally, well setup Heroku:

  1. Install the Heroku toolbelt
  2. Log in to Heroku in your terminal:
heroku login

3. Create our application on Heroku and set our buildpack (youll need to use a unique name like robot-or-not-bot23 since Heroku app names are unique):

heroku create --buildpack vapor/vapor robot-or-not-bot

4. Set our configuration environment variable to release:

heroku config:set -a robot-or-not-bot SWIFT_BUILD_CONFIGURATION=release

5. Set up our Heroku remote:

heroku git:remote -a robot-or-not-bot 

6. Push to master:

git push heroku master

At this point, youll see Heroku go through the build process exciting!

Run It!

heroku run:detached slackbot

Over in Slack, youll see robot-or-not-bots user presence indicator fill in. Its alive! (again)

All systems operational

Just to be sure if its 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.

Youre Done!

Built With:

You'll find me hiding behind a pair of sunglasses.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store