
Implements Milky protocol client with API calling, event subscription (WebSocket support), Ktor HTTP transport and kotlinx-serialization payload handling; excludes WebHook event push listening.
[!important]
此项目的开发已迁移至 SaltifyDev/saltify 仓库下。
[!tip] 使用时,你需要在项目中添加一个 Ktor Client 引擎依赖,例如
ktor-client-cio、ktor-client-okhttp等。
val client = MilkyClient {
addressBase = "http://localhost:3000"
eventConnectionType = EventConnectionType.WebSocket
// accessToken = "..."
// 直接定义插件
plugin("name") {
// ...
}
// 导入插件
install(myPlugin)
}
// 释放 client
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")
}
}
}
// ...
}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()
// 监听消息事件,并创建 Job 以便后续取消监听
val job = launch {
client.on<Event.MessageReceive> {
when (val data = it.data) {
is IncomingMessage.Group -> {
println("Group message from ${data.senderId} in ${data.group.groupId}:")
println(milkyJsonModule.encodeToString(data.segments))
}
is IncomingMessage.Friend -> {
println("Private message from ${data.senderId}:")
println(milkyJsonModule.encodeToString(data.segments))
}
else -> {}
}
}
}
// 退出时取消监听
job.cancel()
// tips: disconnectEvent() 不会被 client.close() 自动调用
client.disconnectEvent()runBlocking {
client.on<IllegalStateException> { _, e ->
println("Receive exception: ${e.message}")
if (e.message != "test exception") {
this@runBlocking.cancel(CancellationException(e.message, e))
}
}
}[!important]
此项目的开发已迁移至 SaltifyDev/saltify 仓库下。
[!tip] 使用时,你需要在项目中添加一个 Ktor Client 引擎依赖,例如
ktor-client-cio、ktor-client-okhttp等。
val client = MilkyClient {
addressBase = "http://localhost:3000"
eventConnectionType = EventConnectionType.WebSocket
// accessToken = "..."
// 直接定义插件
plugin("name") {
// ...
}
// 导入插件
install(myPlugin)
}
// 释放 client
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")
}
}
}
// ...
}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()
// 监听消息事件,并创建 Job 以便后续取消监听
val job = launch {
client.on<Event.MessageReceive> {
when (val data = it.data) {
is IncomingMessage.Group -> {
println("Group message from ${data.senderId} in ${data.group.groupId}:")
println(milkyJsonModule.encodeToString(data.segments))
}
is IncomingMessage.Friend -> {
println("Private message from ${data.senderId}:")
println(milkyJsonModule.encodeToString(data.segments))
}
else -> {}
}
}
}
// 退出时取消监听
job.cancel()
// tips: disconnectEvent() 不会被 client.close() 自动调用
client.disconnectEvent()runBlocking {
client.on<IllegalStateException> { _, e ->
println("Receive exception: ${e.message}")
if (e.message != "test exception") {
this@runBlocking.cancel(CancellationException(e.message, e))
}
}
}