Skip to content

Commit 95cde5d

Browse files
committed
feat: add message event handling
refactor!: Upgrading to Micronaut 2.5.3
1 parent 6cda679 commit 95cde5d

51 files changed

Lines changed: 1293 additions & 98 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle.kts

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
plugins {
2-
val pluginVersion by System.getProperties()
4+
val kotlinVersion: String by System.getProperties()
5+
val micronautPluginVersion: String by System.getProperties()
6+
val dokkaVersion: String by System.getProperties()
7+
8+
id("io.micronaut.library") version micronautPluginVersion
9+
10+
kotlin("jvm") version kotlinVersion
11+
kotlin("kapt") version kotlinVersion
12+
kotlin("plugin.allopen") version kotlinVersion
13+
id("org.jetbrains.dokka") version dokkaVersion
314

4-
id("io.micronaut.library") version "1.1.0"
5-
id("org.jetbrains.dokka") version "1.4.20"
6-
id("org.jetbrains.kotlin.jvm") version "$pluginVersion"
7-
id("org.jetbrains.kotlin.kapt") version "$pluginVersion"
8-
id("org.jetbrains.kotlin.plugin.allopen") version "$pluginVersion"
915
id("groovy")
1016
id("maven-publish")
1117

@@ -14,51 +20,67 @@ plugins {
1420
version = "0.1.0"
1521
group = "org.microjservice.lark"
1622

17-
val kotlinVersion = project.properties["kotlinVersion"]
1823
val jdkVersion: String by project
1924
val micronautVersion: String by project
25+
val micronautTestVersion: String by project
26+
val jakartaAnnotationVersion: String by project
27+
val jupiterEngineVersion: String by project
28+
val mockkVersion: String by project
29+
val kotestVersion: String by project
30+
31+
val dokkaVersion: String by System.getProperties()
2032

2133
micronaut {
2234
version(micronautVersion)
35+
36+
runtime("jetty")
37+
// runtime("netty")
38+
2339
}
2440

41+
42+
2543
repositories {
26-
jcenter()
2744
mavenCentral()
2845
}
2946

47+
48+
3049
dependencies {
3150
kapt(platform("io.micronaut:micronaut-bom:$micronautVersion"))
3251
kapt("io.micronaut:micronaut-inject-java")
3352
kaptTest("io.micronaut:micronaut-inject-java")
34-
testAnnotationProcessor("io.micronaut:micronaut-inject-java")
35-
dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.4.20")
53+
testAnnotationProcessor("io.micronaut", "micronaut-inject-java")
54+
dokkaHtmlPlugin("org.jetbrains.dokka", "kotlin-as-java-plugin", dokkaVersion)
3655

3756
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
38-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
57+
58+
runtimeOnly("io.micronaut:micronaut-http-server-netty")
59+
implementation("io.micronaut:micronaut-http-server")
3960

4061
implementation("io.micronaut", "micronaut-http-client")
62+
implementation("jakarta.inject", "jakarta.inject-api", jakartaAnnotationVersion)
4163

4264
runtimeOnly("ch.qos.logback:logback-classic")
65+
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
66+
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
4367

44-
testImplementation ("io.micronaut.test:micronaut-test-spock")
68+
// implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.+")
69+
implementation("io.github.microutils:kotlin-logging-jvm:2.0.6")
70+
71+
testImplementation("io.micronaut.test:micronaut-test-spock")
4572
testImplementation("org.spockframework:spock-core") {
46-
exclude("org.codehaus.groovy", "groovy-all")
73+
exclude("org.codehaus.groovy", "groovy-all")
4774
}
4875

4976
testImplementation("org.junit.jupiter:junit-jupiter-api")
50-
testImplementation("io.micronaut.test:micronaut-test-junit5:2.3.2")
51-
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
52-
53-
// testImplementation( "io.micronaut.test:micronaut-test-kotest")
54-
// testImplementation( "io.mockk:mockk")
55-
// testImplementation( "io.kotest:kotest-runner-junit5-jvm")
56-
57-
testImplementation("io.micronaut.test:micronaut-test-kotest:2.3.2")
58-
testImplementation("io.mockk:mockk:1.10.4")
59-
testImplementation("io.kotest:kotest-runner-junit5-jvm:4.3.0")
77+
testImplementation("io.micronaut.test", "micronaut-test-junit5", micronautTestVersion)
78+
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", jupiterEngineVersion)
6079

61-
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
80+
testImplementation("io.micronaut.test", "micronaut-test-kotest")
81+
testImplementation("io.mockk", "mockk", mockkVersion)
82+
testImplementation("io.kotest", "kotest-runner-junit5-jvm", kotestVersion)
83+
// implementation(kotlin("stdlib-jdk8"))
6284
}
6385

6486
tasks.withType<Test> {
@@ -67,6 +89,8 @@ tasks.withType<Test> {
6789

6890
java {
6991
sourceCompatibility = JavaVersion.toVersion(jdkVersion)
92+
withJavadocJar()
93+
withSourcesJar()
7094
}
7195

7296
tasks {
@@ -80,4 +104,23 @@ tasks {
80104
jvmTarget = jdkVersion
81105
}
82106
}
107+
}
108+
109+
publishing {
110+
publications {
111+
create<MavenPublication>("mavenJava") {
112+
from(components["java"])
113+
}
114+
}
115+
repositories {
116+
mavenLocal()
117+
}
118+
}
119+
val compileKotlin: KotlinCompile by tasks
120+
compileKotlin.kotlinOptions {
121+
jvmTarget = "1.8"
122+
}
123+
val compileTestKotlin: KotlinCompile by tasks
124+
compileTestKotlin.kotlinOptions {
125+
jvmTarget = "1.8"
83126
}

gradle.properties

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
jdkVersion=1.8
2-
micronautVersion=2.4.0
3-
kotlinVersion=1.4.30
4-
systemProp.pluginVersion=1.4.30
2+
micronautVersion=2.5.3
3+
jakartaAnnotationVersion=2.0.0
4+
jupiterEngineVersion=5.7.0
5+
micronautTestVersion=2.3.2
6+
mockkVersion=1.10.4
7+
kotestVersion=4.3.0
8+
9+
systemProp.kotlinVersion=1.4.32
10+
systemProp.micronautPluginVersion=1.5.0
11+
systemProp.dokkaVersion=1.4.30
512

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,54 @@
11
package org.microjservice.lark
22

3-
import io.micronaut.context.annotation.Value
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.databind.module.SimpleModule
5+
import io.micronaut.context.annotation.Bean
6+
import io.micronaut.context.annotation.Factory
7+
import io.micronaut.context.event.BeanCreatedEvent
8+
import io.micronaut.context.event.BeanCreatedEventListener
9+
import jakarta.inject.Singleton
10+
import org.microjservice.lark.core.event.v2.EventConsumer
11+
import org.microjservice.lark.core.event.v2.model.EventRequest
12+
import org.microjservice.lark.core.event.v2.model.EventRequestDeserializer
13+
import org.microjservice.lark.core.event.v2.model.MessageEvent
14+
import org.slf4j.LoggerFactory
15+
416

517
/**
618
* Configuration for Lark suite.
719
*
820
* @author Coder Yellow
921
* @since 0.1.0
1022
*/
11-
data class AppConfiguration(
12-
@Value("") var endpoint: String
13-
)
23+
@Factory
24+
class AppConfiguration {
25+
26+
27+
@Bean
28+
fun messageEventHandlerV2(): EventConsumer<MessageEvent> {
29+
return object: EventConsumer<MessageEvent> {
30+
31+
val logger = LoggerFactory.getLogger(javaClass)
32+
33+
override val eventType: Class<MessageEvent>
34+
get() = MessageEvent::class.java
35+
36+
override fun handleEvent(eventRequest: EventRequest<MessageEvent>) {
37+
logger.info("Receive $eventRequest")
38+
}
39+
}
40+
}
41+
42+
@Singleton
43+
internal class ObjectMapperBeanEventListener : BeanCreatedEventListener<ObjectMapper?> {
44+
override fun onCreated(event: BeanCreatedEvent<ObjectMapper?>): ObjectMapper {
45+
val mapper: ObjectMapper = event.bean ?: ObjectMapper()
46+
mapper.registerModule(object : SimpleModule() {
47+
init {
48+
addDeserializer(EventRequest::class.java, EventRequestDeserializer())
49+
}
50+
})
51+
return mapper
52+
}
53+
}
54+
}
Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package org.microjservice.lark
22

3+
//import jakarta.inject.Singleton
34
import io.micronaut.context.ApplicationContext
5+
import io.micronaut.runtime.EmbeddedApplication
6+
import io.micronaut.runtime.server.EmbeddedServer
7+
import org.microjservice.lark.api.BotApi
48
import org.microjservice.lark.api.CardMessageApi
59
import org.microjservice.lark.api.ChatApi
6-
import org.microjservice.lark.core.auth.TokenManager
10+
import org.microjservice.lark.api.MessageApi
11+
import org.microjservice.lark.core.auth.AuthorizationApi
712
import org.microjservice.lark.core.auth.models.Credential
13+
import org.microjservice.lark.core.event.v2.EventConsumer
814
import javax.inject.Singleton
915

1016
/**
@@ -15,21 +21,67 @@ import javax.inject.Singleton
1521
*/
1622
@Singleton
1723
class LarkClient(
18-
val cardMessageApi: CardMessageApi,
24+
val authorizationApi: AuthorizationApi,
1925
val chatApi: ChatApi,
26+
val cardMessageApi: CardMessageApi,
27+
val messageApi: MessageApi,
28+
val botApi: BotApi,
2029
) {
30+
var context: ApplicationContext? = null
31+
2132

2233
class Builder {
23-
var credential: Credential? = null
24-
var endpoint: String? = null
34+
35+
companion object {
36+
const val APP_ID_PROPERTY_NAME = "lark.app-id"
37+
const val APP_SECRETE_PROPERTY_NAME = "lark.app-secret"
38+
const val AUTHORIZATION_TYPE_PROPERTY_NAME = "lark.authorization-type"
39+
const val ENDPOINT_PROPERTY_NAME = "lark.endpoint"
40+
}
41+
42+
private var credential: Credential? = null
43+
44+
private var endpoint: String? = null
45+
46+
private lateinit var packages: Array<out String>
47+
48+
private var eventHandlers: MutableList<EventConsumer<Any>> = mutableListOf()
2549

2650
fun withCredential(credential: Credential) = apply { this.credential = credential }
51+
2752
fun withEndpoint(endpoint: String) = apply { this.endpoint = endpoint }
2853

54+
fun withEventHandler(eventHandler: EventConsumer<Any>) = apply { this.eventHandlers.add(eventHandler) }
55+
56+
fun packages(vararg packages: String) = apply { this.packages = packages }
57+
2958
fun build(): LarkClient {
30-
val context = ApplicationContext.run()
31-
return context.getBean(LarkClient::class.java)
59+
val properties = mutableMapOf<String, Any?>().apply {
60+
credential?.let {
61+
put(APP_ID_PROPERTY_NAME, credential?.appId)
62+
put(APP_SECRETE_PROPERTY_NAME, credential?.appSecret)
63+
put(AUTHORIZATION_TYPE_PROPERTY_NAME, credential?.credentialType)
64+
put(ENDPOINT_PROPERTY_NAME, endpoint)
65+
}
66+
}
67+
val context = ApplicationContext
68+
.builder()
69+
.properties(properties)
70+
.singletons(*eventHandlers.toTypedArray())
71+
.packages(*packages)
72+
.start()
73+
74+
if (!context
75+
.getBeansOfType(EventConsumer::class.java)
76+
.isEmpty()
77+
) {
78+
context.getBean(EmbeddedServer::class.java)
79+
.start()
80+
}
81+
82+
val larkClient = context.getBean(LarkClient::class.java)
83+
larkClient.context = context
84+
return larkClient
3285
}
3386
}
34-
3587
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.microjservice.lark.api
2+
3+
import io.micronaut.http.annotation.Get
4+
import io.micronaut.http.client.annotation.Client
5+
import org.microjservice.lark.api.models.BaseResponse
6+
import org.microjservice.lark.api.models.BotInfo
7+
import org.microjservice.lark.api.models.BotResponse
8+
9+
/**
10+
* Bot API
11+
*
12+
* @author Coder Yellow
13+
* @since 0.1.0
14+
*/
15+
@Client(value = "\${lark.endpoint}/bot/v3")
16+
interface BotApi {
17+
@Get("/info")
18+
fun info(): BotResponse
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.microjservice.lark.api
2+
3+
import io.micronaut.context.annotation.Parameter
4+
import io.micronaut.http.MediaType
5+
import io.micronaut.http.annotation.Body
6+
import io.micronaut.http.annotation.Post
7+
import io.micronaut.http.annotation.QueryValue
8+
import io.micronaut.http.client.annotation.Client
9+
import org.microjservice.lark.api.models.Message
10+
11+
/**
12+
* Message API
13+
*
14+
* @author Coder Yellow
15+
* @since 0.1.0
16+
*/
17+
@Client(value = "\${lark.endpoint}/im/v1")
18+
interface MessageApi {
19+
20+
@Post(value = "/messages", produces = [MediaType.APPLICATION_JSON])
21+
fun send(@QueryValue("receive_id_type") receiveIdType: Message.ReceiveIdType, @Body message: Message)
22+
}

src/main/kotlin/org/microjservice/lark/api/models/BaseResponse.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import io.micronaut.core.annotation.Introspected
1010
*/
1111
@Introspected
1212
open class BaseResponse<T>(
13-
val code: Int,
14-
val msg: String,
15-
val data:T,
16-
)
13+
code: Int,
14+
msg: String,
15+
val data: T,
16+
) : Response(code, msg)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.microjservice.lark.api.models
2+
3+
import io.micronaut.core.annotation.Introspected
4+
import java.net.URL
5+
6+
/**
7+
* BotInfo
8+
*
9+
* @author Coder Yellow
10+
* @since 0.1.0
11+
*/
12+
@Introspected
13+
data class BotInfo(
14+
val activateStatus: Int,
15+
val appName: String,
16+
val avatarUrl: URL,
17+
val ipWhiteList: List<String>,
18+
val openId: String,
19+
)
20+
21+

0 commit comments

Comments
 (0)