Begin of command parser
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
package eu.fosil.okupamicoche
|
||||
|
||||
import eu.fosil.okupamicoche.services.EventTnxService
|
||||
import eu.fosil.okupamicoche.services.RoomService
|
||||
import eu.fosil.okupamicoche.services.UserService
|
||||
import mu.KotlinLogging
|
||||
import net.folivo.trixnity.appservice.rest.AppserviceService
|
||||
import net.folivo.trixnity.appservice.rest.DefaultAppserviceService
|
||||
import net.folivo.trixnity.client.api.MatrixApiClient
|
||||
import net.folivo.trixnity.core.model.events.Event
|
||||
import net.folivo.trixnity.core.model.events.m.room.CreateEventContent
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
fun createAppService(matrixApiClient:MatrixApiClient): AppserviceService {
|
||||
|
||||
val appserviceService = DefaultAppserviceService(
|
||||
EventTnxService(), UserService(matrixApiClient), RoomService(matrixApiClient)
|
||||
)
|
||||
|
||||
appserviceService.subscribeAllEvents {
|
||||
logger.debug("All events: $it")
|
||||
}
|
||||
appserviceService.subscribe<CreateEventContent> {
|
||||
if (it is Event.RoomEvent) {
|
||||
logger.info("${it.content.creator} created room ${it.roomId}")
|
||||
}
|
||||
}
|
||||
appserviceService.subscribe<RoomMessageEventContent.TextMessageEventContent> {
|
||||
if (it is Event.MessageEvent) {
|
||||
val roomId = it.roomId
|
||||
logger.info("${it.sender} sent \"${it.content.body}\" on $roomId")
|
||||
logger.info("$it")
|
||||
|
||||
if (it.content.body.startsWith("!travel")) {
|
||||
logger.info("send!")
|
||||
matrixApiClient.rooms.joinRoom(roomId)
|
||||
matrixApiClient.rooms.sendMessageEvent(
|
||||
roomId,
|
||||
RoomMessageEventContent.TextMessageEventContent("Te creo un viaje?")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return appserviceService
|
||||
}
|
||||
30
src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt
Normal file
30
src/main/kotlin/eu/fosil/okupamicoche/cli/CommandParser.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package eu.fosil.okupamicoche.cli
|
||||
|
||||
import net.folivo.trixnity.core.model.RoomId
|
||||
import net.folivo.trixnity.core.model.UserId
|
||||
|
||||
object CommandParser {
|
||||
|
||||
fun isCommand(body: String) = body.matches("![a-zA-Z]+( .*)?".toRegex())
|
||||
|
||||
fun parse(
|
||||
user: UserId,
|
||||
room: RoomId,
|
||||
body: String
|
||||
) {
|
||||
val words = Regex("[a-zA-Z]+").findAll(body)
|
||||
val command = words.first().value
|
||||
val args = words.drop(1).toList().map { sequence -> sequence.value }
|
||||
handleCommand(user, room, command, args)
|
||||
}
|
||||
|
||||
private fun handleCommand(
|
||||
user: UserId,
|
||||
room: RoomId,
|
||||
command: String,
|
||||
args: List<String>
|
||||
) {
|
||||
println("command=$command args=$args")
|
||||
}
|
||||
|
||||
}
|
||||
54
src/main/kotlin/eu/fosil/okupamicoche/createAppService.kt
Normal file
54
src/main/kotlin/eu/fosil/okupamicoche/createAppService.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
package eu.fosil.okupamicoche
|
||||
|
||||
import eu.fosil.okupamicoche.cli.CommandParser
|
||||
import eu.fosil.okupamicoche.services.EventTnxService
|
||||
import eu.fosil.okupamicoche.services.RoomService
|
||||
import eu.fosil.okupamicoche.services.UserService
|
||||
import mu.KotlinLogging
|
||||
import net.folivo.trixnity.appservice.rest.AppserviceService
|
||||
import net.folivo.trixnity.appservice.rest.DefaultAppserviceService
|
||||
import net.folivo.trixnity.client.api.MatrixApiClient
|
||||
import net.folivo.trixnity.core.model.events.Event
|
||||
import net.folivo.trixnity.core.model.events.m.room.CreateEventContent
|
||||
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
fun createAppService(matrixApiClient: MatrixApiClient): AppserviceService {
|
||||
|
||||
return DefaultAppserviceService(
|
||||
EventTnxService(), UserService(matrixApiClient), RoomService(matrixApiClient)
|
||||
).apply {
|
||||
subscribeAllEvents {
|
||||
logger.debug("All events: $it")
|
||||
}
|
||||
subscribe<CreateEventContent> {
|
||||
if (it is Event.RoomEvent) {
|
||||
logger.info("${it.content.creator} created room ${it.roomId}")
|
||||
}
|
||||
}
|
||||
subscribe<RoomMessageEventContent.TextMessageEventContent> {
|
||||
if (it is Event.MessageEvent) {
|
||||
val roomId = it.roomId
|
||||
val body = it.content.body
|
||||
logger.info("${it.sender} sent \"${it.content.body}\" on $roomId")
|
||||
logger.info("$it")
|
||||
|
||||
if (CommandParser.isCommand(body)) {
|
||||
CommandParser.parse(
|
||||
user = it.sender,
|
||||
roomId,
|
||||
body
|
||||
)
|
||||
// matrixApiClient.rooms.joinRoom(roomId)
|
||||
// matrixApiClient.rooms.sendMessageEvent(
|
||||
// roomId,
|
||||
// RoomMessageEventContent.TextMessageEventContent("Viaje creado!")
|
||||
// )
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
src/main/kotlin/eu/fosil/okupamicoche/model/Travel.kt
Normal file
21
src/main/kotlin/eu/fosil/okupamicoche/model/Travel.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package eu.fosil.okupamicoche.model
|
||||
|
||||
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||
|
||||
object Travel: IntIdTable() {
|
||||
val roomId = varchar("room_id", 255)
|
||||
val driver = varchar("driver", 255)
|
||||
val origin = varchar("origin", 255)
|
||||
val destination = varchar("destination", 255)
|
||||
val time = integer("time")
|
||||
val places = integer("places")
|
||||
val description = varchar("destination", 1023)
|
||||
}
|
||||
|
||||
//object StarWarsFilms : Table() {
|
||||
// val id: Column<Int> = integer("id").autoIncrement()
|
||||
// val sequelId: Column<Int> = integer("sequel_id").uniqueIndex()
|
||||
// val name: Column<String> = varchar("name", 50)
|
||||
// val director: Column<String> = varchar("director", 50)
|
||||
// override val primaryKey = PrimaryKey(id, name = "PK_StarWarsFilms_Id") // PK_StarWarsFilms_Id is optional here
|
||||
//}
|
||||
Reference in New Issue
Block a user