CodeX
Published in

CodeX

Creating a Slack bot (Kotlin) and deploying it in Heroku

This is the minimum needed to create and deploy an app that replies to Slack commands.

Photo by Brett Jordan on Unsplash

1. Init project

  1. Create a new Kotlin project.
  2. If you haven’t yet made git init, you should do it now. Heroku needs Git so that you can easily push to deploy to Heroku.
  3. Make sure you have a .gitignore file at the root with:
/.gradle
/.idea
/build

2. Heroku app

We need to create a Heroku app which will serve our Kotlin app created before. This was based on the official Heroku docs.

  1. Install the Heroku CLI. This allows a quick way to interact with Heroku— using the terminal.
  2. Create a Heroku app by running:
    heroku apps:create HEROKU_APP_NAME
    HEROKU_APP_NAME needs to be unique. This will bind the current Git repository to the Heroku app. You can confirm that the app exists at Heroku Dashboard. You can check that Git is set up by running git remote -v in the terminal.
  3. On the Kotlin app’s root folder, create a file named Procfile with the following content:
    web: java -jar build/libs/PROJ_ID-1.0-SNAPSHOT-all.jar
    (to get PROJ_ID, check settings.gradle.kts).
    This represents the command that Heroku runs to launch the web app.
  4. Heroku requires a stage target. At the bottom of build.gradle.kts, add:
task("stage") {
dependsOn("shadowJar")
}

3. Slack Bolt app

  1. Add the following dependencies to build.gradle.kts:
dependencies {
implementation("org.slf4j:slf4j-simple:1.+")
implementation
("com.slack.api:bolt:1.+")
implementation("com.slack.api:bolt-servlet:1.+")
implementation("com.slack.api:bolt-jetty:1.+")
}

2. Add Gradle Shadow build.gradle.kts (this will enable creating a single fat jar that can be run later in Heroku):

plugins {
id("com.github.johnrengelman.shadow") version "7.+"
application
}
// at the bottom:
application {
mainClass.set("SlackMainKt")
}

📝 You may need to click the buttons to refresh Gradle, in the top-right corner of the file in the IDE.

3. At src/main/kotlin/SlackMain.kt, put:

fun main() {
val app = App()
app.command("/hello") { _, _ ->
Response.ok("world")
}
SlackAppServer(app, getenv("PORT")?.toInt() ?: 8080)
.start()
}

📝 PORT is an environment variable provided by Heroku. It represents the port that Heroku exposes. Therefore, we need to serve our app at that port.

4. Commit all the local Git changes.

5. Run git push heroku to deploy the app into Heroku (in fact, the first push needs another command, but the message will tell which).

4. Slack app configuration

Based on the official docs: Building an app with Bolt for Java and Getting Started with Bolt.

  1. Go to Slack Apps and click “Create New App”.
  2. Pick “From scratch”, click “Next”, name the app and assign it to a workspace.
  3. On the left-side menu, go to “OAuth & Permissions”, scroll to “Scopes” and add chat:write.
  4. On the menu, click “Slack Commands” then “Create New Command”.
  5. Fill in the details with:
    Command: hello
    Request URL: BASE_APP_URL/slack/events
    To get BASE_APP_URL, run heroku apps:info in the terminal (it’s the Web URL).
    Short Description: hello world
    Save the changes.
  6. We need to copy the Slack environment variables from Slack UI to Heroku. To do it, go to “Basic Information”, “App Credentials”, “Signing Secret”, click “Show” and copy it. Go to Heroku Dashboard, pick the created app, “Settings”, “Reveal Config Vars”, and add a new one named SLACK_SIGNIN_SECRET with the value copied from before.
  7. Going back to the Slack UI, go to “OAuth & Permissions”, and copy the value at “Bot User OAuth Token”. Go back to Heroku and create another new env var named SLACK_BOT_TOKEN and paste the copied value.
  8. On the Slack UI menu, go to “Basic Information” and click the “Install to workspace” button. Confirm the app installation in the intended workspace.
  9. Time to test the app! Go to Slack itself, type /hello in any channel, and press Enter. You should get world as reply.

📝 If there are issues, you can invoke heroku logs --tail in the terminal.

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Application Frameworks

What is Generic in C# ?

在Windows上安裝Python和 Django 然後搭配 Pycharm Ide 來撰寫專案

A Case Study into Usage of Redis Data Structures in an E-Commerce Application

Tessitura for developers — part 3, designing the select seats page

Web Scrape Twitter by Python Selenium (Part 2)

Multi GCP environments with Terraform: from local backend to TF Cloud

Instance-Volume Attachment using Terraform

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
Luís Soares

Luís Soares

I write about Lean, TDD, CI/CD, trunk-based dev., user-centric dev, DDD, coding good practices, testing

More from Medium

TDD — Simple Demonstration in Kotlin

Speeding up Kotlin Multiplatform

Create a LazyColumn With Indexing for Jetpack Compose

Kotlin tries to Delegate