Skip to content

Commit

Permalink
Allows chat service implementations to indicate support for user term…
Browse files Browse the repository at this point in the history
…ination. Adds REST API store example.
  • Loading branch information
outofcoffee committed Feb 4, 2018
1 parent bc30157 commit bb02607
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 30 deletions.
17 changes: 17 additions & 0 deletions bots/common/src/main/kotlin/com/gatehill/corebot/bot/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.google.inject.Guice.createInjector
import com.google.inject.Module
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.inject.Inject

/**
Expand All @@ -18,10 +20,25 @@ class Bot @Inject constructor(private val chatService: ChatService) {
*/
fun start() {
chatService.listenForEvents()

if (chatService.supportsUserTermination) {
try {
val reader = BufferedReader(InputStreamReader(System.`in`))
println("Press <ENTER> to terminate.")
reader.readLine()
} catch (e: Exception) {
e.printStackTrace()
} finally {
stop()
}
}
}

fun stop() {
println("Terminating...")
chatService.stopListening()
println("Bye!")
System.exit(0)
}

/**
Expand Down
16 changes: 1 addition & 15 deletions bots/http-slack/src/main/kotlin/com/gatehill/corebot/bot/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,10 @@ import com.gatehill.corebot.asSingleton
import com.gatehill.corebot.backend.slack.SlackDriverModule
import com.gatehill.corebot.frontend.http.HttpModule
import com.google.inject.AbstractModule
import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array<String>) {
println("Warning: the HTTP bot is experimental.")

val bot = Bot.build(BotModule(), HttpModule())
bot.start()

try {
val reader = BufferedReader(InputStreamReader(System.`in`))
println("Please press a key to stop the server.")
reader.readLine()
} catch (e: Exception) {
e.printStackTrace()
} finally {
bot.stop()
}
Bot.build(BotModule(), HttpModule()).start()
}

private class BotModule : AbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@ import com.gatehill.corebot.backend.items.ItemsDriverModule
import com.gatehill.corebot.frontend.websocket.WebSocketModule
import com.gatehill.corebot.frontend.websocket.chat.endpoint.CustomConfigurator
import com.google.inject.AbstractModule
import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array<String>) {
println("Warning: the WebSocket bot is experimental.")

val bot = Bot.build(BotModule(), WebSocketModule())
bot.start()

try {
val reader = BufferedReader(InputStreamReader(System.`in`))
println("Please press a key to stop the server.")
reader.readLine()
} catch (e: Exception) {
e.printStackTrace()
} finally {
bot.stop()
}
Bot.build(BotModule(), WebSocketModule()).start()
}

private class BotModule : AbstractModule() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.gatehill.corebot.chat
* @author Pete Cornish {@literal <outofcoffee@gmail.com>}
*/
interface ChatService {
val supportsUserTermination: Boolean
fun listenForEvents()
fun stopListening()
}
13 changes: 13 additions & 0 deletions examples/plugins/websocket-items-restapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
frontends:
- dependency: com.gatehill.corebot:websocket-frontend-plugin:war:$VERSION
classes:
- com.gatehill.corebot.frontend.websocket.WebSocketModule

backends:
- dependency: com.gatehill.corebot:items-backend-plugin:war:$VERSION
classes:
- com.gatehill.corebot.backend.items.ItemsDriverModule

storage:
- dependency: com.gatehill.corebot:restapi-store-plugin:war:$VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ open class HttpChatServiceImpl @Inject constructor(private val factoryService: F
private val sessionService: HttpSessionService,
private val messageService: MessageService) : ChatService {

override val supportsUserTermination = true

private var server: HttpServer? = null

private fun configureRoutes(vertx: Vertx, router: Router) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import javax.inject.Inject
open class SlackChatServiceImpl @Inject constructor(private val sessionService: SlackSessionService,
private val messageService: MessageService) : ChatService {

/**
* The Slack library effectively acts as daemon.
*/
override val supportsUserTermination = false

private val logger: Logger = LogManager.getLogger(SlackChatServiceImpl::class.java)
private val messageMatcher = Pattern.compile("<@(?<botUser>[a-zA-Z0-9]+)>:?(\\s(?<command>.+))?")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.glassfish.tyrus.server.Server
* @author Pete Cornish {@literal <outofcoffee@gmail.com>}
*/
open class WebSocketChatServiceImpl : ChatService {
override val supportsUserTermination = true

private val server: Server by lazy { Server(hostname, port, "/", setOf(BotWebSocketEndPoint::class.java)) }

override fun listenForEvents() {
Expand Down

0 comments on commit bb02607

Please sign in to comment.