Skip to content

Commit

Permalink
feat: support message chain
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Aug 31, 2024
1 parent 900ed78 commit f4f4af2
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 28 deletions.
45 changes: 29 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
> 最低JDK版本为 `11`
# 如何使用

框架需要使用`java-websocket`, `gson`, `kotlinx.coroutines` 三个库

## 添加Maven仓库

```kotlin
maven {
name = "repo.rtast.cn"
url = uri("https://repo.rtast.cn/api/v4/projects/33/packages/maven")
}
```

## 添加依赖

```kotlin
dependencies {
implementation("cn.rtast:ROneBot:{version}")
}
```

# 最小实例

```kotlin
Expand Down Expand Up @@ -63,25 +84,17 @@ fun main() {
}
```

# 使用
# 消息构造器

框架需要使用`java-websocket`, `gson`, `kotlinx.coroutines` 三个库

## 添加Maven仓库
> 框架支持使用链式调用的方式构造一个消息, 以下是一个简单的示例, 也可以直接使用字符串的形式发送
```kotlin
maven {
name = "repo.rtast.cn"
url = uri("https://repo.rtast.cn/api/v4/projects/33/packages/maven")
}
```

## 添加依赖

```kotlin
dependencies {
implementation("cn.rtast:ROneBot:{version}")
}
val msgChain = MessageChain.Builder()
.addAt(message.sender.userId)
.addText(message.rawMessage)
.addNewLine(3) // repeat 3 times: append 3 \n to end
.build()
this.sendGroupMessage(message.groupId, msgChain)
```

# 注意事项
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kotlin.code.style=official

libVersion=1.3.1
libVersion=1.4.0
14 changes: 14 additions & 0 deletions src/main/kotlin/cn/rtast/rob/entity/misc/PokeMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/31
*/


package cn.rtast.rob.entity.misc

data class PokeMessage(
val name: String,
val type: Int,
val id: Int
)
12 changes: 12 additions & 0 deletions src/main/kotlin/cn/rtast/rob/enums/MusicShareType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/31
*/


package cn.rtast.rob.enums

enum class MusicShareType(val type: String) {
QQ("qq"), Netease("163"), Xiami("xm")
}
29 changes: 29 additions & 0 deletions src/main/kotlin/cn/rtast/rob/enums/PokeMessage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/31
*/


package cn.rtast.rob.enums

import cn.rtast.rob.entity.misc.PokeMessage

object PokeEnums {
val Poke = PokeMessage("戳一戳", 1, -1)
val ShowLove = PokeMessage("比心", 2, -1)
val Like = PokeMessage("点赞", 3, -1)
val Heartbroken = PokeMessage("心碎", 4, -1)
val SixSixSix = PokeMessage("666", 5, -1)
val FangDaZhao = PokeMessage("放大招", 6, -1)
val BaoBeiQiu = PokeMessage("宝贝球", 126, 2011)
val Rose = PokeMessage("玫瑰花", 126, 2007)
val ZhaoHuanShu = PokeMessage("召唤术", 126, 2006)
val RangNiPi = PokeMessage("让你皮", 126, 2009)
val JieYin = PokeMessage("结印", 126, 2005)
val ShouLei = PokeMessage("手雷", 126, 2004)
val GouYin = PokeMessage("勾引", 126, 2003)
val ZhuaYiXia = PokeMessage("抓一下", 126, 2001)
val SuiPing = PokeMessage("碎屏", 126, 2002)
val QiaoMen = PokeMessage("敲门", 126, 2002)
}
148 changes: 148 additions & 0 deletions src/main/kotlin/cn/rtast/rob/util/message/MessageChain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/8/31
*/


package cn.rtast.rob.util.message

import cn.rtast.rob.entity.misc.PokeMessage
import cn.rtast.rob.enums.MusicShareType

