70 lines
No EOL
2.1 KiB
Kotlin
70 lines
No EOL
2.1 KiB
Kotlin
import io.javalin.Context
|
|
import io.javalin.Javalin
|
|
import org.slf4j.LoggerFactory
|
|
|
|
fun main() {
|
|
val logger = LoggerFactory.getLogger("main")
|
|
|
|
val app = Javalin.create().start(7000)
|
|
app.get("/") { ctx -> ctx.result("Hello World") }
|
|
|
|
app.post("/slack/receive") {
|
|
ctx -> handleSlackEvent(ctx)
|
|
}
|
|
|
|
app.post("/api/v1/spongebob") {
|
|
ctx -> ctx.json(TextResponse(200, ctx.body().toSpongemock()))
|
|
}
|
|
|
|
app.post("/api/v1/uppercase") {
|
|
ctx -> ctx.json(TextResponse(200, ctx.body().toUpperCase()))
|
|
}
|
|
}
|
|
|
|
fun handleSlackEvent(ctx: Context) {
|
|
|
|
// Short circuit for ssl check
|
|
if ((ctx.formParam("ssl_check") ?: 0) == "1") {
|
|
ctx.status(200)
|
|
return
|
|
}
|
|
|
|
val responseObj = RespObj(
|
|
command = ctx.formParamMap().get("command")?.get(0).processtoCommand(),
|
|
text = ctx.formParamMap().get("text")?.get(0) ?: "",
|
|
response_url = ctx.formParamMap().get("response_url")?.get(0) ?: "",
|
|
team_id = ctx.formParamMap().get("team_id")?.get(0) ?: "",
|
|
channel_id = ctx.formParamMap().get("channel_id")?.get(0) ?: ""
|
|
)
|
|
|
|
sendResponse(ctx, responseObj)
|
|
}
|
|
|
|
private fun String?.processtoCommand(): Commands {
|
|
return when (this) {
|
|
"/spongemock2" -> Commands.Spongebob
|
|
"/uppercase" -> Commands.Uppercase
|
|
else -> Commands.Error
|
|
}
|
|
}
|
|
|
|
fun sendResponse(ctx: Context, respObj: RespObj) {
|
|
val returnMap = mutableMapOf<String, String>()
|
|
with(returnMap) {
|
|
when (respObj.command) {
|
|
Commands.Spongebob -> {put("text", respObj.text.toSpongemock()); put("response_type", "in_channel")}
|
|
Commands.Uppercase -> {put("text", respObj.text.toUpperCase()); put("response_type", "in_channel")}
|
|
Commands.Error -> {put("text", respObj.text); put("response_type", "ephemeral")}
|
|
}
|
|
|
|
}
|
|
ctx.json(returnMap)
|
|
}
|
|
|
|
data class TextResponse(val status: Int, val response: String)
|
|
|
|
private fun String.toSpongemock(): String {
|
|
return this.mapIndexed { index, c -> if (!index.isOdd()) { c.toUpperCase() } else c }.joinToString(separator = "")
|
|
}
|
|
|
|
private fun Int.isOdd(): Boolean = this.rem(2) == 0 |