Getting started with Chameleon: the Swift Slack bot!

Ian Keen
4 min readJun 21, 2016

--

Setting up a Swift Slack bot on Heroku!

Note: This article/series is deprecated, There has been a lot of changes in the Chameleon framework. I will produce a new set of articles soon.. sorry for not completing this series.

What is Chameleon?

Chameleon is an open-source, extensible Slack bot built in Swift. It is capable of running on both macOS and Linux. Instructions for both are on the repository but today I am going to take you through getting setup on Heroku in a bit more detail.

Prerequisites

First, you will need to create a Slack bot user for your team by following this link. You will see the following:

Choose a name for your bot and add the integration. You will be presented with the Integration Settings for your bot. Configure these to your liking but make sure you take note of the API Token; you will need it later.

Next, if you don’t already have one, create a Heroku account (a free one will be sufficient) and make sure you have the Heroku Toolbelt installed.

Open up a terminal window and create a new folder for your bot. Clone the repository with:

git clone git@github.com:IanKeen/Chameleon.git
cd Chameleon

Next we will log into Heroku and create our app:

heroku login
(you will be prompted for your username and password)
heroku create <name>

This will create a new app for you on Heroku and add a git remote which we will use later to deploy the bot. Next head over to the Heroku dashboard and navigate into your new app. Under the Settings tab you will see a Reveal Config Vars button:

After tapping the button you will be able to enter custom environment variables for your bot. We need two variables here, one named TOKEN which will contain the Slack token from earlier; as well as a variable for the Swift build configuration named SWIFT_BUILD_CONFIGURATION whose value will be “debug”:

Staying in Settings, scroll down to find the Buildpacks section. Here we will add a new buildpack using the url: https://github.com/oarrabi/heroku-buildpack-swift and save the changes.

Back in the terminal we will create a Procfile for Heroku to use:

echo "worker: App" >> Procfile

This file tells Heroku what to execute after each deploy, in this case the command “worker: App” is telling it to execute the program “App” configured as a worker.

Deploying

At this point you will have a bot that is ready to go! You can deploy to Heroku with the following terminal commands:

git add .
git commit -am 'deploy to heroku'
git push heroku master

Anytime you make a change to your bot’s code you will need to commit and push to Heroku.

This might take a few minutes. It needs to upload, compile and execute on Heroku. Once that is complete the last step is to type:

heroku ps:scale worker=1

This tells Heroku to allocate 1 dyno to run your Slack bot and you will receive confirmation of the change. After a small delay your bot will connect.

You should only need to do this once, if you ever need to stop the bot use worker=0 to reduce the dyno count to zero. Subsequently using worker=1 will start the bot up again.

Hello Chameleon

If everything went according to plan you should see your new bot join your Slack team!

You can invite it to any channel by typing /invite @yourbot

Finally, say hello!

What’s Next?

Currently your bot will not be able to persist any data, it’s only using memory for storage. Next time I’ll take you through adding a Redis store to Heroku and telling your bot to use that to make sure you don’t lose any data!

Be sure to check out the repository.

If you like the project feel free to recommend or retweets! 😄

You can catch me on Twitter and iOS Developers.

--

--

Ian Keen

Developer, Snowboarder (not at the same time unfortunately) — All posts here are my own