Skip to content

Instantly share code, notes, and snippets.

@seratch
Last active June 6, 2021 07:34
Show Gist options
  • Save seratch/1a9bfffe4a25eb6cff8738bdb329e9f6 to your computer and use it in GitHub Desktop.
Save seratch/1a9bfffe4a25eb6cff8738bdb329e9f6 to your computer and use it in GitHub Desktop.
Slack app built with Bolt for Java (beta) and Quarkus

Bolt for Java

Running the application in dev mode

You can run your application in dev mode that enables live coding using:

brew install maven
mvn quarkus:dev

Packaging and running the application

The application is packageable using ./mvnw package. It produces the executable quarkus-slack-app-1.0.0-SNAPSHOT-runner.jar file in /target directory. Be aware that it’s not an über-jar as the dependencies are copied into the target/lib directory.

The application is now runnable using java -jar target/quarkus-slack-app-1.0.0-SNAPSHOT-runner.jar.

package hello
import com.slack.api.bolt.App
import com.slack.api.bolt.response.Responder
import com.slack.api.bolt.servlet.SlackAppServlet
import javax.enterprise.inject.Produces
import javax.inject.Inject
import javax.servlet.annotation.WebServlet
import com.slack.api.model.view.Views.*
import com.slack.api.model.block.Blocks.*
import com.slack.api.model.block.composition.BlockCompositions.*
import com.slack.api.model.block.element.BlockElements.*
@WebServlet("/slack/events")
class SlackApp(app: App?) : @Inject SlackAppServlet(app)
class Components {
@Produces
fun initApp(): App {
val app = App()
app.command("/ping") { req, ctx ->
ctx.ack("<@${req.payload.userId}> pong!")
}
app.command("/open-modal") { req, ctx ->
val privateMetadata = req.payload.responseUrl
val modal = view { view -> view
.type("modal")
.callbackId("request-modal")
.title(viewTitle { it.type("plain_text").text("Request Form") })
.submit(viewSubmit { it.type("plain_text").text("Submit") })
.close(viewClose { it.type("plain_text").text("Cancel") })
.privateMetadata(privateMetadata)
.blocks(asBlocks(
input { it
.blockId("request-block")
.element(plainTextInput { it.actionId("request-action").multiline(true) })
.label(plainText { it.text("Detailed Request") })
}
))
}
val viewsOpenResult = ctx.client().viewsOpen { it
.triggerId(req.payload.triggerId)
.view(modal)
}
if (viewsOpenResult.isOk) {
ctx.ack()
} else {
ctx.ack(":x: Failed to open a modal (error: ${viewsOpenResult.error})")
}
}
app.viewSubmission("request-modal") { req, ctx ->
val request = req.payload.view.state.values["request-block"]!!["request-action"]!!.value
if (request == null || request.trim().isEmpty()) {
ctx.ackWithErrors(mapOf("request-block" to "write the request here"))
} else if (request.length < 10) {
ctx.ackWithErrors(mapOf("request-block" to "must have more than 10 characters"))
} else {
val responseUrl = req.payload.view.privateMetadata
val apiResponse = Responder(ctx.slack, responseUrl).sendToCommand { it.text(request) }
if (apiResponse.code == 200) {
ctx.logger.error("Failed to send a message (error: ${apiResponse.message})")
}
ctx.ack()
}
}
return app
}
}
quarkus.http.port=3000
quarkus.log.level=INFO
quarkus.log.category."com.slack.api".level=DEBUG
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.seratch</groupId>
<artifactId>bolt-with-quarkus</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<kotlin.version>1.3.61</kotlin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>1.2.1.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.2.1.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
</properties>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-undertow</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kotlin</artifactId>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt</artifactId>
<version>0.99.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemProperties>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
</systemProperties>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<configuration>
<javaParameters>true</javaParameters>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=javax.ws.rs.Path</option>
<option>all-open:annotation=javax.enterprise.context.ApplicationScoped</option>
<option>all-open:annotation=io.quarkus.test.junit.QuarkusTest</option>
</pluginOptions>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
@v0112358
Copy link

v0112358 commented Jun 6, 2021

Do you have a demo Quarkus and Slack Bolt in socket mode ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment