[!important]
此项目的开发已迁移至 SaltifyDev/saltify 仓库下。
Milky SDK for Kotlin

特性
- 基于 Ktor Client 和 Kotlinx Serialization 实现
- 兼容 JVM / Native / JS / WASM 平台
- 支持 Milky 协议的所有功能
- 例外:不支持通过 WebHook 事件推送监听事件
[!tip]
使用时,你需要在项目中添加一个 Ktor Client 引擎依赖,例如 ktor-client-cio、ktor-client-okhttp 等。
用例
见 ClientTest.kt
初始化
val client = MilkyClient {
addressBase = "http://localhost:3000"
eventConnectionType = EventConnectionType.WebSocket
plugin("name") {
}
install(myPlugin)
}
client.close()
定义插件
val myPlugin = milkyPlugin {
onStart {
}
command("say") {
val content = greedyStringParameter("content", "words to repeat")
onExecute {
respond {
text(content.value)
}
}
onFailure {
respond {
text("Command run failed: $it")
}
}
}
}
调用 API
val loginInfo = client.getLoginInfo()
client.sendGroupMessage(123456789L) {
text("Hello from Milky🥛!")
image("https://example.com/example.jpg")
image("https://example.com/example2.jpg", subType = "sticker")
}
监听事件
client.connectEvent()
val job = launch {
client.on<Event.MessageReceive> {
when (val = it.) {
IncomingMessage.Group -> {
println()
println(milkyJsonModule.encodeToString(.segments))
}
IncomingMessage.Friend -> {
println()
println(milkyJsonModule.encodeToString(.segments))
}
-> {}
}
}
}
job.cancel()
client.disconnectEvent()
异常处理
runBlocking {
client.on<IllegalStateException> { _, e ->
println("Receive exception: ${e.message}")
if (e.message != "test exception") {
this@runBlocking.cancel(CancellationException(e.message, e))
}
}
}