class MessageChain internal constructor(builder: StringBuilder) {

internal val finalString = builder.toString()

class Builder {
private val stringBuilder = StringBuilder()

fun addAt(userId: Long): Builder {
stringBuilder.append("[CQ:at,qq=$userId]")
return this
}

fun addText(text: String): Builder {
stringBuilder.append(text)
return this
}

fun addImage(file: String): Builder {
stringBuilder.append("[CQ:image,file=$file]")
return this
}

fun addFace(id: Int): Builder {
stringBuilder.append("[CQ:face,id=$id]")
return this
}

fun addRecord(file: String): Builder {
stringBuilder.append("[CQ:record,file=$file]")
return this
}

fun addVideo(file: String): Builder {
stringBuilder.append("[CQ:video,file=$file]")
return this
}

fun addRPS(): Builder {
stringBuilder.append("[CQ:rps]")
return this
}

fun addDice(): Builder {
stringBuilder.append("[CQ:dice]")
return this
}

fun addShake(): Builder {
stringBuilder.append("[CQ:shake]")
return this
}

fun addPoke(poke: PokeMessage): Builder {
stringBuilder.append("[CQ:poke,type=${poke.type},id=${poke.id}]")
return this
}

fun addShare(url: String, title: String): Builder {
stringBuilder.append("[CQ:share,url=$url,title=$title]")
return this
}

fun addContactUser(userId: Long): Builder {
stringBuilder.append("[CQ:contact,type=qq,id=$userId]")
return this
}

fun addContactGroup(groupId: Long): Builder {
stringBuilder.append("[CQ:contact,type=group,id=$groupId]")
return this
}

fun addLocation(lat: Double, lon: Double, title: String? = null, content: String? = null): Builder {
stringBuilder.append("[CQ:location,lat=${lat},lon=${lon}")
if (title != null) stringBuilder.append(",title=$title")
if (content != null) stringBuilder.append(",content=$content")
stringBuilder.append("]")
return this
}

fun addMusicShare(type: MusicShareType, id: String): Builder {
stringBuilder.append("[CQ:music,type=${type.type},id=$id]")
return this
}

fun addCustomMusicShare(
url: String,
audio: String,
title: String,
content: String? = null,
image: String? = null
): Builder {
stringBuilder.append("[CQ:music,type=custom,url=$url,audio=$audio,title=$title")
if (image != null) stringBuilder.append(",image=$image")
if (content != null) stringBuilder.append(",content=$content")
stringBuilder.append("]")
return this
}

fun addReply(messageId: Long): Builder {
stringBuilder.append("[CQ:reply,id=$messageId]")
return this
}

fun addForwardMessage(messageId: String): Builder {
stringBuilder.append("[CQ:forward,id=$messageId]")
return this
}

fun addForwardNodeMessage(messageId: String): Builder {
stringBuilder.append("[CQ:node,id=$messageId]")
return this
}

fun addXMLMessage(xml: String): Builder {
stringBuilder.append("[CQ:xml,data=$xml]")
return this
}

fun addJsonMessage(json: String): Builder {
stringBuilder.append("[CQ:json,data=$json]")
return this
}

fun addNewLine(repeatTimes: Int = 1): Builder{
repeat(repeatTimes) {
stringBuilder.append("\n")
}
return this
}

fun build(): MessageChain {
return MessageChain(stringBuilder)
}
}
}
13 changes: 3 additions & 10 deletions src/main/kotlin/cn/rtast/rob/util/ob/OBAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import cn.rtast.rob.entity.out.SetGroupNameOut
import cn.rtast.rob.entity.out.SetGroupNameOut.Params
import cn.rtast.rob.entity.out.SetGroupRequestOut
import cn.rtast.rob.entity.out.SetGroupWholeBanOut
import cn.rtast.rob.util.message.MessageChain
import cn.rtast.rob.util.toJson

interface OBAction {
Expand All @@ -55,16 +56,8 @@ interface OBAction {
this.sendToWs(GroupMessageOut(params = GroupMessageOut.Params(groupId, content)))
}

/**
* if appendNewLine is true, the content will be @{user}\n{content}
* if it is false, content will be @{user}{content}
*/
suspend fun sendGroupMessageWithAt(groupId: Long, userId: Long, content: String, appendNewLine: Boolean = true) {
val content = StringBuilder("[CQ:at,qq=$userId]").also {
if (appendNewLine) it.append("\n")
it.append(content)
}
this.sendGroupMessage(groupId, content.toString())
suspend fun sendGroupMessage(groupId: Long, content: MessageChain) {
this.sendGroupMessage(groupId, content.finalString)
}

suspend fun sendPrivateMessage(userId: Long, content: String) {
Expand Down
8 changes: 7 additions & 1 deletion src/test/kotlin/TestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.message.MessageChain
import cn.rtast.rob.util.ob.OBMessage
import org.java_websocket.WebSocket

Expand All @@ -15,7 +16,12 @@ fun main() {
val wsAccessToken = System.getenv("WS_ACCESS_TOKEN")
val rob = ROneBotFactory.createClient(wsAddress, wsAccessToken, object : OBMessage {
override suspend fun onGroupMessage(websocket: WebSocket, message: GroupMessage, json: String) {
println(message.rawMessage)
val msgChain = MessageChain.Builder()
.addAt(message.sender.userId)
.addText(message.rawMessage)
.addNewLine(3) // repeat 3 times: append 3 \n to end
.build()
this.sendGroupMessage(message.groupId, msgChain)
}

override suspend fun onWebsocketError(webSocket: WebSocket, ex: Exception) {
Expand Down

0 comments on commit f4f4af2

Please sign in to comment.